Jump to content

Checking an undefined variable?


Recommended Posts

I have a self-repairing item, but each item is meant to be unique, with a max repair value.  What I had done before was calculate a max value when the object was made, but it seems to reset when the game is reset.  I have a feeling the value is saved, but I can't call it.  What I mean is, there is no command to call the "max uses".  Surprising, but apparently true.

local maxuses = TUNING.TOTAL_DAY_TIME * 3 + math.random(1,TUNING.TOTAL_DAY_TIME * 0.75)  --Note: Seems to recalculate when the game is loaded, causing incorrect percentage values to be displayed.
inst.components.finiteuses:SetMaxUses(maxuses)
inst.components.finiteuses:SetUses(maxuses)

In a similar instance, the item can be overcharged, but I'm having trouble defining unique max limits.  I wanted to say "if a true-max limit hasn't been defined yet, define one" (since the item is recalculating it's true max limit every time it's overcharged), but apparently asking if an undefined value is nil (which happens the first time an item is overcharged) isn't correct..  I already have ways around this, but I wanted to ask if someone knows what I'm doing wrong.

if truemaxuses == nil then
	local truemaxuses = TUNING.TOTAL_DAY_TIME * 5 + math.random(1,TUNING.TOTAL_DAY_TIME * 0.75)
end

 

Link to comment
Share on other sites

@penguin0616
When a tent is placed it gains a random durability, which I don't ever want to be recalculated, but it's happening when the game is reloaded.
The reason for the random durability is that the dens don't wear down in the wintertime, so come spring, if all their durabilities were identical, all the dens would break simultaneously, which is just weird to witness. 

Link to comment
Share on other sites

@penguin0616

Thar’s just it though, why is the variable being reassigned at all?  The code generates a random number, then assigns that number to the maxuses and uses variable.  This makes sense when an ice den is built.  Hen the game is reloaded, the number of remaining uses remains the same, but it recalculates the number of max uses.  Just seems weird to me.

Link to comment
Share on other sites

@FurryEskimo Because the Lua state is different. Here's a really rough example:
You can check the current date and remember it. That's nice and dandy, but you'll eventually go to sleep and forget. When you check the date again, it's changed. It's a different day, different circumstances, etc. There's no reason the date should stay the same.

Edit: Thought of a better example:
You can write something down in a notebook, and throw it away when you are done. When you get a new notebook, what you wrote down isn't there and you're starting from scratch. No reason for the new notebook to have suddenly become a duplicate of the old one.

(What I'm getting at you're in a new lua state, and your code is the same, so of course it'll generate a new amount because you are explicitly telling it to. Your code isn't written to care about what the first set amount was, so why should it remember it?).

Edited by penguin0616
clarify
  • Like 1
Link to comment
Share on other sites

@penguin0616

I think I understand, but it’s weird that it seems to only change one number of the two.  It assigns the new random number to the max uses component, but not the uses.

i added a line of code that checks to see if the remaining uses is low; if it is a new random number is generated and no one will notice, and a new tent automatically has a low value until it’s updated.  Should be fine.

Link to comment
Share on other sites

@FurryEskimo The default saving system in place for finiteuses remembers the old current uses and applies that to the new finiteuses when you reload. No one's really expecting max uses to change, and it makes more sense for it not to be remembered from a continued development perspective.

Edited by penguin0616
  • Like 1
Link to comment
Share on other sites

in additoin to what penguin0616 said:

if you want a local varibale be known also outside of the if statement, you can use sth like this:

local truemaxuses -- defines the variable and the value will be nil
if truemaxuses == nil then
	truemaxuses = TUNING.TOTAL_DAY_TIME * 5 + math.random(1,TUNING.TOTAL_DAY_TIME * 0.75)
end

ok, in this special case the if check makes no sense because it will be always nil. But simply use the "local" only the first time the variable is defined. Afterwarfs don't write local in front of it..  (thanks penguin0616, I knew local makes variables within functions local, but I did not know this is also true for if statements)

About the save/load stuff:
Don't starve is build like this: If you shut down the server, everything is "deleted", except that stuff that is explicitly saved. And when you start the server again, all scripts do run from beginning, like when it would be the first start and also all AddPrefabPostInits are running again. After that the saved stuff is loaded. So without loading the stuff that was previously saved, it would be like a new server.
Now you can take a look at the files you are interested in, like finiteuses and see that in the OnSave function, only the current uses are saved and loaded, not the maxuses, like penguin0616 said.
If your code above, that makes SetUses is done within AddPrefabPostInint or AddComponentPostInit, it is executed before the loading. So even if SetUses sets it to a strange number, the loading will overwrite it with the last saved value.
Of course you can use AddComponentPostInit to also save the maxuses if you want.

Edited by Serpens
  • Like 1
Link to comment
Share on other sites

@penguin0616 @Serpens

Ohhhhh!  So the new uses is being calculated, but then it’s being overwritten.  I should have guessed.  Little weird that max uses isn’t saved, but I guess I can find a way around it.  It’s not a big deal.  Thanks.

also yeah, I did try that form of variable testing elsewhere.  I defined the local variable outside the function, so the first time it runs the value is nil, and once set it registers as defined.  I was hoping there’d be another way of doing it, because it feels kind of sloppy to me.

Link to comment
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
 Share

×
  • Create New...