OFPEC Forum

Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting Multiplayer => Topic started by: Cougarxr7 on 01 Jul 2009, 07:52:40

Title: Squad Leader as a respawn point. Solved!
Post by: Cougarxr7 on 01 Jul 2009, 07:52:40
I remmember playing Battlefield2 and in it you could respawn on your Squad Leader.
Has anyone come acrossed any code like that for Arma?
I have an idea of how it should be setup but then again I am no scripter.
I hope to make this possible to help encourage ground pounding.
Thanks!
Title: Re: Squad Leader as a respawn point.
Post by: JamesF1 on 01 Jul 2009, 11:26:45
I've not tested this (but it should work), and it does absolutely no checking to see if the unit is spawning inside a building, etc., but it does provide basic functionality.

Put this in your "Description.ext":
Code: [Select]
respawn = "BASE";
respawnDelay = 10;
Adjust the respawnDelay value to match the number of seconds you want to wait until respawn.

Put this in a file called "respawnOnLeader.sqf":
Code: (respawnOnLeader.sqf) [Select]
if(!isServer) exitWith {};
private["_leader", "_marker"];
_leader = _this select 0;
_marker = _this select 1;
while {true} do {
  _marker setMarkerPos (getPos _leader);
  sleep 0.5;
};

Then in your "init.sqf" put:
Code: (init.sqf) [Select]
[squadLeader, "respawn_west"] execVM "respawnOnLeader.sqf";
Replace "squadLeader" with the name of your unit (defined in the editor).  Don't put quotes around this value.

Replace "respawn_west" with "respawn_east" if you want this functionality for OPFOR, etc... leave the quotes surrounding it.  Then make sure you've placed a marker, name as above, in the editor.
Title: Re: Squad Leader as a respawn point.
Post by: Spooner on 01 Jul 2009, 13:54:17
"respawn_west", "respawn_east" are only used as respawn points in "BASE" respawn mode. "INSTANT" makes you respawn where you died.

Although your method, assuming you use "BASE" respawn, will work if there is only one group per side, it won't work in most missions.

Code: (description.ext) [Select]
// Remember to place the side spawn markers in a safe position.
respawn = "BASE";
respawnDelay = 10;

Code: (init.sqf) [Select]
if (not isDedicated) then {
  [] spawn {
    waitUntil { alive player };
    player addEventHandler ["KILLED", {
      [] spawn
      {
        waitUntil { alive player };
        if ((alive (leader player)) and (player != (leader player))) then {
          // Move the player just behind the leader, but ensure they arrive on the ground.
          _spawnPos = getPos ((vehicle _leader) modelToWorld [0, -2, 0]);
          _spawnPos set [2, 0];
          player setPos _spawnPos;
        };
      };
    }];
  };
};


Note: This doesn't take into account vehicles, since that is a bit messy (and I can't be bothered right now, but if it is important, maybe someone else could deal with that special case for you using emptyPositions). You'll spawn in just behind the leader (or leader's vehicle) and hope that the leader isn't on the edge of a cliff, in a plane or driving a tank backwards :whistle:

You could also use BIS_fnc_findSafePos (from BIS Functions module) to find a clear position in which to spawn.

NOTE: All example code UNTESTED!

EDIT: Remembered additional spawn required. Also should ensure that leader is alive before spawning on them.
Title: Re: Squad Leader as a respawn point.
Post by: JamesF1 on 01 Jul 2009, 14:38:22
"respawn_west", "respawn_east" are only used as respawn points in "BASE" respawn mode. "INSTANT" makes you respawn where you died.
Whoops, 'slip of the fingers'.  I really need to double-check my stuff a little more before I post, eh? :)

Quote
You could also use BIS_fnc_findSafePos (from BIS Functions module) to find a clear position in which to spawn.
To be honest, Spooner, I learn something pretty much every time you post ;)
Title: Re: Squad Leader as a respawn point.
Post by: Spooner on 01 Jul 2009, 19:03:50
Actually, you might learn something new, but I just realised I missed out a spawn I needed in the code I posted last. Edited it in the previous post rather than repeated it. Oops (sorry!).
Title: Re: Squad Leader as a respawn point.
Post by: Cougarxr7 on 03 Jul 2009, 09:05:24
I tried the following code.
init.sqf
Code: [Select]
//Code by Spooner

