PDA

View Full Version : Scripting: save custom variables set with setVar when respawning an object



Huffel
06-14-2016, 12:52 PM
Values saved to an object with the setVar function are currently lost when the rewind function is used and the object is loaded again. I think it would be a great help to have them loaded again automatically.

Currently the only way to safely identify an item type is by putting the ID in the name or description. However that is only safe as long as players do not change them.

The GUIDs do not change when rewinding, so a possible workaround could be to store the values for each object in a global table and have each item check if there are any values stored for its GUID in the onLoad event.

dig65
06-14-2016, 03:34 PM
You should encode them (write Lua script) to the object when you want the values to be permanent across saves, rewinds, etc.

MrStump
06-14-2016, 11:46 PM
I'm not saying this isn't a good suggestion. But there is a solution for it currently in the LUA script I think. You could use setLuaScript( string script ) to inject function onSave() and function onload() commands. Have the onSave copy the var value and the onload load the var value. I have not used setLuaScript before, but it looks pretty straight forward. Here is an approximation of the script it could inejct


function onSave()
local data_to_save = {variable = your_variable_you_set}
saved_data = JSON.encode(data_to_save)
return saved_data
end

function onload(saved_data)
local loaded_data = JSON.decode(saved_data)
if loaded_data == '' then
your_variable_you_set = nil -- or 0, or '', or whatever is a void value for it
else
your_variable_you_set = loaded_data.variable
end
end

Another possible idea would be to have your script keep track of what setVar it is applying and when they should be applied. Then, it can save that information in a table using onSave in your main code, then re-apply the setvar during onLoad in your main code! Here is an example of how you could save this hash table with a record of the objects names and values (assuming you did not remove or undo those edits).


object = getObjectFromGUID('43023') --The object
local name = 'variable name' --variable name
local value = 0 --variable value
object.setVar( 'variable name', 0) --setting name/value
local for_table = {GUID = object.getGUID(), name = name, value = value}
table.insert(table_to_save, for_table)

Then you could use onSave and onload to have that table be loaded up on each load/undo and re-apply all values to their objects. I hope this helps you with your problem while you hope for a change to setVar.

Huffel
06-15-2016, 05:30 AM
Thanks for the suggestions to both of you. I'm sure I am going to use them when I work on making my mod rewind-friendly. However I have put that task on hold for now since there currently is a bug with scripting zones that I cannot work around.

@MrStump:
You second suggestion is about what I had in mind. I have not used the onSave event yet I'll have a look at that too.

Sancho
10-02-2016, 03:56 AM
Thanks for the suggestions to both of you. I'm sure I am going to use them when I work on making my mod rewind-friendly. However I have put that task on hold for now since there currently is a bug with scripting zones that I cannot work around.

Mind sharing what that bug is?

Huffel
10-02-2016, 04:03 AM
http://www.berserk-games.com/forums/showthread.php?2503-Spawning-a-custom-object-inside-scripting-zone-breaks-onObjectEnter-Leave-events

This is the bug. And despite what I wrote above I got my mod to be very rewind friendly by now(apart from the huge amount of objects to be loaded). The bug does rarely affect the game but if it does it is a big problem because it is very hard to notice. I have certain items that give victory points when in a zone (e.g. Longest Road card) and if rewind is used they will not be deducted for the player holding the card anymore if he has to give it away.

Mark
10-02-2016, 08:08 AM
That doesn't work very well if the object with the set variable is inside a deck or a bag at the time of rewinding.
I'd prefer this method too. Much like a script attaches to an object, so should variables. (We can't use setLuaScript() if the object is already spawned unfortunately.)

Sancho
10-02-2016, 08:13 AM
http://www.berserk-games.com/forums/showthread.php?2503-Spawning-a-custom-object-inside-scripting-zone-breaks-onObjectEnter-Leave-events

This is the bug. And despite what I wrote above I got my mod to be very rewind friendly by now(apart from the huge amount of objects to be loaded). The bug does rarely affect the game but if it does it is a big problem because it is very hard to notice. I have certain items that give victory points when in a zone (e.g. Longest Road card) and if rewind is used they will not be deducted for the player holding the card anymore if he has to give it away.

Very cool, thanks. I actually think I recall running into that bug on your mod a long while back. Is there a workaround that you use when this happens?

EDIT: Also sorry for the resurrection. I hadn't realized how old this thread was.

Huffel
10-02-2016, 08:32 AM
That doesn't work very well if the object with the set variable is inside a deck or a bag at the time of rewinding.
I'd prefer this method too. Much like a script attaches to an object, so should variables. (We can't use setLuaScript() if the object is already spawned unfortunately.)
My workaround for the setVar variables not being saved is just put it in the onLoad event of the item:
function onLoad() self.setVar('sType', 'something') end
Works also for items in decks and bags. As soon as they come out they will set the variable themselves. The downside is that I end up having ~100 objects with scripting when everything is spawned. I do not know if that makes any difference for the performance but it tends to crash Atom when you press rewind and every object is loaded in the editor, I can live with that though ;-)


Very cool, thanks. I actually think I recall running into that bug on your mod a long while back. Is there a workaround that you use when this happens?
No, there is no workaround and absolutely nothing I can do about it (except creating an example and post it on the forums...).

Mark
10-02-2016, 04:40 PM
My workaround for the setVar variables not being saved is just put it in the onLoad event of the item:
function onLoad() self.setVar('sType', 'something') end
Works also for items in decks and bags. As soon as they come out they will set the variable themselves. The downside is that I end up having ~100 objects with scripting when everything is spawned. I do not know if that makes any difference for the performance but it tends to crash Atom when you press rewind and every object is loaded in the editor, I can live with that though ;-)

I can't use that method because I don't always have a static variable stored in each object, rather it comes and goes and is dependant on other variables in my script.

It's quite a lengthy workaround.