Jump to content
Guest

Having trouble with _this

Recommended Posts

Guest

Hey!

 

So I've been playing around with _this and this for a while, and sometimes it works, sometimes it doesn't, so I'd like to know what is wrong with this piece of code. It worked previously, I changed something, and now it no longer works. I am running on a dedicated server.

 

 

///This is in serverInit.sqf

[player_1, ["Check Resources","scripts\CheckResources.sqf", nil, 1.5, true, true, "", "true", distanceFromAction, false, "", ""]] remoteExec ["addAction", 0, true]; //It creates the action as intended, and runs the script when I use the action

////This is in CheckResources.sqf
me = this select 0;
other = this select 1;

_var1 = other getVariable "unarmed";
_var2 = other getVariable "unmasked";
_var3 = other getVariable "naked";
_var4 = captive other;
_player_group_size = count units other;

format ["%1\n%2\n%3\%4\n%5", _var1, _var2, _var3, _var4, _player_group_size] remoteExec ["hint"];

I tried using _this instead of this, but it seems to not work. I thought I understood this system, but it seems I do not, since I changed something (not sure what) that felt completely irrelevant to this script, and now it no longer works.

Share this post


Link to post
Share on other sites

The rules are here:

https://community.bistudio.com/wiki/Magic_Variables

 

this works only in init field of objects in editor (and trigger condition or waypoints... roughly)

 

When you are using an event (so automatic) script, there are some embedded parameters which can be hold by _this or params [...]; So, _this can be an array of params.

Same for usage inside event handlers.

Example:

In player's init field:
this addEventHandler ["AnimChanged", { params ["_unit", "_anim"]; hint str _this }];

Here this refers to the player's unit and you apply the EH to him.

_this refers to params so, you'll hint an array:  [here _unit is just player, so new animation of player]

 

For your codes, as parameters, example:

[allUnits, allMapMarkers] spawn {

  params ["_units","_mkrs"];

// is same as

  private _units = _this select 0;

  private _mkrs = _this select 1;

....
};

 

  • Like 4

Share this post


Link to post
Share on other sites
Quote

I've been playing around with _this and this for a while, and sometimes it works, sometimes it doesn't...

 

this spawn {
	waitUntil {sleep 1;
		if alive _this then {systemChat str _this};
		!alive _this
	}
}

[this, name this, group this] call {	params ["_unit", "_name", "_grp"];
	_this spawn {
		waitUntil {sleep 1;
			if alive (_this #0) then {systemChat str _this};
			!alive (_this #0)
		}
	}
}

_this is the argument(s) passed to a script. A magic variable from which other variables may be derived.
THIS is just another way to refer to the unit, object, vehicle into which you paste script. If you name a unit "bob" then THIS is bob in bob's init field.

Have fun!

 

  • Like 3

Share this post


Link to post
Share on other sites
On 9/10/2021 at 7:03 PM, Lauri Hirn said:

Hey!

 

So I've been playing around with _this and this for a while, and sometimes it works, sometimes it doesn't, so I'd like to know what is wrong with this piece of code. It worked previously, I changed something, and now it no longer works. I am running on a dedicated server.

 

 


///This is in serverInit.sqf

[player_1, ["Check Resources","scripts\CheckResources.sqf", nil, 1.5, true, true, "", "true", distanceFromAction, false, "", ""]] remoteExec ["addAction", 0, true]; //It creates the action as intended, and runs the script when I use the action

////This is in CheckResources.sqf
me = this select 0;
other = this select 1;

_var1 = other getVariable "unarmed";
_var2 = other getVariable "unmasked";
_var3 = other getVariable "naked";
_var4 = captive other;
_player_group_size = count units other;

format ["%1\n%2\n%3\%4\n%5", _var1, _var2, _var3, _var4, _player_group_size] remoteExec ["hint"];

I tried using _this instead of this, but it seems to not work. I thought I understood this system, but it seems I do not, since I changed something (not sure what) that felt completely irrelevant to this script, and now it no longer works.

Delete

me = this select 0;
other = this select 1;

and replace these variables in the script by original agruments (i.e. replace me with _target and other with _caller).

Also count units will not work with other, becouse other in your script refers to an object, not a group.

count units group _target will count group units of the object which the action is assigned to.
count units group _caller will count group units of the player that activated the action.

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

×