if (not isDedicated) then {
  [] spawn {
    waitUntil { alive player };
    player addEventHandler ["KILLED", {
      [] spawn
      {
        waitUntil { alive player };
        if ((alive (leader player)) and (player != (leader player))) then {
          // Move the player just behind the leader, but ensure they arrive on the ground.
          _spawnPos = getPos ((vehicle _leader) modelToWorld [0, -2, 0]);
          _spawnPos set [2, 0];
          player setPos _spawnPos;
        };
      };
    }];
  };
};
init.sqf
Code: [Select]
[1-1-A, "respawn_west"] execVM "data\scripts\respawnOnLeader.sqf";
descripton.ext
Code: [Select]
respawn="BASE";
respawndelay=30;
It retuned this error.
[1-1-A, "respawn_west"] execVM "data\scripts\>
  Error position: <A, "respawn_west"] execVM "data\scripts\>
  Error Undefined variable in expression: a
Thanks!
 

Title: Re: Squad Leader as a respawn point.
Post by: Spooner on 03 Jul 2009, 13:06:33
Sorry, I didn't specifically tell you how to call the script. From init.sqf:
Code: (This is right way) [Select]
[] execVM "data\scripts\respawnOnLeader.sqf";
You will need to place specific respawn markers on the map as appropriate for regular "BASE" respawn (since you want to respawn at base if the leader is dead or if you are the leader).

Code: (This is wrong, so don't do it) [Select]
[1-1-A, "respawn_west"] execVM "data\scripts\respawnOnLeader.sqf";
Cannot work, since 1-1-A is the name of a group, not a variable containing that group (hard to go back to first principles here). This is why you get an error. Apart from that, I don't ask for variables to be sent to the script (if you see one or more lines like
Code: (How to tell if something requires parameters) [Select]
_xxx = _this select N; at the top of a script, then it tells you that you need to send it parameters).
Title: Re: Squad Leader as a respawn point.
Post by: Cougarxr7 on 03 Jul 2009, 15:23:37
Respawn marker for "Base" is set.
I Change the name from 1-1-A to eng5. eng5 is the name of the squad leader.
Code: [Select]
[eng5, "respawn_west"] execVM "data\scripts\respawnOnLeader.sqf";
It returned this error.
[eng5, "respawn_west"] execVM "data\scrip>
  Error position: <eng5, "respawn_west"] execVM "data\scrip>
  Error Undefined variable in expression: eng5
Thanks!
Title: Re: Squad Leader as a respawn point.
Post by: Spooner on 03 Jul 2009, 16:07:24
Please read my post again! I tell you how to do it in the first code snippet (I've further commented it now to make it utterly clear).
Title: Re: Squad Leader as a respawn point.
Post by: Cougarxr7 on 04 Jul 2009, 08:49:52
Spooner
Please remmember with me, old and meds.
init.sqf
Code: [Select]
[] execVM "data\scripts\respawnOnLeader.sqf";descripton.ext
Code: [Select]
respawn="BASE";
respawndelay=30;


respawnOnLeader.sqf
Code: [Select]
//Code by Spooner

if (not isDedicated) then {
  [] spawn {
    waitUntil { alive player };
    player addEventHandler ["KILLED", {
      [] spawn
      {
        waitUntil { alive player };
        if ((alive (leader player)) and (player != (leader player))) then {
          // Move the player just behind the leader, but ensure they arrive on the ground.
          _spawnPos = getPos ((vehicle _leader) modelToWorld [0, -2, 0]);
          _spawnPos set [2, 0];
          player setPos _spawnPos;
        };
      };
    }];
  };
};
Ran this on a test server.
I was set to Squad Leader in editor, friends were Squad Members, all privates in editor.
I was 2000 meters from default spawn. All members said 'away' on icon.
My friends respawned at base everytime.
My respawn timer is 30secs, I have a kill cam script that runs at players death.
Do you think that 30secs respawn could be the problem or does that matter?
What am I missing/doing wrong?
Any ideas?
Thanks!
Title: Re: Squad Leader as a respawn point.
Post by: Spooner on 04 Jul 2009, 16:09:50
You aren't wrong at all. I'm just sloppy and didn't do a live test because I think I know everything  :whistle:

Code: [Select]
// Call from init.sqf with:
//   [] execVM "data\scripts\respawnOnLeader.sqf";

if (not isDedicated) then {
[] spawn {
waitUntil { alive player };
player addEventHandler ["KILLED", {
[] spawn
{
private ["_leader", "_spawnPos"];
waitUntil { alive player };
_leader = leader player;
if ((alive _leader) and (player != _leader)) then {
// Move the player just behind the leader, but ensure they arrive on the ground.
_spawnPos = (vehicle _leader) modelToWorld [0, -2, 0];
_spawnPos set [2, 0]; // Ensure respawn on the ground.
player setPos _spawnPos;
};
};
}];
};
};

2m behind leader seemed a bit too close, so I moved it to 5m. Easy enough to change it to an appropriate value though.

Can't be bothered ensuring that you are facing the leader either. It isn't critical though, since the important thing is just that you aren't placed in front of the leader where he would get a shock and shoot you... ;)
Title: Re: Squad Leader as a respawn point.Solved!
Post by: Cougarxr7 on 06 Jul 2009, 15:59:05
Spooner, it works! :D
Been busy testing. Thank you so much for your help!
It's really cool, squad members spawn randomly between leader and default.
Members were able to spawn inside a building with leader and outside of vehicles the leader was in.
I was in a stairwell that went to nowhere and member spawned under the stairs inside the walls but was able to walk right out.
In a helo which was landing and a member spawned on the ground below the leader.
Where do you change the number to 5, This one [0, -2, 0]; or this one [2, 0]; ?
I am so close to having this modded mission done.
Have a few more questions, starting new thread.
Thanks!
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Spooner on 06 Jul 2009, 16:11:12
Yeah, sorry, I said 5m behind but I left it as 2m in the code I gave you!
Code: [Select]

_spawnPos = (vehicle _leader) modelToWorld [0, -5, 0]; // -5 is 5m behind leader
_spawnPos set [2, 0]; // Ensure respawn on the ground. Set z-coordinate to 0.
Title: Re: Squad Leader as a respawn point.
Post by: Rommel92 on 06 Jul 2009, 16:36:30
Modified version, works for multiple groups etc.

Code: (description.ext) [Select]
respawn = "BASE";
respawnDelay = 10;
Adjust the respawnDelay value to match the number of seconds you want to wait until respawn.

Code: (respawnOnLeader.sqf) [Select]
if(isDedicated) exitWith {};
_side = side player;
waituntil {
  "respawn_west" setMarkerPosLocal (getPos leader player);
  sleep 0.5;
  0 > 1
};

Code: (init.sqf) [Select]
execVM "respawnOnLeader.sqf";
-----------

Untested code:
Code: (respawnOnLeader.sqf) [Select]
if(isDedicated) exitWith {};
_side = side player;
waituntil {
  format["respawn_%1", _side] setMarkerPosLocal (getPos leader player);
  sleep 0.5;
  0 > 1
};
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Cougarxr7 on 07 Jul 2009, 02:36:39
Thanks Rommel92,
 I will try it and test it!
Need to find out why I'm losing squad control.
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Spooner on 07 Jul 2009, 04:08:29
Rommel's does mostly the same as mine, but uses an continual loop rather than an event handler, so is less efficient. It also doesn't place the player behind the leader. Also, when there is no leader alive to spawn at, you will respawn out at sea ([0, 0, 0], the position of nullObject) rather than at base (all easy to add, but just so you know ;) ).

