Jump to content
feldmaus

multiple commands in createUnit init field

Recommended Posts

Good morning,

 

i wanna commit several commands in the init field of the <createUnit> command, How to do this? I get always an Error about missing brackets. Here my code:

// create a new unit of same type at starting position
_unitType createUnit [
	_posUnit,
	_unitgroup,
		"_unit = this;
		this addEventHandler ["killed", {0 = [_this,_delay,_vehCall,_vehSeat,_parkPlace,_parkRad] execVM "unit_spawn.sqf"}];",
	_unitSkill,
	_unitRank];

Any idea?

 

regards

Share this post


Link to post
Share on other sites

the problem should be the double quotes inside your init line. Idk for sure but something similar to the follwoing pseudo code examples should fix it:

 

 

 


Pseudo example 1:
... createUnit [ ..., "doThis; doThat; thisStringis = ""my freaky string in init line""; doSomeMore; relax;", ...];

Pseudo example 1:
... createUnit [ ..., "doThis; doThat; thisStringis = 'my freaky string in init line'; doSomeMore; relax;", ...];

try those handling of the init line string and one of the examples should work...

Share this post


Link to post
Share on other sites

Your local variables can't be passed from a scope (createUnit command) to another one, especially an event handler one (here killed code).

I don't know where you're running your code... In init field of a unit/vehicle?.

So, here, in all cases,  _delay, _vehCall, _vehSeat, _parkPlace, _parkRad remain undefined (EH).

For the other ones, where are _posUnit, _unitGroup coming from?

 

The workaround for EH is probably (not tested) to setVariable for the unit (an array of variables with all the stuff will be "attached" to the unit).

In the code element: "_unit = this; _unit setVariable [ 'unitDef',[_delay,_vehCall,_vehSeat,..._posUnit,_unitGroup]; _unit addEventHandler ['killed',{
  _unit = _this select 0;

  [_this,(_unit getVariable 'unitDef') select 0,(_unit getVariable 'unitDef') select 1,.....] execVM 'unit_spawn.sqf' }];",.....

 

Note: I did something like that here, without working with the code element, for a similar result.

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites

@pierremgi i am executing a script called "unit_spawn.sqf" from the unit Init field in the editor with an addEventHandler command, and it passes the following arguments,

Unit (obj)

Delay (int)

Vehicle CallSign (str)

Vehicle Seat (str)

Park place (obj)

Park radius (int)

 

rest is determined in the script "unit_spawn.sqf".

 

Here a link to my script,

https://pastebin.com/0fRjL8u5

Share this post


Link to post
Share on other sites

Confirmed! That will never work. Use setVariable and attach what you need to your unit while alive. Then recall that for the new unit created with EH.

>> The variable container of the destroyed/killed unit doesn't disappear, (until you decide to make it nil).

 

Share this post


Link to post
Share on other sites

from what i understand it's the same unit respawning in same position so,  use the same variables again maybe.. ? (performance wise) 

also create a function like ,

 fnc_unitSpawn =
	{
	sleep ((_this select 1)*4);
	if (!alive (_this select 0)) then	
		{
		//deleteVehicle (_this select 0);
		sleep (_this select 1);
		_unit = (group (_this select 0))	createUnit [(typeOf (_this select 0)), ((getPos)_this select 0), [], (_this select 1), "FORM"];
		_unit addEventHandler ["killed", {0 = [(_this select 0),5,"blackfish_1","Driver",helipad_2,15] call fnc_unitSpawn}];
		_unit setSkill (skill(_this select 0));
		_unit setRank (rank(_this select 0));//not sure this or above code will work
		_unit setPosATL ((getPos)_this select 0);
		_unit setVehicleVarName (vehicleVarName (_this select 0));
		_unit setDir (getDir (_this select 0));
		deleteVehicle (_this select 0);
		 0 = [_unit,(_this select 1),blackfish_1,Driver,helipad_2,15] execVM unit_getin.sqf;
		 0 = [_unit,(_this select 1),blackfish_1,Driver,helipad_2,(_this select 5)] execVM "unit_getin.sqf";
		};
	};

 

not tested btw

Share this post


Link to post
Share on other sites

thank you jazzraill, but static arguments are not a good idea. The script is used by more then one AI. However i changed my code, i think pierremgi is right but i will check my code tommorow.

https://pastebin.com/0fRjL8u5

 

