Jump to content
rileyrazor

Change Player Side if They Have a Weapon

Recommended Posts

Hello all. 

I am somewhat new to scripting and I have attempted to write a section of code that will change the player's side to civilian if they do not have a weapon in the weapon slots. Weapons in bags are okay.

The hint pops up fine but the side of my player never changes. 

Any help would be appreciated.

This code is in my initPlayerLocal.sqf

 

if (hasInterface) then {
	["weapon", {
		params ["_player", "_weapon"];
	if (primaryWeapon _player == "" && handgunWeapon _player == "") then {
		hint parseText "<t size='2.0'>You do not have a weapon.</t>";
		if (playerSide isEualTo west) then {[player] joinSilent _CivGroup} else {};}  
	else {
		hint parseText "<t size='2.0'>You have a weapon.</t>";
		if (playerSide isEualTo civilian) then {[player] joinSilent _BluGroup} else {};}
	}] call CBA_fnc_addPlayerEventHandler;
};

 

Share this post


Link to post
Share on other sites

@rileyrazor, seems you made a typo -- should be written isEqualTo. Also you use local variables _CivGroup and _BluGroup, that don't exist in event handler's scope.

Also you don't have to use else-branch of if-statement -- you can just write

if (playerSide isEqualTo west) then { [player] joinSilent _CivGroup; };

 

  • Like 2

Share this post


Link to post
Share on other sites

Like Schatten suggested, the problem should be that you use variables that are not existent in the scope of the event handler. Most probably you should make the variables global, or in case you intend to use the handler in MP public either with publicVariable command or with setVariable command with the last argument set to true. An example with the latter could be

// Get the group of the player (consider it to be the group you want)
private _bluGrp = group player;

// Set a variable with the group in mission namespace and broadcast it to be public
missionNamespace setVariable["blueGroup", _bluGrp, true];

Then, you could get the group inside the event handler like

// Inside the event handler
private _bluGrp = missionNamespace getVariable ["blueGroup", grpNull]; // Get the group

// Check if the group exists (you can skip this test if you are sure the group exists)
if(isNull _bluGrp) exitWith {
	systemChat "Group is empty";
}

// Make the player join the group
player joinSilent _bluGrp;

// Rebroadcast the group to be available to other clients too
missionNamespace setVariable["blueGroup", _bluGrp, true];

The same approach would work for the civilian group but you should either create it or get it from a civilian or whatever suits your needs best (I assume you already have that since it is present in the code you submitted).

 

Please let us know if that works for you or if you would require further help with it.

  • Like 1

Share this post


Link to post
Share on other sites

If your aim is no weapon in slots, so player becomes civilian when no visible weapon:

in initPlayerLocal.sqf:
 

player  spawn {
  params ["_plyr"];
  while {true} do {
    waitUntil {sleep 2; primaryWeapon _plyr + secondaryWeapon _plyr + handgunWeapon _plyr isEqualTo ""};
    _plyr setCaptive TRUE;
    waitUntil {sleep 2; !(primaryWeapon _plyr + secondaryWeapon _plyr + handgunWeapon _plyr isEqualTo "")};
    _plyr setCaptive FALSE;
  };
};

No need to mess with groups, or CBA or broadcast variables. Works in MP. Feel free to sleep shorter, the conditions are light.

  • Like 3

Share this post


Link to post
Share on other sites
2 hours ago, pierremgi said:

If your aim is no weapon in slots, so player becomes civilian when no visible weapon:

in initPlayerLocal.sqf:
 


player  spawn {
  params ["_plyr"];
  while {true} do {
    waitUntil {sleep 2; primaryWeapon _plyr + secondaryWeapon _plyr + handgunWeapon _plyr isEqualTo ""};
    _plyr setCaptive TRUE;
    waitUntil {sleep 2; !(primaryWeapon _plyr + secondaryWeapon _plyr + handgunWeapon _plyr isEqualTo "")};
    _plyr setCaptive FALSE;
  };
};

No need to mess with groups, or CBA or broadcast variables. Works in MP. Feel free to sleep shorter, the conditions are light.

I've pasted this directly into my initPlayerLocal.sqf but when I drop all weapons out of my players inventory, my player is still blufor and gets shot at by enemies. I'm getting no errors but the code doesn't seem to work for some reason.

Share this post


Link to post
Share on other sites
6 hours ago, ZaellixA said:

Like Schatten suggested, the problem should be that you use variables that are not existent in the scope of the event handler. Most probably you should make the variables global, or in case you intend to use the handler in MP public either with publicVariable command or with setVariable command with the last argument set to true. An example with the latter could be

 

Thank you for the help. I'm fairly new to scripting, so I can understand each line, but I'm still figuring out how to implement it into my code. Kind of like understanding individual words, but not sentences. I'll get back to you when I've got everything together.

 

EDIT: I now understand more about why the variables need to be global variables.

I have one blufor unit named _BluGuy and the group variable is set to _BluGroup. My civilian is named _CivGuy and the group variable is set to _CivGroup.
How do I go about setting those as variables in the mission space so the script can access it? Sorry I've you've already answered how and I missed it.

 

