Scripts / Local VS Global

Author
Message
Mariokiki
15
Years of Service
User Offline
Joined: 22nd Oct 2008
Location:
Posted: 12th Jun 2016 17:19
Hi everybody !

You perhaps already know, you the "Super scripts coders"... but me, I didn't know !

It's been over an hour I'm looking for why my scripts doesn't work correctly.

In fact, I used variables that have the same name in several scripts, and by default they was recognized as "global" because I forgot to mention "local" before the variables !

By activating a switch, I didn't move the good object... and if I added an object using the same script as before, it was always the last one who was active with the switch...

Once I added "local" before the variables, each switch work correctly with its respective target objet !

I don't know if this will be a good information to share, but if you don't want to seem like an idiot (like me!) then remember this important rule!

LOCAL is not GLOBAL !

Thanks !
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 12th Jun 2016 17:38
Global can be accessed/modified for whatever function, along the game running.
Local, just for the function than declare it.

If you declare 2 vars named same, in 2 different functions it works, anyway I prefer to name different each one, or at least add some letter to make it exclusive, IE; "local timer" and "local n_timer", if I want keep their name for relationship, and easy identify.

3com

Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
Mariokiki
15
Years of Service
User Offline
Joined: 22nd Oct 2008
Location:
Posted: 12th Jun 2016 18:05

I can do like this also... (different name in each script)

Thanks for the tip 3Com !!
PM
PartTimeCoder
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location:
Posted: 12th Jun 2016 18:18
using local not only localizes the variable it also helps to reduce the memory Lua is using, on today's machines it is probably a negligible amount but once upon a time it mattered, and anyway its good coding practice and avoids errors, a variable should only be global if it absolutely needs to be global.

also, you can define local functions within scripts, this is handy for when making table.function pairs and need 'class' like behavior with public and private members.

PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Jun 2016 21:55
Local variables are also much quicker to access, global variables are stored in a list and when used are looked up by name, local variables are stored in registers and are accessed instantly.

If you are reading a global variable many times in a script a neat trick is to localise it, for example:

local gX = g_Entity[e]['x']

or even

local Entity = g_Entity[e]
local gX = Entity.x
local gZ = Entity.z

Now simply use gX wherever you previously had g_Entity[e]['x']

Another efficiency you can use is to use squared values for checking distances, for example (assuming you have localised x & z for both player and entity):

local DX, DZ = pX-eX, pZ-eZ
if (DX*DX + DZ*DZ) < (200 * 200) then

gives the same result as calling PlayerDistance and comparing with 200 but is far more efficient (Lua precalculates constant expressions like the 200 * 200 so at runtime they are simply a constant value).
Been there, done that, got all the T-Shirts!
PM
Mariokiki
15
Years of Service
User Offline
Joined: 22nd Oct 2008
Location:
Posted: 12th Jun 2016 22:57
Thank you guys for advice !

I use global variables when I need to trigger an action from a script to interact on a second script... (action on first object create an action on a second object)

In my example, these are levers objects that control stone objects deplacement.
The problem occurs when I've duplicated multiple levers objects by copy/paste without changing the variables names in scripts; only the script name changed...

It works fine now. At the same level, I work independently 3 levers objects that control the deplacement of 3 stone objects.
see picture attached...
More clearer now for me ! Thanks again !

Attachments

Login to view attachments
PM

Login to post a reply

Server time is: 2024-05-04 09:15:15
Your offset time is: 2024-05-04 09:15:15