You are not logged in.

Applications: [GameMaster: OPEN] | [Volunteer Testers: OPEN]


This forum will be permanently shut down on Friday 13.07.2018
Please copy or save all important information from old forum before they will be deactivated
We have moved to new board. https://forum.runesofmagic.gameforge.com/Come join us.

1

Tuesday, March 25th 2014, 7:20pm

Few questions about variables and such

Slowly getting my head around lua, but my question is about how do you setup a variable that stays current while the client is running and not be reset each time the function is called., so if I was to use ABC as a counter and every time I called my lua via /script MyLua() I could increment ABC by 1 and use something like DEFAULT_CHAT_FRAME:AddMessage(ABC) to see the result.
Hope that makes sence.

2

Tuesday, March 25th 2014, 9:03pm

There are at least two ways to "declare" variables -- local or global.

In lua, to distinguish between a local variable and a global one, you simple add (or omit) the word local before the variable name/assignment.

local my_local_var = "this is a local variable"

vs.

my_global_var = "this is a global variable"

my_local_var exists only in the current function/macro/script. Once that function is finished executing, the variable gets discarded. Global variables exist outside of any given function. Once created they can be used in multiple functions. Whenever possible it is better to use a local variable.

3

Wednesday, March 26th 2014, 4:58am

Thanks for taking the time to explain that. I will jump back in to the deep end and swim some more.

Noguai

Beginner

Posts: 5

Location: Germany

  • Send private message

4

Wednesday, March 26th 2014, 11:00pm

No, a local variable does not only exist in the current function, etc. You just cannot see local variables in nested scopes, but the other way round of course works. Also Lua does not instantly dispose local variables once you exit a scope.
You probably want to read this: http://lua-users.org/wiki/ScopeTutorial

But as correctly said, you should use locals whenever possible. Simply declare the local outside of your function. You do not need to assign a value, but declare the name.