Are Persistent Variables Possible?
0

5 posts in this topic

Recommended Posts

Is it possible to declare variables in a lua script that will persistent beyond the lifetime of the function and into further instances? In other words, if I have a script that's called by Events.OnPlayerUpdate.Add(), can I declare a variable that will retain it's value and be used in future instances of that script being triggered by Events.OnPlayerUpdate.Add()? What I want in the end is the ability to store a value that will increment each time that function runs, but the problem I'm having is that the variable appears to get dropped and thus never actually increments. I'm not sure if what I'm looking for is possible or not though

Share this post


Link to post
Share on other sites
Posted (edited)

If you want this variable to persist throughout your script, and be usable in other functions in this same file, you simply need to declare it as a global variable.

Here is a basic example:

local testVar = 0

local function Global()
    testVar = testVar + 1
    print(testVar)
end

 

However, if you would like this variable to persist when quitting the game, you can save it as ModData and it will be saved to the specific character that executed the script.

Here is a basic example of this:

--Saving
getSpecificPlayer(0):getModData()["Test"] = ZombRand(100) --set "Test" to a random value between 0-99

--Loading
if (getSpecificPlayer(0) ~= nil) then --if player not nil
  print("*****test value:*****")
  print(getSpecificPlayer(0):getModData()["Test"]) --print the value
end

 

Edited by nater

Share this post


Link to post
Share on other sites
1 hour ago, nater said:

If you want this variable to persist throughout your script, and be usable in other functions in this same file, you simply need to declare it as a global variable.

Here is a basic example:


local testVar = 0

 

Technically, thats not a global. Its a local variable, with the scope of the file is written in, and can only be accessed from that file. A global variable is defined in lua without the 'local' keyword, and accessed from any file. Not trying to be picky but it helps to clarify when explaining.

Global variables should be avoided, Local variables with a file scope (like the previous example) are much more preferred.

Share this post


Link to post
Share on other sites
Posted (edited)
49 minutes ago, Fenris_Wolf said:

 

Technically, thats not a global. Its a local variable, with the scope of the file is written in, and can only be accessed from that file. A global variable is defined in lua without the 'local' keyword, and accessed from any file. Not trying to be picky but it helps to clarify when explaining.

Global variables should be avoided, Local variables with a file scope (like the previous example) are much more preferred.

 

True, I should've specified scope rather than global,  but that's why I said 'functions in this same file' and declared it local, I always avoid global variables so it slipped my mind!

Edited by nater

Share this post


Link to post
Share on other sites

Thank you both so much, this is exactly what I had needed. Originally, I wasn't sure why my global variables weren't behaving that way, but later discovered that's because of a design error that was resetting those variables. I've since fixed that. Extra kudos to nater for bringing ModData to my attention, as I didn't know doing that was a possibility and I do need these values to persist across save/load.

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
0