Scripts / [SOLVED] Newbie to Lua; 2 questions; about Images/sprite 2-global var safe?

Author
Message
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 17:42
Hi Guys! I am quite new to Lua and late to the party... so maybe my questions will not make much sense to ya, but i will risk asking them anyway. I have search other treads and read a lot but i am still confuse...so here i go;

1-When you load images (for sprites), how much memory do you have to store them? Can i have sprites the size of the entire screen in order to create nice HUDs (using transparent color at saving time) and make them appear on a press of a button? I am asking because i tried this and I keep getting error 301 and GG stop then reboot my level. Here is an exemple i am working on; (but it bug out with 'error 301() 'when i switch them using g_KeyPress and g_Scancode...)

I think GG is great and i am waiting for GGM to hit the steam store. My money is ready!!!

function mydisplay_init(e)

amyHud=0
g_amyHudSprite=0
g_amyHudSprite1=0

HideHuds()

amyHud = LoadImage("scriptbank\\images\\magnanir\\images\\HUD_MagnanirBase1.png")
g_amyHudSprite = CreateSprite ( amyHud )
SetSpriteSize ( g_amyHudSprite, 100 , 100 )
SetSpriteDepth ( g_amyHudSprite, 100 )
SetSpriteColor ( g_amyHudSprite, 255,255,255,250)
SetSpritePosition ( g_amyHudSprite, 0,0)

amyHud = LoadImage("scriptbank\\images\\magnanir\\images\\advsheet1.png")
g_amyHudSprite1 = CreateSprite ( amyHud )
SetSpriteSize ( g_amyHudSprite1, 100 , 100 )
SetSpriteDepth ( g_amyHudSprite1, 99 )
SetSpriteColor ( g_amyHudSprite1, 255,255,255,250)
--to hide the sprite, I move it away from the screen
SetSpritePosition ( g_amyHudSprite1,100,100)
end

function mydisplay_main(e)

if g_PlrKeyJ==1 then
SetSpritePosition ( g_amyHudSprite,100,100)
PasteSpritePosition (g_amyHudSprite1,0,0)
end
--if player use T...
if g_Scancode==20 then
SetSpritePosition ( g_amyHudSprite1,100,100)
PasteSpritePosition (g_amyHudSprite,0,0)
end

end


2-My second question is about variables. How can i make sure i am not overwriting a value with another script attached to another entity? For exemple, if i used g_amyHudSprite1 in this script, how can i be sure that i am not assigning it another value in another script? (sending my sprite in the confusion world...) Should i create a 'global script loading all my HUD first (images)? Where should i put my variable?

Thank you for your understanding. I have learn so much in 3 weeks, but i am hitting a wall here...
When I died, I want to be buried in Cyrodil!

The author of this post has marked a post as an answer.

Go to answer
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 25th Aug 2021 18:30 Edited at: 25th Aug 2021 18:31
your script is fine (except maybe you will want to add some toggle for the key press rather than having it spam trigger while the inputs are held down) - so it's likely a different script causing your error

for variables you make them 'local'
i.e.
local myvariable = 1
will make 'myvariable' = 1 for any objects with that script but if you made a new script for other objects you can make a new local variable with the same name and give it a different value.
to avoid overwriting global variables you can use something like
myvariable = myvariable or 1
so if it already exists it will take the old value otherwise it will be set to 1
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Aug 2021 19:04 Edited at: 25th Aug 2021 19:09
The LoadImage function uses memory to store the actual image and will use enough memory to store the whole uncompressed version of the image (i.e. the size it would be if in bmp format - or whatever the actual internal format DX uses to store images in).

Creating a sprite only uses a small amount of memory for each one as all it does is store the values unique to that sprite (screen location, scaling, rotation, colour etc) and a pointer to the actual image.

So in summary if you had a display made up of lots of the sprites of the same image (maybe tiled or something) then only the storage for one copy of the actual image needs to be worried about.

If creating a single sprite from an image you should combine the calls like this:


That way you can't accidentally delete the image and cause grief later on.

Been there, done that, got all the T-Shirts!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 19:20
For my first question; Thanks! Yeah, i was thinking about having my character sheet appear with an on/off switch variable, but i came from Dark Basic so i am a little bit out of my wheel here... My problem is i am having a character sheet, but also an inventory sheet, then a HUD sheet...and some Special Event in the game use other big images the size of the screen, so.... So it is kind of weird for me not to have an exact number to identify each sprite to call them up/control them. OK, i will check my other scripts and see what could cause the problem. Do you think i should group all my loading images call to one place (as soon as the player get into the level?), and set 'always active' to the entity i set this on?... i think i goof and place my loading images a little bit everywhere as the player approach the event, so that's maybe my problem...

For my second one; Ok i get it better with your answer. 'local' keep this variable into that particular script. if i use a global one, so it can be affected in other script on my level, i can set it equal to itself 'or 1' before i use it again in any script. Got it. That should be my problem... i am probably using that same variable somewhere else...

When I died, I want to be buried in Cyrodil!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 19:43
Many thanks smallg!!!

AmenMoses thanks for your reply!

