New function: hackDoNotSave()

For AI and campaign script related discussions and questions
Post Reply
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

New function: hackDoNotSave()

Post by Per »

Add a new, experimental function hackDoNotSave(global name).

It adds global by name to list of variables to not save to savegames. This too is not saved to savegames, so must be called again whenever game is restarted from a savegame.
User avatar
Prot
Trained
Trained
Posts: 242
Joined: 29 Nov 2010, 12:41

Re: New function: hackDoNotSave()

Post by Prot »

Why not just clear useless variables, when event save game is called? Or var's anyway saved but empty?
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: New function: hackDoNotSave()

Post by Per »

Prot wrote:Why not just clear useless variables, when event save game is called? Or var's anyway saved but empty?
That should work, too.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: New function: hackDoNotSave()

Post by NoQ »

Prot wrote:Why not just clear useless variables, when event save game is called? Or var's anyway saved but empty?
You'd need to regenerate them when saving is over, not just after loading, which is extra coding. Or you'd want to store them in another variable, which will be saved but under a different name, and then bring them back, which is also extra coding.

It's cool to be able to replace this extra work with a single line:

Code: Select all

var V;
hackDoNotSave('V');
That said, variables in anonymous closures work too, if that's comfortable for your purposes:

Code: Select all

(function(global) {
    var V;
    global.getV = function() { return V; }
    global.setV = function(v) { V = v; }
})(this);
(in this case V would not be saved, but it can escape by reference and live and be accessible for as long as you want)

Per wrote:must be called again whenever game is restarted from a savegame.
If used as in the first code snippet above, i guess it'd work, so it's cool. I was wrong about that in the original proposal.
Last edited by NoQ on 20 Mar 2017, 15:59, edited 2 times in total.
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: New function: hackDoNotSave()

Post by Per »

I'm a bit worried that using tricks like anonymous closures would make the code hard to read and understand, though. One of our primary aims should be to make the code easy to read, understand and improve by anyone who has done some coding before.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: New function: hackDoNotSave()

Post by NoQ »

Yeah. Well, since we started this discussion from hook problems, adding some anonymous closures wouldn't make things much worse. But i agree that letting the scripters stick to simple variables and functions is very beneficial.
Post Reply