Jump to content
Sign in to follow this  
tryteyker

lbData -> String to Object

Recommended Posts

Hi,

I'm stuck trying to convert a string to an object. What I'm doing right now is basically store a range of values in a listbox, and pass the objects name onto lbSetData. When trying to access it with lbData, however, it returns a string. This makes the object unusable by distance checks and other functions.

How I define the data:

   if (_artillery == 1 && {_side == _playerside} && {_count < 4}) then { // All 3 conditions have to be true, but cond 2 & 3 are only evaluated if the preceding one is true. Lazy Evaluation.
       _index = lbAdd [_listBox, _displayName]; // Add display name to ListBox entry. Goes from 0 to 3. (0-index based)
       lbSetPicture [_listBox, _index, _picture]; // Adds the previously defined picture to the listbox entry. Again, 0 to 3. It will show up to the left of the text.
       lbSetData [_listBox, _index, _x]; // This command is used for later reference, it has no immediate effect on the listbox.
       _count = _count + 1;
   };

This is part of a "forEach vehicles" loop, running for all vehicles. ListBox has a standard value of IDC 1500, and index is from 0 to 3. It all works neat until the main function of this whole thing gets executed:

_artillery = [_this,0,missionNamespace getVariable "ArtilleryUsed",[objNull]] call BIS_fnc_param; // Artillery used. (OBJECT, UNIT)
_amrounds = [_this,1,missionNameSpace getVariable "RoundsUsed",[0]] call BIS_fnc_param; // Amount of rounds. (NUMBER, INTEGER)
_delay = [_this,2,missionNamespace getVariable "SetDelay",[0]] call BIS_fnc_param; // Delay in seconds between shots. (NUMBER, FLOAT)
_round = [_this,3,missionNamespace getVariable "ShellUsed",[""]] call BIS_fnc_param; // Type of round used (STRING, CLASSNAMED)
_target = [_this,4,missionNamespace getVariable "MarkerPositionUsed",[[0,0,0]]] call BIS_fnc_param; // Target. (POSITION, MARKER)

Now they are defined very different. Everything besides _target and _artillery uses listbox selection values. _target is working as it gets a position value. _artillery is not as it gets a string instead of an object name.

As to how I access the data and define ArtilleryUsed:

switch (lbCurSel _listBox) do {
   case 0:
   {
   _textValue = lbText [_listBox,0];
   ctrlSetText [_display,_textValue];
   missionNamespace setVariable ["ArtilleryUsed",lbData [1500,0]];
   };
// Continues downwards

The first 2 things in the case only add text to a seperate RscText. Third line defines ArtilleryUsed. This gets done repeatedly until the dialog closes. Again, this all works, HOWEVER I need to trim away the " " from the string, effectively making it an object. compile does NOT work as it makes it Code, which is not what I need. Array doesn't cut it either. Any ideas? please :(

Share this post


Link to post
Share on other sites

This is the better way to do it:

_obj = missionNamespace getVariable [_objString, objNull];

Or you can do it the "more popular" (but worse) way:

_obj = call compile _objString;

Share this post


Link to post
Share on other sites

The problem isn't how he gets the variable - the main function works great, and he defines everything aswell. The only problem is that lbData passes a STRING to the function, but I need an OBJECT. I was thinking of getting a way to trim away the quotes but there seems to be no way of doing that, unfortunately.

Share this post


Link to post
Share on other sites
_objString = lbData [_lb, _index]; //string
_obj = missionNamespace getVariable [_objString, objNull];
player sideChat format ["%1", typeName _obj]; //should print OBJECT? 

Edited by Iceman77

Share this post


Link to post
Share on other sites

Still no luck. My file structure is a bit screwed up in the sense that the script that defines the "ArtilleryUsed" missionNamespace var is practically a dead-end. The variable gets updated frequently, but only referenced in the main script, which, in turn, gets called through 2 other scripts completely independent of the script defining the variable initially. I've tried implementing Iceman's (and Naughts) thingy but it's obviously not working considering I can't reference local vars elsewhere. I also skipped sleep this night which doesn't make things any better.

OKAY, I GOT IT. The way I did, to preserve compatibility with outside remote calls (this function is supposed to be useable without GUI as well), I've added a special if statement that triggers when the var _artillery is a string (as it is when using lbData). I've then added Iceman's code and redefined the variable with the updated code. It's now an object. Yay.

if (typeName _artillery == "STRING") then {
   _objString = lbData [1500, lbCurSel 1500]; //string
   _obj = missionNamespace getVariable [_objString, objNull];
   _artillery = missionNamespace setVariable ["ArtilleryUsed",_obj];
};

Nevermind.. It returns objNull as index isn't saved properly (dialog's already closed as he tries to access lbCurSel).

EDIT

Back to step -1. I can't use Iceman's method in the main function because I can't reference data values anymore once the dialog's closed. And predefined data values are actually in the artillery string. Testing it out with format, it's void. There's nothing in it. But there's still a selection, it's just not selected.

EDIT2

RIGHT, I FIXED IT. I ditched all the lbSetData bullshit and used a global variable instead. MissionNameSpace references still exist for now because it's ok and functional, and also useful, and the global variable works, and the whole object thingy thing actually references correctly now. Time wasted without any sleep: 20 hours. Goddamn lbData. :(

Edited by tryteyker

Share this post


Link to post
Share on other sites

Nevermind.. It returns objNull as index isn't saved properly (dialog's already closed as he tries to access lbCurSel).

You need to store the current selection into the a variable before the dialog is closed. That way you can still reference what WAS the selection, even if the dialog is closed. I'm always needing to do this.

Edited by Iceman77

Share this post


Link to post
Share on other sites

Yeah probably. I've got a global variable workaround for it now. There's other issues ahead.

Share this post


Link to post
Share on other sites
The problem isn't how he gets the variable - the main function works great, and he defines everything aswell. The only problem is that lbData passes a STRING to the function, but I need an OBJECT. I was thinking of getting a way to trim away the quotes but there seems to be no way of doing that, unfortunately.

Ya, that's what my function does...

This is the code that you should have used:

if (_artillery == 1 && {_side == _playerside} && {_count < 4}) then { // All 3 conditions have to be true, but cond 2 & 3 are only evaluated if the preceding one is true. Lazy Evaluation.
_index = lbAdd [_listBox, _displayName]; // Add display name to ListBox entry. Goes from 0 to 3. (0-index based)
lbSetPicture [_listBox, _index, _picture]; // Adds the previously defined picture to the listbox entry. Again, 0 to 3. It will show up to the left of the text.
if ((vehicleVarName _x) == "") then {
	if (isNil "veh_ref_counter") then {veh_ref_counter = 0};
	veh_ref_counter = veh_ref_counter + 1;
	private ["_vehVarName"];
	_vehVarName = format["veh_ref_%1", veh_ref_counter];
	_x setVehicleVarName _vehVarName;
	missionNamespace setVariable [_vehVarName, _x];
};
lbSetData [_listBox, _index, (vehicleVarName _x)]; // This command is used for later reference, it has no immediate effect on the listbox.
_count = _count + 1;
};

switch (lbCurSel _listBox) do {
case 0: {
	_textValue = lbText [_listBox, 0];
	ctrlSetText [_display, _textValue];
	missionNamespace setVariable ["ArtilleryUsed", missionNamespace getVariable [(lbData [1500,0]), objNull]];
};
// Continues downwards

*** EDIT:

I understand that the listbox was a bit too complicated for what you were looking for, but I posted this up for future reference and/or anyone else who wants a solution.

  • Like 2

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  

×