Jump to content
Sign in to follow this  
Muzzleflash

setVariable doesn't set properly

Recommended Posts

Have the following snippet.

_dude is a valid civilian (I can getPos him).

_i is a valid number. I've tried using a literal 10 also.

Both lines come right after each other as below.

Code after the hint is also executed.

Everything is in editor-singleplayer, no locality problems.

Result is "Var is <null>".

_dude setVariable ["index", _i];
hint format ["Var is %1", _dude getVariable "index"];

I can't figure out what's wrong with this. Have used setVariable many times before.

Thanks.

Share this post


Link to post
Share on other sites

Your syntax matches how I use these 2 commands. Is "_dude" a civ that was placed on the map, or is it spawned? I ask because you have it as a local variable, and I'm guessing that your script is not able to reference the dude on the map if it's a map object. You might try renaming him to "dude" and see if that helps.

Share this post


Link to post
Share on other sites

You might also try a quick sleep - like .2 or something - to give it time to process?

Edit:

Also, I seem to recall a similar problem, and to test it, I used:

_indexTest = _dude getVariable "index";
hint format ["Var is %1", _indexTest];

That worked, so I just left it like that.

Share this post


Link to post
Share on other sites
Your syntax matches how I use these 2 commands. Is "_dude" a civ that was placed on the map, or is it spawned? I ask because you have it as a local variable, and I'm guessing that your script is not able to reference the dude on the map if it's a map object. You might try renaming him to "dude" and see if that helps.

The variable is local yes, however, it is set and I can use getPos on him.

You might also try a quick sleep - like .2 or something - to give it time to process?

Edit:

Also, I seem to recall a similar problem, and to test it, I used:

Code:

_indexTest = _dude getVariable "index";
hint format ["Var is %1", _indexTest];

That worked, so I just left it like that.

I tried doing it that way and 1s sleep before getVariable, same results however.

I've also tried adding the 3rd parameter, ",true" to setVariable, didn't work though.

Share this post


Link to post
Share on other sites

Some objects have magic internal variables that are used for BIS purposes. "Index" looks suspiciously like the kind of name that might be used internally. You can do a quick check by changing the name of the variable to "muzIndex" or something similar.

Share this post


Link to post
Share on other sites

Have also used "TheIndex" and some other (with very unlikely chance of collision) I can't remember.

Share this post


Link to post
Share on other sites

If i place this in an initialization area of an object in the editor.

SPR1 = [this] spawn
{
_dude = _this select 0;
_i = 112;

_dude setVariable ["index", _i];
hint format ["Var is %1", _dude getVariable "index"];

};

Results in "Var is 112"

Now if i place this in

SPR1 = [this] spawn
{
_dude = _this select 0;
_j = [];
_i = _j select 0;
_dude setVariable ["index", _i];
hint format ["Var is %1", _dude getVariable "index"];

};

I get "Var is <null>"

So it could be to do with where and how you are assigning _i it's value.

This might help if it is an array issue.

http://community.bistudio.com/wiki/Array

Good Luck

Blake

Share this post


Link to post
Share on other sites

Muzzleflash said he'd tried using a literal value so I don't think it's anything to do with the scope of the variable. I think at this point you need to look at a bit more of the surrounding code - there's nothing wrong that I can see about the method you're using so all I can assume is there is something odd about the object you are setting the variable into. Can you post a bit more context ?

Share this post


Link to post
Share on other sites
Muzzleflash said he'd tried using a literal value so I don't think it's anything to do with the scope of the variable. I think at this point you need to look at a bit more of the surrounding code - there's nothing wrong that I can see about the method you're using so all I can assume is there is something odd about the object you are setting the variable into. Can you post a bit more context ?

As SHK points out;

_j = [];
_i = _j select 0;

Makes no sense?

The problem is clearly

_j = [];

_i = _j select 0;

_i will be NULL... accessing array index out of bound, as there is no index 0 inside the array :)

Share this post


Link to post
Share on other sites

SB- if you read the whole thread you'll see that SHK was commenting on some invented code by BlakeAce which as nothing to do with MuzzleFlash's original problem. So, until MuzzleFlash posts a bit more context we're still in the dark. :)

