PDA

View Full Version : run a script on a player the moment he respawns



Mongoose_84
Dec 18 2006, 18:53
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...

CrashDome
Dec 18 2006, 18:59
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[/QUOTE] 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.

Mongoose_84
Dec 18 2006, 19:17
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.[/QUOTE]
thanks for the quick answer&#33;

i have almost no experience with MP-scripting so far and don&#39;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&#39;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&#39;m probably gonna despair a few times http://forums.bistudio.com/oldsmileys/crazy_o.gif )

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

KaRRiLLioN
Dec 19 2006, 19:31
If you use the trigger as suggested, it will run on every human player each time they respawn. Since you&#39;re using "Player" in the condition, that also makes it local to each human player. In other words, you don&#39;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.

Mongoose_84
Dec 19 2006, 19:59
thx for that clarification - it&#39;s a bit of a relief being told that http://forums.bistudio.com/oldsmileys/smile_o.gif

i&#39;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&#39;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&#39;s run on the player, when he&#39;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 &#40;_x == count _members&#41; then {goto &#34;respawn&#34;};
_unitX = _members select _x;
if &#40;NOT alive _unitX OR _unitX == _this&#41; then {goto &#34;findUnit&#34;};
_distX = &#40;_this distance _unitX&#41;;
if &#40;_distNewUnit &#33;= -1 AND _distX &#62; _distNewUnit&#41; then {goto &#34;findUnit&#34;};
_vehicleX = vehicle _unitX;
if &#40;_vehicleX &#33;= _unitX&#41; then {goto &#34;isInVehicle&#34;};
_newUnit = _unitX;
_distNewUnit = _distX;
_posIVNew = -1;
goto &#34;findUnit&#34;;

#isInVehicle

if &#40;commander _VehicleX == _unitX&#41; then {_posIVX = 0; goto &#34;isValid&#34;};
if &#40;driver _VehicleX == _unitX&#41; then {_posIVX = 1; goto &#34;isValid&#34;};
if &#40;gunner _VehicleX == _unitX&#41; then {_posIVX = 2; goto &#34;isValid&#34;};

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

goto &#34;findUnit&#34;;

#isValid

if &#40;_distX == _distNewUnit AND _posIVNew &#60; _posIVX&#41; then {goto &#34;findUnit&#34;};
_newUnit = _unitX;
_distNewUnit = _distX;
_posIVNew = _posIVX;
_vehicleNew = _vehicleX;
goto &#34;findUnit&#34;;

#respawn

if &#40;_distNewUnit == -1&#41; then {goto &#34;createNewUnit&#34;};

_newWeapons = weapons _newUnit;
_newMagazines = magazines _newUnit;
_weaponsCount = count _newWeapons;
_magazinesCount = count _newMagazines;
for &#91;{_x = 0}, {_x &#60; _magazinesCount}, {_x= _x + 1}&#93; do {_this addMagazine &#40;_newMagazines select _x&#41;};
for &#91;{_x = 0}, {_x &#60; _weaponsCount}, {_x= _x + 1}&#93; do {_this addWeapon &#40;_newWeapons select _x&#41;};
if &#40;posIVNew &#33;= -1&#41; then {goto &#34;moveInVehicle&#34;};
_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 &#40;_posIVNew&#41; do {case 0&#58; _this moveInCommander _vehicleNew; case 1&#58; _this moveInDriver _vehicleNew; case 2&#58; _this moveInGunner _vehicleNew; case 3&#58; _this moveInCargo _vehicleNew};
exit;

#createNewUnit