Does have the advantage that moving the spawn-point means that you will automatically spawn in a safe place, rather than in a wall, which is a big boon. Well, at least I assume respawn is that clever.

Scripts could be merged though, to get the best of both worlds.

@Rommel:
Code: [Select]
waituntil {
  "respawn_west" setMarkerPosLocal (getPos leader player);
  sleep 0.5;
  0 > 1
};
Is clearer as:
Code: [Select]
while { true } do {
  "respawn_west" setMarkerPosLocal (getPos leader player);
  sleep 0.5;
};
Since waitUntil implies you are running once-per-frame. while is better for loops with sleep in.
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Rommel92 on 07 Jul 2009, 06:01:37
Thanks for clearing that up for me, in any case, unfortunately respawn isn't that smart.  :whistle:
It does try to make an effort but.

Title: Re: Squad Leader as a respawn point. Solved!
Post by: Spooner on 07 Jul 2009, 12:47:02
Oh right. Still, plenty of other methods to ensure that the guy appears in a sensible place. You could spawn a local HeliH and see where it appears or use that BIS function I mentioned.
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Cellus on 14 Jul 2009, 07:08:26
Hello all.

I'm using a script to move respawn at random intervals to random people, but I have this problem:

Quote from: Spooner
Also, when there is no leader alive to spawn at, you will respawn out at sea ([0, 0, 0],

How would I overcome this?

And how do I move the marker behind, instead of onto a player? Or better yet, a certain distance from the player in the direction of original spawn?
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Spooner on 16 Jul 2009, 14:43:48
As I gave in examples above, it is best to keep the base marker in place and to only move the player from that position if there is a viable leader to move to. Saves continually moving the marker, too, so is more efficient.

If you need to move the marker, then:
Code: (place spawnpoint 5m behind the leader) [Select]
_spawnPos = getPos ((vehicle _leader) modelToWorld [0, -5, 0]);
"respawn_west" setMarkerPos _spawnPos;
Title: Re: Squad Leader as a respawn point. Solved!
Post by: Kezei on 23 Aug 2009, 20:20:00
Sorry for bringing up an older thread, especially if there is one newer, but how would one go about modifying this script so that you would spawn on your respective squad leader if and only if he's alive. If he's dead, you instead spawn as a crow?