Share this post


Link to post
Share on other sites
SB- if you read the whole thread you'll see that SHK was commenting on some invented code by BlakeAce which as nothing to do with MuzzleFlash's original problem. So, until MuzzleFlash posts a bit more context we're still in the dark. :)

Woops :)

Share this post


Link to post
Share on other sites

This is the entire function:

EBP_AH_Populate = {    
   private ["_posdir","_pos","_dir","_dude","_data","_type","_stance"];
   _data = EBP_CurrentData select 0;
   if (count _data < 1) exitWith {};
   _type = typeOf player; //"RU_Citizen1";
   for "_i" from 0 to (count _data - 1) do {
       _posdir = _data select _i;
       _pos = _posdir select 0;
       //_pos set [2, (_pos select 2)+0.7];
       _pos = [EBP_CurrentBuilding, _pos] call EBP_WorldPos;
       _dir = _posdir select 1;
       _stance = _posdir select 2;
       _dude = _type createVehicle (_pos);
       removeAllItems _dude;
       removeAllWeapons _dude;
       _dude setDir _dir;
       _dude setPosATL _pos;
       if (_stance == "MIDDLE") then {
           _dude switchmove "aidlpknlmstpslowwrfldnon_idlesteady02";
       };
       if (_stance == "DOWN") then {
           _dude switchMove "aidlppnemstpsraswrfldnon0s";
       };

       //_dude disableAI "MOVE";
[b]        _dude allowDamage false;
       _dude setVariable ["index", _i];
       hint format ["Var is %1", _dude getVariable "index"];[/b]
       _dude addAction ["Remove position", "EBP\ActionHandler.sqf", "Remove_Point"];
       EBP_CurPopulation set [count EBP_CurPopulation, _dude];
   };
   EBP_CurBldPopulated = true;
};

- I have tried using getPos and hinting the result in there and _dude exists.

- I have tried a literal 10 instead of _i and other names, "TheIndex" and other non-collision likely.

- I'm confident I haven't written anything wrong, eg. lndex.

- This very scriptfile also functions as an addAction handler and is thus called from the player's action menu.

- Code after the set/get-Variable is run.

- This "execution environment" allows sleep.

This function is called by later in the same script file using:

switch (toUpper _param) do {
   case "SELNEARBLD": {call EBP_AH_Select};
   case "LOAD": {call EBP_AH_Load};
   case "CANCEL": {call EBP_AH_Cancel};
   case "ADDPOS": {call EBP_AH_AddPos};
   case "CLIPBOARD": {call EBP_AH_ToClipboard};
   case "NO_POS": {call EBP_AH_NumberPos};
[b]    case "POPULATE": {call EBP_AH_Populate;};[/b]
   case "UNPOPULATE": {call EBP_AH_Unpopulate; hint "Unpopulated";};
   case "REMOVE_POINT": {call EBP_AH_RemovePoint};
   default {hint format ["Unknown action param: %1", _param];};
};

Hope that can help you help me :)

Edited by Muzzleflash

Share this post


Link to post
Share on other sites

Wacky idea, but maybe put parens around _dude getVariable "index", so it looks like:

hint format ["Var is %1", (_dude getVariable "index")];

I sometimes get odd results without explicit parens.

Edit: oh, and what does the hint format show? Anything at all?

Never mind, found it in the first post. :)

Share this post


Link to post
Share on other sites

dude1 = group player createunit ["RU_Citizen1", getpos player, [], 0, "FORM"];
dude1 setvariable ["myvar",10];
dude2 = "RU_Citizen1" createvehicle (getpos player);
dude2 setvariable ["myvar",10];
hint format ["dude1 %1\ndude2 %2",dude1 getVariable "myvar",dude2 getVariable "myvar"];

Returns:

dude1 10

dude2 <null>

Share this post


Link to post
Share on other sites

Thank SHK I got it working now using createUnit. :)

Can verify your results.

Strange that createVehicle lets setVariable work for vehicles (actual vehicles) and not units.

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  

×