{_this addMagazine &#39;8Rnd_9x18_Makarov&#39;} forEach &#91;1,2,3,4,5,6,7,8&#93;;
_this addWeapon &#39;Makarov&#39;;
sleep 0.1;
_this selectWeapon &#39;Makarov&#39;;
[/QUOTE]
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&#39;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&#39;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&#39;s run on respawn - saying "Preprocessor failed on file ...respawnPlayer.sqs - error 7".

Celery
Dec 19 2006, 20:24
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&#58; alive player
Activation&#58; &#91;&#93; exec &#34;script.sqs&#34;[/QUOTE]

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

Igor Drukov
Dec 19 2006, 20:44
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...

Mongoose_84
Dec 19 2006, 20:59
that&#39;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) (http://www.flashpoint1985.com/cgi-bin/ikonboard311/ikonboard.cgi?act=ST;f=71;t=56106)

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?...

Igor Drukov
Dec 19 2006, 21:09
CrashDome is working on it. He says here (http://www.flashpoint1985.com/cgi-bin/ikonboard311/ikonboard.cgi?act=ST;f=71;t=55788;st=45) to expect it by... tomorrow&#33; I&#39;m almost tempted to insert a banana here...

Mongoose_84
Dec 19 2006, 21:17
CrashDome is working on it. He says here (http://www.flashpoint1985.com/cgi-bin/ikonboard311/ikonboard.cgi?act=ST;f=71;t=55788;st=45) to expect it by... tomorrow&#33; I&#39;m almost tempted to insert a banana here...
don&#39;t bother, i&#39;m gonna do it for you http://forums.bistudio.com/oldsmileys/wink_o.gif

http://forums.bistudio.com/oldsmileys/yay.gif

and thanks for the infos&#33;

Mongoose_84
Dec 20 2006, 20:44
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 &#91;{_x = 0}, {_x &#60; count _members}, {_x = _x + 1}&#93; do
{
_unitX = _members select _x;
if &#40;alive _unitX AND _unitX &#33;= _this&#41; then
{
_distX = &#40;_this distance _unitX&#41;;
if &#40;_distNewUnit == -1 OR _distX &#60; _distNewUnit&#41; then
{
_vehicleX = vehicle _unitX;
if &#40;_vehicleX == _unitX&#41; then
{
_newUnit = _unitX;
_distNewUnit = _distX;
_posIVNew = -1;
}
else
{
if &#40;commander _vehicleX == _unitX&#41; then {_posIVX = 0}
else if &#40;driver _vehicleX == _unitX&#41; then {_posIVX = 1}
else if &#40;gunner _vehicleX == _unitX&#41; then {_posIVX = 2}
else if &#40;_unitX in crew _vehicleX&#41; then {_posIVX = 3}
else {_posIVX = -1};
if &#40;_posIVX &#33;= -1 AND &#40;_distX &#60; _distNewUnit OR _posIVX &#60; _posIVNew&#41;&#41;
{
_newUnit = _unitX;
_distNewUnit = _distX;
_posIVNew = _posIVX;
_vehicleNew = _vehicleX;
};
};
};
};
};

if &#40;_distNewUnit &#33;= -1&#41; then
{
_newWeapons = weapons _newUnit;
_newMagazines = magazines _newUnit;
_weaponsCount = count _newWeapons;
_magazinesCount = count _newMagazines;
for &#91;{_x = 0}, {_x &#60; _magazinesCount}, {_x= _x + 1}&#93; do {_this addMagazine &#40;_newMagazines select _x&#41;};
for &#91;{_x = 0}, {_x &#60; _weaponsCount}, {_x= _x + 1}&#93; do {_this addWeapon &#40;_newWeapons select _x&#41;};
if &#40;posIVNew == -1&#41; 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 &#40;_posIVNew&#41; do
{
case 0&#58; _this moveInCommander _vehicleNew;
case 1&#58; _this moveInDriver _vehicleNew;
case 2&#58; _this moveInGunner _vehicleNew;
case 3&#58; _this moveInCargo _vehicleNew;
};
};
}
else
{
{_this addMagazine &#39;8Rnd_9x18_Makarov&#39;} forEach &#91;1,2,3,4,5,6,7,8&#93;;
_this addWeapon &#39;Makarov&#39;;
sleep 0.1;
_this selectWeapon &#39;Makarov&#39;;
};
[/QUOTE]

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

CrashDome
Dec 20 2006, 21:08
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> else
{
if &#40;commander _vehicleX == _unitX&#41; then {_posIVX = 0}
else if &#40;driver _vehicleX == _unitX&#41; then {_posIVX = 1}
else if &#40;gunner _vehicleX == _unitX&#41; then {_posIVX = 2}
else if &#40;_unitX in crew _vehicleX&#41; then {_posIVX = 3}
else {_posIVX = -1};
if &#40;_posIVX &#33;= -1 AND &#40;_distX &#60; _distNewUnit OR _posIVX &#60; _posIVNew&#41;&#41;
{[/QUOTE]

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

Mongoose_84
Dec 20 2006, 22:38
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> else
{
if &#40;commander _vehicleX == _unitX&#41; then {_posIVX = 0}
else if &#40;driver _vehicleX == _unitX&#41; then {_posIVX = 1}
else if &#40;gunner _vehicleX == _unitX&#41; then {_posIVX = 2}
else if &#40;_unitX in crew _vehicleX&#41; then {_posIVX = 3}
else {_posIVX = -1};
if &#40;_posIVX &#33;= -1 AND &#40;_distX &#60; _distNewUnit OR _posIVX &#60; _posIVNew&#41;&#41;
{

I believe you cannot use "else if" which is not a capability.[/QUOTE]
it worked in C... but then again this isn&#39;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&#33;

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&#39;ve caused the same error (i just had already removed them by then).

Mongoose_84
Dec 21 2006, 11:49
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.

Mongoose_84
Dec 21 2006, 17:44
i need to know one last thing to finalize my script. i&#39;m gonna make it available for download here, if anyone&#39;s interested.

very simple question, actually: how do i add additional parameters to my .sqf-script executed with execVM? couldn&#39;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...