Jump to content
Sign in to follow this  
Mongoose_84

run a script on a player the moment he respawns

Recommended Posts

i am looking for some way to do exactly what you (or at least i) would normally expect the 'init'-eventHandler to do (but for some reason doesn't do). that is, i want to run a script on a player everytime he respawns in MP. i'd be glad, if someone could help me with this...

Share this post


Link to post
Share on other sites

I used to do this in OFP by placing a trigger and making it repeatable.

I would then put <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> alive player in the condition field

You can put commands in the activation field for when they spawn/start the game and commands in the deactivation for when they die (although OnPlayerKilled is much better/reliable for that).

This trigger executes the activation field once when the game starts and once everytime the player respawns.

Keep in mind instant respawning might affect it. I usually use a 5+ sec delay in my missions anyways to allow the trigger to deactivate before reactivating again.

Share this post


Link to post
Share on other sites
I used to do this in OFP by placing a trigger and making it repeatable.

I would then put <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> alive player

in the condition field

You can put commands in the activation field for when they spawn/start the game and commands in the deactivation for when they die (although OnPlayerKilled is much better/reliable for that).

This trigger executes the activation field once when the game starts and once everytime the player respawns.

Keep in mind instant respawning might affect it. I usually use a 5+ sec delay in my missions anyways to allow the trigger to deactivate before reactivating again.

thanks for the quick answer!

i have almost no experience with MP-scripting so far and don't know what possible problems i have to be aware of compared to SP-scripting, but can i for example use "player" this way and it'll work for every player in the game?

i also have yet to find out, what this server/client locality is all about and what i have to do differently about it... (i'm probably gonna despair a few times crazy_o.gif )

Share this post


Link to post
Share on other sites

i'm just doing it with an eventHandler 'killed' triggering a respawn script that uses {waitUntil alive player;} at the beginning. i can only hope, that it'll actually work on every player in a game and not just now, while i'm testing on a single server and client.

Share this post


Link to post
Share on other sites

If you use the trigger as suggested, it will run on every human player each time they respawn. Since you're using "Player" in the condition, that also makes it local to each human player. In other words, you don't need to worry about further localizing the script for each human player.

Using a killed EH is fine, but the trigger works nearly flawlessly.

Share this post


Link to post
Share on other sites

thx for that clarification - it's a bit of a relief being told that smile_o.gif

i'm almost done with my custom respawn-script that combines group respawning with standart respawning, but i need some help now, since though i do have some experience with programming in general, i have only just begun scripting for ArmA and i don't seem to quite understand the differences in sqs/sqf, exec/execVM and all that stuff. and the biki is no real help concerning this.

it's run on the player, when he's alive again

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_members = units group _this;

_distNewUnit = -1;

_posIVNew = -1;

group _this selectLeader _this;

_x = -1;

#findUnit

_x = _x + 1;

if (_x == count _members) then {goto "respawn"};

_unitX = _members select _x;

if (NOT alive _unitX OR _unitX == _this) then {goto "findUnit"};

_distX = (_this distance _unitX);

if (_distNewUnit != -1 AND _distX > _distNewUnit) then {goto "findUnit"};

_vehicleX = vehicle _unitX;

if (_vehicleX != _unitX) then {goto "isInVehicle"};

_newUnit = _unitX;

_distNewUnit = _distX;

_posIVNew = -1;

goto "findUnit";

#isInVehicle

if (commander _VehicleX == _unitX) then {_posIVX = 0; goto "isValid"};

if (driver _VehicleX == _unitX) then {_posIVX = 1; goto "isValid"};

if (gunner _VehicleX == _unitX) then {_posIVX = 2; goto "isValid"};

*here i want to check, if the unit is in cargo (just don't know how...) and otherwise don't use this unit, because i can't find out it's position (because it may have some turret position or i dunno what...)*

goto "findUnit";

#isValid

if (_distX == _distNewUnit AND _posIVNew < _posIVX) then {goto "findUnit"};

_newUnit = _unitX;

_distNewUnit = _distX;

_posIVNew = _posIVX;

_vehicleNew = _vehicleX;

goto "findUnit";

#respawn

if (_distNewUnit == -1) then {goto "createNewUnit"};

_newWeapons = weapons _newUnit;

_newMagazines = magazines _newUnit;

_weaponsCount = count _newWeapons;

_magazinesCount = count _newMagazines;

for [{_x = 0}, {_x < _magazinesCount}, {_x= _x + 1}] do {_this addMagazine (_newMagazines select _x)};

for [{_x = 0}, {_x < _weaponsCount}, {_x= _x + 1}] do {_this addWeapon (_newWeapons select _x)};

if (posIVNew != -1) then {goto "moveInVehicle"};

_newPosition = getPos _newUnit;

_newDirection = getDir _newUnit;

deleteVehicle _newUnit;

_this setPos _newPosition;

_this setDir _newDirection;

sleep 0.1;

_this selectWeapon primaryWeapon _this;

exit;

#moveInVehicle

_newUnit setDammage 1;

sleep 0.1;

deleteVehicle _newUnit;

switch (_posIVNew) do {case 0: _this moveInCommander _vehicleNew; case 1: _this moveInDriver _vehicleNew; case 2: _this moveInGunner _vehicleNew; case 3: _this moveInCargo _vehicleNew};

exit;

#createNewUnit

{_this addMagazine '8Rnd_9x18_Makarov'} forEach [1,2,3,4,5,6,7,8];

_this addWeapon 'Makarov';

sleep 0.1;

_this selectWeapon 'Makarov';

first, i ran it with "exec" and it worked fine, but when i got to the point, where i needed to delete a unit from a vehicle (which unfortunately can't seem to be done only with deleteVehicle), i also wanted to use "sleep" to get it to work. but the way i understand it, i can't use sleep, when i run a script with "exec", so now i want to use "execVM" instead, but that causes a CTD, everytime it's run on respawn - saying "Preprocessor failed on file ...respawnPlayer.sqs - error 7".

Share this post


Link to post
Share on other sites

In order to make a script run individually for the respawned player, I use this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Cond: alive player

Activation: [] exec "script.sqs"

In my knowledge the brackets make the event individual to the unit concerned.

Share this post


Link to post
Share on other sites

Mongoose_84, your "Preprocessor failed on file ...respawnPlayer.sqs - error 7" is caused by the fact that you use .sqs syntax in something the engine expects to be .sqf (using execVM). I suppose you simply changed the format of the file from .sqs to .sqf, but (un)fortunately, all your "gotos" and such have to go...

Share this post


Link to post
Share on other sites

that's kinda funny, cause when i started making this script, i did it almost like i was programming in C. and when nothing seemed to work, i thought i would have to use this style, because i had seen it in some opf-scripts before... (i made this thread then)

is there some entry in the biki perhaps, that would be a good introduction for this C-like syntax? and is it connected to execVM and .sqf, while the old syntax is connected to exec and .sqs? or do i still not get it?...

Share this post


Link to post
Share on other sites
CrashDome is working on it. He says here to expect it by... tomorrow! I'm almost tempted to insert a banana here...

don't bother, i'm gonna do it for you wink_o.gif

yay.gif

and thanks for the infos!

Share this post


Link to post
Share on other sites

ok.. i tried to turn this code (back) into C-style syntax:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_members = units group _this;

_newUnit = ObjNull;

_distNewUnit = -1;

_posIVNew = -1;

group _this selectLeader _this;

for [{_x = 0}, {_x < count _members}, {_x = _x + 1}] do

{

_unitX = _members select _x;

if (alive _unitX AND _unitX != _this) then

{

_distX = (_this distance _unitX);

if (_distNewUnit == -1 OR _distX < _distNewUnit) then

{

_vehicleX = vehicle _unitX;

if (_vehicleX == _unitX) then

{

_newUnit = _unitX;

_distNewUnit = _distX;

_posIVNew = -1;

}

else

{

if (commander _vehicleX == _unitX) then {_posIVX = 0}

else if (driver _vehicleX == _unitX) then {_posIVX = 1}

else if (gunner _vehicleX == _unitX) then {_posIVX = 2}

else if (_unitX in crew _vehicleX) then {_posIVX = 3}

else {_posIVX = -1};

if (_posIVX != -1 AND (_distX < _distNewUnit OR _posIVX < _posIVNew))

{

_newUnit = _unitX;

_distNewUnit = _distX;

_posIVNew = _posIVX;

_vehicleNew = _vehicleX;

};

};

};

};

};

if (_distNewUnit != -1) then

{

_newWeapons = weapons _newUnit;

_newMagazines = magazines _newUnit;

_weaponsCount = count _newWeapons;

_magazinesCount = count _newMagazines;

for [{_x = 0}, {_x < _magazinesCount}, {_x= _x + 1}] do {_this addMagazine (_newMagazines select _x)};

for [{_x = 0}, {_x < _weaponsCount}, {_x= _x + 1}] do {_this addWeapon (_newWeapons select _x)};

if (posIVNew == -1) then

{

_newPosition = getPos _newUnit;

_newDirection = getDir _newUnit;

deleteVehicle _newUnit;

_this setPos _newPosition;

_this setDir _newDirection;

sleep 0.1;

_this selectWeapon primaryWeapon _this;

}

else

{

_newUnit setDammage 1;

sleep 0.1;

deleteVehicle _newUnit;

switch (_posIVNew) do

{

case 0: _this moveInCommander _vehicleNew;

case 1: _this moveInDriver _vehicleNew;

case 2: _this moveInGunner _vehicleNew;

case 3: _this moveInCargo _vehicleNew;

};

};

}

else

{

{_this addMagazine '8Rnd_9x18_Makarov'} forEach [1,2,3,4,5,6,7,8];

_this addWeapon 'Makarov';

sleep 0.1;

_this selectWeapon 'Makarov';

};

but now it says something about "else" being a string (expected dunnowhat...). can someone please tell me, what is still wrong with it?

Share this post


Link to post
Share on other sites

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> else

{

if (commander _vehicleX == _unitX) then {_posIVX = 0}

else if (driver _vehicleX == _unitX) then {_posIVX = 1}

else if (gunner _vehicleX == _unitX) then {_posIVX = 2}

else if (_unitX in crew _vehicleX) then {_posIVX = 3}

else {_posIVX = -1};

if (_posIVX != -1 AND (_distX < _distNewUnit OR _posIVX < _posIVNew))

{

I believe you cannot use "else if" which is not a capability.

Share this post


Link to post
Share on other sites
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> else

{

if (commander _vehicleX == _unitX) then {_posIVX = 0}

else if (driver _vehicleX == _unitX) then {_posIVX = 1}

else if (gunner _vehicleX == _unitX) then {_posIVX = 2}

else if (_unitX in crew _vehicleX) then {_posIVX = 3}

else {_posIVX = -1};

if (_posIVX != -1 AND (_distX < _distNewUnit OR _posIVX < _posIVNew))

{

I believe you cannot use "else if" which is not a capability.

it worked in C... but then again this isn't C, after all. probably needs to be put in further brackets here (else {if {...}}).still, i think the error message was linked to an "else" other than those "else if"s. i will check again tomorrow, thx!

edit: yep, i used extra brackets to avoid the use of "else if", but it still produces error messages...

edit2: i experimented and removed bits of code one by one to find the error and it turned out to be a missing "then" inside of another then/else {...} - to which the error message referred. so you were right, the "else if" should've caused the same error (i just had already removed them by then).

Share this post


Link to post
Share on other sites

another question: can i somehow "eject" a dead body from a vehicle without replacing it with some other unit?

edit: i think i can just use action "getout" to remove the unit, while it is still alive and then delete it.

Share this post


Link to post
Share on other sites

i need to know one last thing to finalize my script. i'm gonna make it available for download here, if anyone's interested.

very simple question, actually: how do i add additional parameters to my .sqf-script executed with execVM? couldn't find anything about it in the according biki-entries.

i want to add the possibility to customize the respawn equipment and use a distance limitation for group respawning - and if anyone can think of something else, just let me know

edit: i think i figured it out...

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  

×