Jump to content
schadler17

Saving Variable at beginning of script

Recommended Posts

I'm making a script to save a helicopters initial spawn location and to teleport it to another object(Helipad). My issue is, on respawn, the vehicle spawns back at the Helipad, not the original spawn location. Is there any way to call and save the _initspawn variable to be called on later? Or am I writing something wrong?

 

vehicle_pos.sqf:

private ["_veh","_pos_point","_pos","_initspawn"];

_veh = _this select 0;
_initspawn = (getPosATL _veh);
_pos_point = _this select 1;
_pos = (getPosATL _pos_point);

_veh addEventHandler ["Respawn", {_veh setPosATL _initSpawn}]

_veh setDammage 0;
_veh addEventHandler ["HandleDamage",{false}];

_veh setPosATL _pos;

sleep 3;

_veh removeEventHandler ["HandleDamage", 0]

Trigger condition:

(isServer ) and ({(_x isKindOf 'Air')} count thislist > 0) and (time > 30)

On Act:

{if(_x isKindOf 'Air') then {0 = [_x,helipos1] execVM "scripts\vehicle_pos.sqf"}} foreach thislist;

The only problem is that when the helicopter respawns, its on the helipad, not the trigger which activates the script. I want the location of the trigger(_initspawn) to save the initial spawn location of the helicopter in editor to be called on in the Respawn EventHandler to set the position back to the original spawn location until the trigger fires again.

Share this post


Link to post
Share on other sites

_intiSpawn is out of scope once it's in the EH, so I recommend using get/set variable to access the information you want.

private ["_veh","_pos_point","_pos","_initspawn"];

_veh = _this select 0;
_initspawn = (getPosATL _veh);
_veh setVariable ["vehicleSpawnPoint",_initspawn,true];
_pos_point = _this select 1;
_pos = (getPosATL _pos_point);

_veh addEventHandler ["Respawn", {_veh setPosATL ((_this select 1) getVariable ["vehicleSpawnPoint",[0,0,0]])}];

_veh setDammage 0;
_veh addEventHandler ["HandleDamage",{false}];

_veh setPosATL _pos;

sleep 3;

_veh removeEventHandler ["HandleDamage", 0];

Share this post


Link to post
Share on other sites

Fixed up the script a bit and finally got it working. I've been trying to find this out now for a few days, maybe a week lol.

Thank you jshock for the tip about the event handler. Only thing I had to change was {_veh setPosATL ((_this select 1) to 0. 1 was calling the _pos_point object, not the helicopter. Other than that, it worked perfectly.

 

Result:

private ["_veh","_pos_point","_pos","_initspawn"];

_veh = _this select 0;

_pos_point = _this select 1;
_pos = (getPosATL _pos_point);

_initspawn = _this select 2;
_spawnpos = (getPosATL _initspawn);

_veh setVariable ["vehicleSpawnPoint",_spawnpos,true];
_veh addEventHandler ["Respawn", {_veh setPosATL ((_this select 0) getVariable ["vehicleSpawnPoint",[0,0,0]])}];

_veh setDammage 0;
_veh allowDamage false;
sleep 2;

_veh setPosATL _pos;
sleep 3;
_veh allowDamage true;

My issue was I have a respawn script in a mod, but it was causing the helicopters that I wanted spawned ontop of the Carrier to blow up on Respawn. The initial spawn was fine, so I knew it was something with the way the respawn script is written(should be fixed next update, but I cbf'd to wait cause this mission is gona be bad ass lol.) My solution, creating 2 markers on the map and a trigger for each helicopter that I wanted spawned. Each helicopter and the initial spawnpoint is on the mainland, while the second marker is on the carrier with a fixed altitude for spawning the objects there and running this script on them.

Share this post


Link to post
Share on other sites
_veh addEventHandler ["Respawn", {_veh setPosATL ((_this select 0) getVariable ["vehicleSpawnPoint",[0,0,0]])}];
The initial spawn was fine, so I knew it was something with the way the respawn script is written.

 

 

To say it with JShock:

_intiSpawn is out of scope once it's in the EH, so I recommend using get/set variable to access the information you want.

 

The same applies to the "_veh" inside your respawn EH (JShock, you failed to see ;)). And to quote myself:

An event handler is only a setup of something which should happen in some case. It's not immediatly executed code, so of course the EH code can't access local variables from the surrounding script.

When dealing with EH code, always look at them like "standalone" code, meaning you should consider everything around it as non-existent.

Share this post


Link to post
Share on other sites

Seems to be working in my case, as everytime the helicopter respawns it respawns at the _initspawn location.

Share this post


Link to post
Share on other sites

Hmm... I can't test right now, but could you (or anyone else) please put a systemChat str _veh into the EH's code and tell what the result is? Because as of the above code, I can't imagine _veh to be anything else than nil inside the respawn EH.

Share this post


Link to post
Share on other sites

You're right, seemed it was working but I noticed sometimes the heli's are spawning on the carrier, not on the spawn markers. Sometimes it works, sometimes not.

 

Any way to fix the local variable so that it carries over to the Event Handler?

 

Edit: Fixed it, just added the EventHandler to the init of the helicopters and changed the local variables to the object names. I use the init EH due to the Respawn script using createVehicle.

this addEventHandler ["Init", {this setPosATL (getPosATL helispawn1)}];

Just gotta do this for each heli and change the name of the spawn location to the helipad object that I want it to initially spawn on(with the trigger). Will post here if I come across any other issues.

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

×