tempeffect_def not saved?

Moderator: Moderator

Post Reply
Message
Author
yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

tempeffect_def not saved?

#1 Post by yufra »

A bit of background: there are some situations in Viral Resistance that I want to have a countdown before another set of actions occur. I decided to code this is a temporary effect as follows:

Code: Select all

player:newEffect{name="OUTER_LOCKDOWN_COUNTDOWN", desc=[[]], type="countdown", 
	on_lose = function(self, err)
		if player:isQuestStatus("code_green", engine.Quest.COMPLETED, "lockdown") then return end
		local chat = engine.Chat.new("lockdown", {name="Surveillance"}, player)
		chat:invoke("auto_lockdown")
	end,
}
This works beautifully... until you save. Apparently the tempeffect_def table is not saved, and an error is thrown as soon as I try loading a game with one of these created effects. I've taken a quick look at Savefile and even untarred the game.teaw to confirm that tempeffect_def is not in the mod.class.Player file, but it will probably take me a little while to understand why the tempeffect_def table is not being saved. Any thoughts DG as to why, and if it shouldn't be saved then how to fix the above problem?
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: tempeffect_def not saved?

#2 Post by darkgod »

This table is the definition of temporary effects it's recreated on load from the timed_effects.lua file


You should give me the error log but just form reading your code I suspect the problem is someting like "tyring to index a nil value" ? ;)
You refer to "player" inside your function, but where is player defined ? not inside your function, this is the dreaded reference to an upvalue !
Inside this method you can jsut use self as it's the entity on which it works.

Now you also seem to create the new effect on the fly yes ? This indeed can not work, you must register temporary effects on load.

Maybe you need something like a GameTimers interface ?
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: tempeffect_def not saved?

#3 Post by yufra »

The code I pasted was from within a chat's action function, so player was defined. The error is a bit of an odd one, coming from these lines in PlayerDisplay actually:

Code: Select all

	for eff_id, p in pairs(player.tmp) do
		local e = player.tempeffect_def[eff_id]
		if e.status == "detrimental" then
Well odd when trying to debug it. It is easy to understand why when you realize tempeffect_def is not saved, simply recreated from temporary_effects.lua. A GameTimers interface would work nicely, and could thus not require the additional "on_turn" code in Mount Doom's zone.lua and support multiple timers running at a time.
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: tempeffect_def not saved?

#4 Post by darkgod »

yeah :)

As for plaeyr being defined, that's the closure thing we talked about.
player is defined outside the funtion, so when the function gets reloaded from a savefile it wont be able to access it anymore.

There are 3 kinds of variables:
* globals: they are defined globaly and sthus can be refered from a saved function
* locals: they are defined inside the scope of the function, and thus work too
* upvalues: they are references to locals defined in the caller (or caller of caller, ...) of the function, and thus do not work when reloaded because the caller scope does not exist anymore at this point

(ok they are not realyl three kinds at all, but that's good enough to explain the phenomenom :) )
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: tempeffect_def not saved?

#5 Post by yufra »

darkgod wrote: As for plaeyr being defined, that's the closure thing we talked about.
player is defined outside the funtion, so when the function gets reloaded from a savefile it wont be able to access it anymore.
Hehe, I need to include more of the functions when I post code for you DG. :wink:

This time I am posting from the PlayerDisplay:display function, which redefines the global game.player to the local player variable, so no upvalues here. The problem is that the newEffect definition in the Chat code (first post) adds an additional entry to tempeffect_def and the index to that new entry into game.player.tmp. When you reload the entry in tempeffect_def is lost, but the reference to its index still exists in game.player.tmp. This results in the local variable e in PlayerDisplay (second post) being a nil value, and then you try subscripting the status variable which throws the error.

Maybe there should be some sort of warning, either in the LuaDoc of newEffect or at runtime that effects that are not loaded in load.lua can cause errors within the game?
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: tempeffect_def not saved?

#6 Post by darkgod »

Yeah :)

Same for talents and such BTW.

As for the closure/upvalue thing I was speacking for the general case not specifically here, sorry :)
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: tempeffect_def not saved?

#7 Post by yufra »

darkgod wrote: As for the closure/upvalue thing I was speacking for the general case not specifically here, sorry :)
No problem, thanks for the clarification. :)
<DarkGod> lets say it's intended

Post Reply