Jump to content
Sign in to follow this  

Referring to _x unit

Recommended Posts

Hey All,


First let me mention that this isn't a 'please help me get this to work' post (as i've got it working), it's more of a discussion i'm interested in havng as to why something doesn't work.

This is more of an inquisitive theory topic, not a 'quick fix'.


Can someone explain for me why a particular part of this code doesn't work? (because by my logic it should)....

I was following a tutorial I saw on how to make units in a group surrender when the count drops below a certain threshold.
That all works fine!


I then decided to go 'off tutorial' and add an action to each surrendering unit to allow me to take their guns away.

In the On Activation I have the following code....

_x setCaptive true;
_x action ["Surrender", _x];
SurUnit = _x;
_x addAction ["Take Weapons", {removeAllWeapons SurUnit); removeAllActions SurUnit;},nil,6,true,true,"","_target distance _this < 3"];
} forEach units enemygrp;

Of course, the first thing I tried, {removeAllWeapons _X} the game didnt like because it didn't know what _X was..being local I guess.

Before that code you see quoted, I had got it to work initially by typing {removeAllWeapons (_this select 0)} which , (referring to the Bohemia page on AddAction), basically returns the 'object the action is applied to' which in this case is the enemy unit with the action.

I then later thought maybe another way would be to store the local variable _x in a global called SurUnit and then refer to SurUnit in the AddAction script section (see above quoted code).


My logic process being as it loops through each surrendering unit:

it sets it captive,

makes it surrender,

stores that current unit in a global,

and adds action to current unit using global containing that unit's details }

so if the units had names for example .. two surrendering units called Bob and Dave lol .... and we walk through the logic.....


(First surrendering unit found)

_x = Bob so Bob is setcaptive

Bob surrenders

SurUnit variable is now set to Bob

Bob now has an action to remove weapons where the variable on his action (SurUnit) refers to him....


(Now on to second unit...)


_x = now Dave so Dave is setcaptive
Dave surrenders
SurUnit variable is changed to Dave
Dave now has an action to remove weapons where the variable on his action (SurUnit) now also refers to him


(Reached End of Units in Group)


The problem that arose on playback is that the last unit....Dave...(or it could be the first) worked fine..but the other didnt remove the weapons or the action after.....not just that!
but when i tried to remove let's say Bobs gun...his didnt go....but Daves had....so..I can summise that SurUnit didnt change, or the action on the second looped unit somehow still contained previous unit data   so  Dave had an action that was somehow set to the equivalent of saying {removeallweapons Bob} (with SurUnit still saying Bob when it should now be Dave).


But I just don't understand how that can be.


Because if it loops around the second time..if _x is now for all intents and purposes Dave (because _x refers to Dave in every other bit code perfectly fine..setcaptive, surrender etc), why isn't SurUnit now ALSO being set to what _x NOW is...Dave...why is Dave getting RemoveWeapons aimed at Bob from that variable?


I'm guessing my logic is flawed but I just don't see how so if anyone can shed light on why its doing this/what's happening i'd be keen to know just for interests sake.


As I said earlier, I have it working anyway by using _this select 0 instead of a global but i just dont see why the global approach isn't working the way I expect it to.

Share this post

Link to post
Share on other sites

I think you already know the answer. Your script loops through all units in that group, overwriting surUnit with _x. _x however changes within a fraction of a second. So a few minutes later when you want to remove bobs weapon, it will of course refer to the last unit in the loop, because _x was dave and dave is now surUnit.


Also, you don't need surUnit = _x; because as you already mentioned, the object the action is attached to is parsed to the code excecuted once you activate the action.


More information about _x: https://community.bistudio.com/wiki/Magic_Variables

More information about addAction: https://community.bistudio.com/wiki/addAction

and about forEach https://community.bistudio.com/wiki/forEach


Last but not least, it can sometimes help to use diag_log or systemChat str to visualize what a script does and what value a variable stores. You can also use the "watch" field inside the debug console.



Share this post

Link to post
Share on other sites

Thanks for responding R3vo!


I know I don't need SurUnit = _x when (_this select 0) is the working solution, I was just messing around with what I thought would be another route to the same place.

What I failed to think of, and after reading your reply it suddenly dawned on me, the variable setting logic is working as it should..updating and replacing each loop through...but what I had not fully grasped is that when the SurUnit variable is then used in the AddAction code, it doesn't 'bake that unit' into the code when it creates the action.
What I mean by that is  I thought mistakenly with the code:


_x addAction ["Take Weapons", {removeAllWeapons SurUnit); removeAllActions SurUnit;

that SurUnit would be written into the code there and then...as Bob when its Bob and Dave when it's Dave...




The variable is actually called when the Action itself (me taking the weapons) is fired off...by which time as you say...SurUnit will now say the last unit in the loop.


In which case, it's best that the action itself controls the object being referred to when it's fired off....ie (_this select 0)


Got it!

Thanks for helping me understand that. I just coudlnt get in my head why the logic wasn't working!



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