Well now that you talk about this, this is confusing for me... if a load an image and have 'amyHudSprite' as the holder in that particular script, what happen if i use it again into another one in the same level? is amyHudSprite suppose to update to 'value=2' by itself or is it gonna go back to 'value=1' and overwrite the image number returned from the first call in the first script? If i follow smallg line of thought, i would certainly consider making my amyHudSprite a global variable! as g_amyHudSprite and declaring it in my init or even outside of a function?

(script1)
g_amyHudSprite=0

function hud_init(e)
g_amiHudSprite=CreateSprite(LoadImage( "scriptbank\\images\\magnanir\\images\\HUD_MagnanirBase1.png" ) )
code here...
end
function hud_main(e)
code here...
end

(script2)
function hud2_init(e)
g_amiHudSprite=CreateSprite( LoadImage( "scriptbank\\images\\magnanir\\images\\HUD_CharacterSheet.png" ) )
code here...
end
function hud2_main(e)
code here...
end

is g_amiHudSprite=1 or 2 now? and how do i call back my first or second image?

PasteSpritePosition (g_amyHudSprite,0,0)
or
SetSpritePosition ( g_amyHudSprite,0,0)

How to make sure which one sprite i am invoking in this case?
When I died, I want to be buried in Cyrodil!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Aug 2021 19:52
Use different names for the sprites!

i.e.:
g_amiHudSprite1=CreateSprite(LoadImage( "scriptbank\\images\\magnanir\\images\\HUD_MagnanirBase1.png" ) )

...
g_amiHudSprite2=CreateSprite( LoadImage( "scriptbank\\images\\magnanir\\images\\HUD_CharacterSheet.png" ) )
Been there, done that, got all the T-Shirts!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 20:11
Ok Good!
Thanks AmenMoses!

And all of them should be global (g_).

Do I put them in a Init? or totally outside of a function?

if the variable is outside of a function, is it automatically 'global' and will it be maintained even if the entity dissapear later on? or do i have to put g_ to tell the engine it is global one?

i should probably put 'always active' to yes for my entity supporting that global initialization?

God, i feel like a total idiot here...

But honestly, i feel lost when it comes to how GG handle variables//scripts in a level.
i know it let them out when outside a certain range to get better performance.
it is certainly a good thing, but God, does it make it hard to understand...
When I died, I want to be buried in Cyrodil!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 21:36
Guys! You will not believe it...

This is what i did now and it now works...

listen...

In my init() i had this, remember? ;

amyHud=0
g_amyHudSprite=0
g_amyHudSprite1=0

so i thought this was all good and fine because i was telling GG that i wanted those variable to be Global (so it will know not to give it the same sprite number when creating another sprite into another script... BUT what i didn't realized was that i already did that into another script with g_amyHudSprite=0.

So yeah, it is good to declare them, but not twice...not good...not good at all...

AND you guys will not believe it either....

I just ''out of curiosity'' removed the ones i set as global in the other script, and the script is doing just fine as well...

So basically, DO NOT SET your variable as global if you want to use it for this;

g_amyHudSprite = CreateSprite ( amyHud )

it will get you error 301 as the GG engine seem to try to keep your global (g_amyHudSprite=0 and
g_amyHudSprite1=0) with the CURRENT value (as if it consider it a constant maybe? or try to use it as the same number it was before?) giving it an illegal call as a error 301 ''illegal sprite number''.

I don't know if It make any sense to you, or maybe you can explain it better than me, but declaring them as global was my mistake...

Now my different HUD works perfectly! (although i have a long way to go to make it look good...the size is still wrong ha ha!)

When I died, I want to be buried in Cyrodil!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 23:08
Again, thanks, it was holding me down!
When I died, I want to be buried in Cyrodil!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 25th Aug 2021 23:16
As One of the trap in my game...
[img]null[/img]
When I died, I want to be buried in Cyrodil!
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Aug 2021 09:16
This post has been marked by the post author as the answer.
yes it's better to use different names for new variables, you should only use the same variable if you are referencing the same thing or you are sure it's local and not going to be effecting anything else.
the 'g_' prefix is something GG does rather than a lua requirement and is really just a way to tell the engine this variable should be saved.. any variables without the 'local' keyword are global in lua.

if you use the same global variable in multiple scripts it will get overwritten each time it is assigned to.
i.e.
script1
g_myvariable = 1

script2
g_myvariable = 200

when you use g_myvariable it will be 200 for all future use (script order is based on the entity number in GG, lower numbers execute first)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 26th Aug 2021 10:20
The <scriptname>_init functions are called once for each entity using the script so anything in it should only be related to entity specific initialisation.

Generally things like sprites can be set up outside of the _init or _main functions in which case they will only happen once at the point the first entity that uses the script is spawned.

Go have a read of the bible: https://www.lua.org/pil/contents.html
Been there, done that, got all the T-Shirts!
PM
GOTO HelloWorld!
10
Years of Service
User Offline
Joined: 21st Oct 2013
Location: Skyrim,...and lost in it!
Posted: 26th Aug 2021 16:50
You guys rock!
Thanks a lot for your answers!
Yes, i am gonna check out 'the bible' right away!
Thanks!

When I died, I want to be buried in Cyrodil!
PM

Login to post a reply

Server time is: 2024-04-20 13:11:13
Your offset time is: 2024-04-20 13:11:13