Forum

> > Stranded II > Scripts > Saving local variables?
Forums overviewStranded II overview Scripts overviewLog in to reply

English Saving local variables?

9 replies
To the start Previous 1 Next To the start

old Saving local variables?

Warbringer
User Off Offline

Quote
I tried to do a "remote" quest using separately loaded map. After loading home map again, all variables have vanished. I know about "savevars" command, but is it working for local variables too, not just globals?

old Re: Saving local variables?

DC
Admin Off Offline

Quote
You don't even have to use s2 cmd savevars. There's a parameter in s2 cmd loadmap to take over global variables to the next map.

Technically it doesn't make too much sense to take over any local variables from one map to another because a new map has other objects than the old one. The only exception is maybe the player unit but unfortunately it's not possible for that either.

So if you want to do something like that you would have to script a workaround. The best way I can think of would be to store local variables in global variables manually before leaving the old map and then setting them back as local variables after loading the new map.
You can use s2 cmd getlocal and s2 cmd setlocal for that.

old I'm not worthy!

Warbringer
User Off Offline

Quote
OMG, I'm not worthy for DC himself answering my posts! Such a honor, wow!

Anyway, I don't want to carry local variables to other map. Contrary, I want them remain on their place and keep their values. Aren't they saved together with the whole map?
If not, I will try Your idea.

old Re: Saving local variables?

DC
Admin Off Offline

Quote
Oh... now I get it.
Unfortunately s2 cmd savemap does only save global variables. In thise case I'm not even sure why as it would make sense to be able to save local variables as well

And you're wrong about the worthiness. You play my stuff and you even use it to create new stuff. Two things I highly appreciate. So you totally can expect to get an answer from me.

old Re: Saving local variables?

Warbringer
User Off Offline

Quote
Just kidding about that worthiness But seriously, I was enjoying your game since I was 12, and now I'm 19.

I'll try to move all variables into global. If this works, I'll finally upload demo of my Archipelago Mod!

BTW: But global variables are saved in savegames?
edited 1×, last 04.09.16 06:08:12 pm

old setlocal/getlocal WTF?

Warbringer
User Off Offline

Quote
Dang. These setlocal/getlocal commands are not working properly or maybe I am doing something wrong. Great DC ( ) , could you tell me the exact syntax for these commands?

old Re: Saving local variables?

JasJack67
Super User Off Offline

Quote
You should first make a local variable in the definition (under the ID#) of the object,unit,item, or info...that you will be "get" and "set" from and too.

var = variable [, description] [, default] [Global]

example1 variable $mfv:
var=mfv, my first variable,0,0

That variable is set to 0. Or $mfv=0.

example2 variable $msv:
var=msv, my second variable,5,0

That variable is set to 5. Or $msv=5.
_________________________________________________
If I want to change the variable in example1 from 0 to 12 I would use this:
setlocal "object",100,"mfv",12;

assuming the thing is an OBJECT with the ID = 100
_________________________________________________
If I wanted get the variable and use it in another script I would use a temporary variable to store it, then use that temp var:
$tmp=getlocal("object",100,"mfv");

so at that moment in my script $tmp would = 12
_________________________________________________
Sometimes you may need to get a variable, change it, then set the variable back. I have done this before but do not recall why atm.

1
2
3
$tmp=getlocal("object",100,"mfv");
$tmp2=$tmp+4;
setlocal "object",100,"mfv",$tmp2;

so at that moment I have set the variable at that object to = 16
__________________________________________________
In summery above;
1) We simply set the variable from 0 to 12.
2) We simply got the variable to use it somewhere else...and it was 12 when we got it.
3) Lastly, we got the variable again, added 4 to it, and set it back again so it is 16

edit: Don't forget that if it is not an "object" to change that and the ID to "unit",id or "item",id etc...
edited 4×, last 25.09.16 09:46:47 pm

old Re: Saving local variables?

JasJack67
Super User Off Offline

Quote
Global variables are usually put in the game.inf in the on:start or on:load...But also, anytime you make a variable in your script that does not actually exist, it is created as a global variable, at that moment.

Global variables are global...meaning they can be accessed from anywhere. You can simply use the variable at any time, or any where in any script/s.

An example as to why you want to put it in the game.inf would be:

Say you want to keep track of how many units the player has killed. You would put a variable in the on:start as something like $units_killed=0; Then in every on:kill in the units.inf you might have $units_killed++; that adds +1 every kill.

So, now every time a new game is "started" the variable is set back to 0. Every time the game is saved/loaded the variable is saved and loaded keeping track of the killed units.

If you put it in the on:load every time the player loads a saved game it would be reset to 0...probably not what you want in this case.
edited 2×, last 27.09.16 02:15:41 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewStranded II overviewForums overview