Jump to content
Sign in to follow this  
Pete_SMS

Static Variables in Lua?

Recommended Posts

Hi,

I ran into a problem with Lua, where I need a static variable inside a function, which retains its value after each call, so I can use it in the next. In C/C++ this is straight forward by using the keyword "static". However, in Lua there is no such thing I guess.

So the questions are, can I use something similar to "static" and how do I initialize a "static" variable in Lua, if possible. I guess I can use the global variable approach, but I am not a huge fan of global variables.

Would be great if somebody could help. Thanks!

Share this post


Link to post
Share on other sites

Hi, Peter,

In LUA, a global is essentially a static. Of course, this is loose ended due to the fact that a global is well...global to all files.The name "static" is commonly dropped, however, as many languages consider local and global to be enough to distinguish between one another.

In short, globals are your way to go in the scenario you're looking for...sooooorta....

One thing you can do is create local variables OUTSIDE of functions. I haven't done enough research yet on whether such variables are local only to the file they are in when outside of a function, so please don't take this last comment as anything definitive. I've heard it both ways from people. One saying that locals will basically be turned into globals for all files, while another saying locals outside of a function will be applied to the file they are in only.

Share this post


Link to post
Share on other sites

Thanks Cameron!

I will check out the "locals outside of a function" theory. Would be good if they stay global just in the current file. Otherwise I have to use an identifier for my variables, so I can easily see, if a certain variable is global or not. That should help messing up things, when the code base gets large.

Currently working on multi-stage supercharger plug-in. This plug-in will completely bypass the Planemaker/X-Plane FADEC system and critical altitude setting, allowing me to make the engine-supercharger combination behave exactly like the real thing at all altitudes, with respect to available power and manifold pressure.

I coded up a small proof of concept in Gizmo and it worked great. Now comes the proper coding with all the fancy algorithms and I need variables which retain their values.

I will come back and let you know how the theory worked out with locals outside a function.

Share this post


Link to post
Share on other sites

...back again and I checked what you told me with respect to locals outside of a function and if they are global only for that specific file they are in.

A quick check confirmed what you said:

(1) When I declare an ordinary global variable in a file outside of a function, it will be global and visible in all other files, too.

(2) When I declare a local variable in a file outside of a function, it will be global (and static) in that specific file, but is not visible in other files.

Thats cool and keeps the code more tidy. Thanks for letting me know Cameron. Much appreciated!

Share this post


Link to post
Share on other sites

I wasn't aware of the "local to file only" usage, cool!

To the best of my knowledge lua has no way of creating the typical "static" variable which in other languages is read only and cannot be changed... You could use a function though...

Function getGlobalReadOnlyValue()

Return 1234.5

End

....kinda fiddly but it buys you protections.

Share this post


Link to post
Share on other sites

I hate it to commit Thread necrosis, but I had to correct this. Lua should essentially give you the requested unchangeable Variable if you are just using the const modifier.

 

Edit: Too Dumb to read.... sry

Edited by Idiot

Share this post


Link to post
Share on other sites

I hate it to commit Thread necrosis, but I had to correct this. Lua should essentially give you the requested unchangeable Variable if you are just using the const modifier.

Please explain.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×