Edited by rileyrazor
Update
  • Like 1

Share this post


Link to post
Share on other sites

Well, wherever you set the guys names and group names all you have to do is use setVariable. I will provide an example where I magically get those two units and I will assume that you have a way to get access to them. So, the example would look like

private _bluGuy = theBlueGuy; // The name on the right is the name you will give the unit in the editor
missionNamespace setVariable["bluGrp", group _bluGuy, true]; // Set the group of the bluefor guy as a public variable

privaet _civGuy = theCivGuy; // The name on the right is the name you will give the unit in the editor
missionNamespace setVariable["civGrp", group _civGuy, true]; // Set the group of the civilian as a public variable

Alternatively, you could skip private variables and go directly for the setVariable command like

missionNamespace setVariable["bluGrp", group theBlueGuy, true]; // Set the group of the bluefor guy as a public variable
missionNamespace setVariable["civGrp", group tehCivGuy, true]; // Set the group of the civilian as a public variable

Hope this helps somehow.

Share this post


Link to post
Share on other sites
3 hours ago, rileyrazor said:

I've pasted this directly into my initPlayerLocal.sqf but when I drop all weapons out of my players inventory, my player is still blufor and gets shot at by enemies. I'm getting no errors but the code doesn't seem to work for some reason.

Try again. I tested it several times.

setCaptive is AL EG so, from initPlayerLocal.sqf , there is no reason to fail, even in MP. As said, you can dim down the sleep commands (both instances) from 2 sec. to 0.5 or even 0.2 (example)

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for all your help! Here is the final code that worked if anyone else coming across this thread needs it.
 

private _bluGuy = bluGuy;
missionNamespace setVariable["BluGroup", group _bluGuy, true];

private _civGuy = civGuy;
missionNamespace setVariable["CivGroup", group _civGuy, true];

if (hasInterface) then {
	["weapon", {
		params ["_player", "_weapon"];
	if (primaryWeapon _player == "" && handgunWeapon _player == "") then {
		hint parseText "<t size='2.0'>You do not have a weapon.</t>";
		[player] joinSilent CivGroup;
	}  
	else {
		hint parseText "<t size='2.0'>You have a weapon.</t>";
		[player] joinSilent BluGroup;
	};
	}] call CBA_fnc_addPlayerEventHandler;
};

 

  • Like 2
  • Confused 1

Share this post


Link to post
Share on other sites

You don't really need to assign the global variable to a local in order to pass it to the setVariable command. You can do it like in the second example in my post above pierremgi's.

 

I am not familiar with CBA's event handlers so I am not sure I understand correctly when and/or how often this event handler is triggered. I would suggest you try out pierremgi's solution too because if the event handler is running often then you won't really get any benefit from creating an event handler instead of using a sleep command in your code. I understand that for some reason this solution didn't work out for you. If you would like to test it a wee bit more let us know to help you out finding the root of the error.

 

Nevertheless, since this works for you it's fine. Also, I hope you know better than me how CBA's event handlers work to have made this choice for your implementation.

 

Have fun, stay healthy and ArmA a lot.

Share this post


Link to post
Share on other sites

 

11 hours ago, rileyrazor said:

....Here is the final code that worked if anyone else coming across this thread needs it.

 

CBA useless, messing with sides instead of setCaptive, not optimized code with variables and commands...  I hope future readers will read the full thread!

 

  • Like 1

Share this post


Link to post
Share on other sites

there is also limitation of Arma, where when you dismount from vehicle, it will automatically equip weapon, making you "armed" for enemy

  • Like 1

Share this post


Link to post
Share on other sites

@M1ke_SK   If I'm right, that occurs when you weapon is at shoulder (back), not in backpack. So here, that doesn't matter (not tested everything).

 

On the other hand, setCaptive has a true bug:

when you jump into driver's seat as captive, your vehicle is civilian, as you are.

But when you dismount (from driver), the vehicle falls into your true side IF this vehicle has the same side (native here, example HUNTER is native blue, IFRIT is native red) as you,  and, i insist: IF you were the driver!

Then, if this particular case (same side (actual for player/native for vehicle), and driver when you dismount), you stay captive but the vehicle is attacked by enemies (as you weren't captive).

These shots against a recent emptied vehicle is fine, but Arma's engine fails for the logic on a captive driver getting out.

Not found a workaround for that.

 

finally, here is a workaround:

 

[] spawn {
  while {true} do {
    waitUntil {sleep 2; primaryWeapon player + secondaryWeapon player + handgunWeapon player isEqualTo ""};
    player setCaptive TRUE;
    player setVariable ["EHCaptive",player addEventHandler ["GetOutMan", {
      params ["_plyr", "_role", "_veh"];
      if (_role == "driver") then {
        unassignVehicle _plyr;
      };
    }]];
    waitUntil {sleep 2; !(primaryWeapon player + secondaryWeapon player + handgunWeapon player isEqualTo "")};
    player setCaptive FALSE;
    player removeEventHandler ["GetOutMan",player getVariable ["EHCaptive",999]];
  };
};

 

  • Like 4

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

×