Jump to content
Sign in to follow this  
igneous01

convert variable name into string? (not the value)

Recommended Posts

I am working on a function for my campaign that allows me to store arrays of objects with saveVar. There is a limitation with saveVar that does not store the objects inside an array, so when referencing them in a later mission, you get objNull.

So with my handy dandy function, it converts all the objects in the arrays into strings, then saves the array into campaign space. I have another function that call compiles the strings to convert the strings back into objects when I load the next mission.

The problem, is that saveVar requires the string of the variable, and using str or format, only converts the value it holds into a string, which is not what I want.

Is there a way to convert the variable name into a string?

Here is the code of the function:

// Fnc to convert all arrays with objects to strings, save in campaign space
// 0 = [GP_ALLUNITS, GP_AVAILABLE, GP_ASSIGNED, GP_KILLED, GP_WOUNDED, GP_CRITICAL] call Fnc_SaveArray
Fnc_SaveArray = {
private ["_arrays"];
_arrays = _this;

// Convert all object based arrays into strings
{
	private ["_array", "_index", "_indexValue"];
	_array = _x;
	if (count _array > 0) then {
		for "_i" from 0 to ((count _x) - 1) do {
			_index = _i;
			_indexValue = _x select _i;
			_array set [_index, str _indexValue];
		};
	};
} foreach _arrays;

// Save the variables
for "_i" from 0 to ((count _arrays) - 1) do {
	private ["_array"];
	_array = _arrays select _i;
	saveVar (format ["%1", _array]);
};
};

Share this post


Link to post
Share on other sites

Hmm could you give an example of what your returned array would look like if you had it working correctly? I'm not sure where or how you want to save the variable names in relation to the objects arrays.

Share this post


Link to post
Share on other sites

sure, the array starts as follows:

GP_ALLUNITS = [s1, s2, s3, s4, s5, s6, s7... etc]

and I need to convert it into strings so that they can be saved with saveVar

GP_ALLUNITS = ["s1", "s2", "s3", "s4", "s5", ... etc]

then I need to somehow convert the variable name into a string, because saveVar needs that:

GP_ALLUNITS = "GP_ALLUNITS"

Then there is another function that simply call compiles each index in the array and using set, changes the index value from a string back into an object.

Share this post


Link to post
Share on other sites

EDIT: I did not know you had a function that does this already. Please ignore.

Edited by Double Doppler
Did not read question correctly

Share this post


Link to post
Share on other sites

Well you always know your variable names. Either you wrote them directly eg. in the editor name field or you created them through scripting, eg. myGlobal = nearestObjects ... or something like that.

If you don't know the names of the variables how would you reference them in the next mission?

I think you are confusing the issue of converting an object to something you can store with finding the variable name. If you use 'str' on an object you might get something back like: <3dObject: #someID g4rb4g3 - can't remember exactly how it looks. There is no way you can find the real object that string is in a next mission.

Let me ask why are you storing this information? Is it to have the battlefield looking like it continues from the previous mission? Then perhaps something like this:

BattlefieldState = [
 [getPos veh_a, getDir veh_a, typeOf veh_a, getDamage typeOf veh_a],
 .....
];
saveVar "BattlefieldState";

Then in your next mission you read the data and recreate it.

Edited by Muzzleflash

Share this post


Link to post
Share on other sites
Well you always know your variable names. Either you wrote them directly eg. in the editor name field or you created them through scripting, eg. myGlobal = nearestObjects ... or something like that.

If you don't know the names of the variables how would you reference them in the next mission?

I think you are confusing the issue of converting an object to something you can store with finding the variable name. If you use 'str' on an object you might get something back like: <3dObject: #someID g4rb4g3 - can't remember exactly how it looks. There is no way you can find the real object that string is in a next mission.

Let me ask why are you storing this information? Is it to have the battlefield looking like it continues from the previous mission? Then perhaps something like this:

BattlefieldState = [
 [getPos veh_a, getDir veh_a, typeOf veh_a, getDamage typeOf veh_a],
 .....
];
saveVar "BattlefieldState";

Then in your next mission you read the data and recreate it.

Actually in some cases variables were created without me knowing their exact names: ie using format:

for "_i" from 0 to (count array -1) do {
_string = format ["_name = s%1", _i];
call compile _string

The reason for converting to string is from a post under saveVar that says this:

If you try to saveVar a vehicle saved in your variable (SavedVars = [Car1]; saveVar "SavedVars"), then Car1 will not be properly "saved", refering to ObjNull if you try to use it in subsequent missions, even if a vehicle with the same vehicle varname exists. To get around this, save the vehicle's varname as a string (SavedVars = [str(Car1)]) and then when you need it just use call compile to "unstring" the varname (_car = call compile (SavedVars select 0);). --Wolfrug 22:56, 28 January 2009 (CET)

hence why I am doing this. Currently My workaround has been to add another input for the function, so it now reads as this:

0 = [[original arrays here], [arrays as string names]] call Fnc_SaveArray

I tested it quickly using two test missions, and dumping the values of the variables into rpt in each mission - also having the init access these variables to make sure they initialized correctly in the first mission. The conversion works as expected: you can load the objects back in and reference them.

The reason I am doing this, is because I am creating a campaign around controlling a platoon of men, and so 40 or so units need to be checked to see if they are alive, wounded, and another wounded state that is critical condition. I figure I would rather use a constant that contains all the units in it and save it, then do some addition/subtraction of men that are wounded/etc from the constant, and make this the available units in that mission. ie -

AVAILABLEUNITS = ALLUNITS - KILLEDUNITS - WOUNDEDUNITS - CRITICALUNITS

All I am trying to save right now are the arrays, im using eventhandlers to add units into proper arrays (killed/etc) anyway. It would be alot easier then trying to handle a huge array containing all units alive/ damage/ group status etc.

Share this post


Link to post
Share on other sites

I do not know how campaigns are linked together. I assume you create several different missions and then you can link those together. To ensure consistency from one mission to the next you use scripting and saveVars right?

The comment you posted seems like it would only work if the next mission had a unit with the same name in the editor. I believe the comment is to be be understood like you can't save objects. You can only save primitive data like numbers, strings, booleans and arrays.

Unfortunately I doubt you can do what you want without saving each single piece of data in simple primitive types and recreating from that data.

Share this post


Link to post
Share on other sites
I do not know how campaigns are linked together. I assume you create several different missions and then you can link those together. To ensure consistency from one mission to the next you use scripting and saveVars right?

The comment you posted seems like it would only work if the next mission had a unit with the same name in the editor. I believe the comment is to be be understood like you can't save objects. You can only save primitive data like numbers, strings, booleans and arrays.

Unfortunately I doubt you can do what you want without saving each single piece of data in simple primitive types and recreating from that data.

yes, which is why I am creating the units in the editor in all the missions with their respected names. Then I can unstring the array (which should reference the name I gave it in the editor) and remove it from the available units you can assign on a mission if they are killed or wounded or whatever.

However im still deciding how I want to save how many missions have passed in a campaign - ex: wounded units unavailable for 1 mission (healing), units in critical condition require two mission turns to pass (in a worse state, requires more time to heal) then somehow add these units back in when the proper amount of time has elapsed.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×