1.  // Following sentences are added to the init line of the _unit
2.  "_unit = this; //_unit is still an object but in next line it will be an array
3.  _unit setVariable [ 'unitDef',[_delay,_vehCall,_vehSeat,_parkPlace,_parkRad];
4. 
5. 
6.  /* We have to extract the Definitions from our Array, but the array is in the addEventHandler known as
7.  _this and not as _unit, so we have to define _unit again. */
8.  _unit addEventHandler ['killed', {
9.  _unit = _this select 0;
10.  	[_this, (_unit getVariable 'unitDef') select 0, //object, delay
11.    	(_unit getVariable 'unitDef') select 1, //vehCall
12.    	(_unit getVariable 'unitDef') select 2, //_vehSeat
13.    	(_unit getVariable 'unitDef') select 3, //_parkPlace
14.    	(_unit getVariable 'unitDef') select 4, //_parkRad
15.    	] execVM 'unit_spawn.sqf'}];

How can i add Line numbers to my Code above?

 

Why not use in Line 10 _unit instead of _this at the beginning?

10.  	[_unit, (_unit getVariable 'unitDef') select 0, //object, delay

 

Please check my discription, am i wrong with my thoughts?

Share this post


Link to post
Share on other sites

Well, on the right way... but:

First of all, your script (pastebin) starts with plenty of passed variables from??? You can't copy paste that in an init field of a unit.

for example, starting with:

_unit = (_this select 0) select 0; // First Argument, get Unit's details???

in the init field of a unit, this will fail for several reasons and the first one is there is nothing in term of _this select 0 select 0 !

 

Note: a good reason to script for spawning units is that you can do what you need when you need. In editor, the code in init field runs at each mission start (server, players, JIP), if anything else not specified.

And:

The only variable you can pass in a init code is : this    (representing the object, and then it's "data" container).

you can copy this variable: myUnit = this , directly in the init field.

As you can see, here is a global variable (myUnit), not a local one (_myUnit) forbidden in init field (direct scope).

Note: to use a local variable for a local script, in init field, you need to spawn the code: 0 = this spawn {_unit = _this; then whatever with _unit};

And you need to use local variables, because each unit is different.

 

So first of all, for an Eden edited unit, pick what data you need about this unit:

-  you can define some local variables like _backpack = backpack _unit (inside the spawned code, remember). but there is no way to pass that inside the event handler scope which runs, on killed event, like in other "dimension" (scope). The only variable known inside the EH code are described in this page., 3rd column, first dot by _this select 0, 2nd dot by _this select 1;...

As I wrote, there is a solution for the EH code. You can "carry" your data, referring to _this select 0 (the guy who died is still an object, and furthermore a variable container, until you delete it).

The best tool (AFAIK) is setVariable.

 

Your init field code should like this:

 

if (isServer) then {                                           // avoid to run this code multiple time in MP
this spawn {
  _unit = _this;
  _unit setVariable ["myunitData", [getpos _unit, getDir _unit, vehicle _unit,.....]]; // for all variables to be passed in EH
  _unit addEventHandler ["killed", {                 // opening EH code
    _unit = _this select 0;                                  // need to be (re)defined inside, you can change the name of course _object or else...
    _position = (_unit getVariable "myUnitData") select 0; //   according to your order in the array of data
    _direction = (_unit getVariable "myUnitData") select 1;
   < do what you need>
   Now, you can deleteVehicle _unit;              // if you want
  }];                                                                // close EH
};                                                                   // end of spawned code
};                                                                  // end server condition

With that, I imagine you want to respawn a unit, you will respawn.... once! Just because your created unit is similar to the died one but without code in its init field.

 

 

 

 

 

Share this post


Link to post
Share on other sites

I check my code tomorrow, it is not complete. But can you comment Line 10? Why do want me to use _this instead of _unit?

Share this post


Link to post
Share on other sites

I don't want anything. It's your first choice!  I'm not supposed to guess what the unit_spawn.sqf needs as entry variables. I'm afraid understanding you are looping this ugly sqf on itself for respawning one unit .... Horrible!

 

NOTE: regardless of the wrong method and the wrong scripting,  _this can have sense as it's the "magic variable" for the event handler. Follow the link to the BIKI page above. Moreover, when you write _unit = _this select 0;   you pick the fist element of the array _this (specific to each EH), and matching here the killed unit.

 

Good luck to make something workable for infinite respawn. I think your approach is a waste of time as you can't do that from inside the init field of one unit. See also :https://community.bistudio.com/wiki/setVehicleInit

 

The good approach seems to me scripting something general, like I did (link on my first post), and adapt for your purpose (specific units, vehicle crews)... WIP on my side.

Share this post


Link to post
Share on other sites

@pierrimgi, you are talking about Line 9 but not Line 10.

 

I understand Line 9.

 

9.  _unit = _this select 0;
10.  	[_this, (_unit getVariable 'unitDef') select 0, //object, delay

why do you use _this in Line 10. In Line 9 i still defined the object, but in Line 10 you are passing the _this(array). I think this is a mistake?

 

I still had a working script for infinte spawn, but its based on while and sleep commands. And i want to change my code to ground it on EH, which i think is much more performance friendly.

Share this post


Link to post
Share on other sites

Sorry, nothing to say more than i did, just before your post.

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

×