Jump to content
Sign in to follow this  
DChristy87

Creating and Calling a Function Walk through (Barney Style)?

Recommended Posts

I'm just not capable of understanding how functions work. I really need to be walked through it... (maybe others might need the same help?)

I know it would take some time to do it but if someone could just walk through the process of writing a function and actually calling it... Yes I've read this: http://community.bistudio.com/wiki/Functions_Library_%28Arma_3%29

But I'm still incapable of wrapping my head around it! Your help will be greatly appreciated!

Lets say my "function" would be this:

_wp = getPos _this;
sleep 10;
hideBody _this;  //or hideBody _this select 0;
_guard = [getMarkerPos "Post1Spawn", east, ["O_Soldier_F"]] call BIS_fnc_spawnGroup;
leader _guard addEventHandler ['killed', {(_this select 0) execVM 'AiKilled.sqf'}];
_wpMove = _guard addwaypoint [_wp, 0]; 
_wpMove setWaypointType "MOVE";
_wpMove setWaypointBehaviour "SAFE";

This is to get a unit to respawn and return to its previous position...

How do I actually make that a function?

Where do I put it?

Where do I compile it?

How do I call it?

WHERE/when do I call it?

PLEASE, I'm talkin Barney Style walkthrough! I've read that functions link and all I really understand is what they do, not how to really create them (where to put them) or where/when to call them!

Share this post


Link to post
Share on other sites

Basically it's exactly the same as a stand alone SQF file but instead of putting it in file you put it in { }.

For that code you'd usually put that in a file called AiKilled.sqf and call it via: deadGuy execVM "AiKilled.sqf";

For that function you'd put that code between aiKilled = { }; and call it via: deadGuy spawn aiKilled;

You can put the code anywhere that you want really, init.sqf or even in it's own function file to keep things clean. You'd compile it by putting it where it'll be run by players. You'd call it via call or spawn. You'd call or spawn it whereever you'd normally run a script.

init.sqf:

execVM "functions.sqf";

functions.sqf:

aiKilled = {
// grab the object then location of the dead body.
_body = [_this, 0, objNull, [objNull]] call BIS_fnc_param;
_waypointLoc = getPos _body;

// Optional values you can set if you want, default to 10, "Post1Spawn", east.. and so on.
_delay = [_this, 1, 10, [123]] call BIS_fnc_param;
_spawnMarker = [_this, 2, "Post1Spawn", [""] call BIS_fnc_param;
_spawnSide = [_this, 3, east, [east] call BIS_fnc_param;
_spawnUnit = [_this, 4, ["O_Soldier_F"], [[]]] call BIS_fnc_param;
_waypointType = [_this, 5, "MOVE", [""]] call BIS_fnc_param;
_wayPointBehaviour = [_this, 6, "SAFE", [""]] call BIS_fnc_param; 

// The actual code of the function.
sleep _delay;
hideBody _body;  //or hideBody _this select 0;
_guard = [getMarkerPos _spawmMarker, _spawnSide, _spawnUnits] call BIS_fnc_spawnGroup;
leader _guard addEventHandler ['killed', {[(_this select 0)] spawn aiKilled}];
_wpMove = _guard addwaypoint [_waypointLoc, 0]; 
_wpMove setWaypointType _waypointType;
_wpMove setWaypointBehaviour _wayPointBehaviour;
};

If you wanted to call this on it's own:

_null = [someObject] spawn aiKilled;

Edited by kylania

Share this post


Link to post
Share on other sites

Okay, So its starting to become a little more clear, so could I run it like this then?:

init field of unit:

this addEventHandler ['killed', {[(_this select 0)] spawn aiKilled}];

Or am I off base? and if this IS correct, is the method you just explained at all quicker or more efficient then just having the AIKilled.sqf file with:

_wp = getPos _this;
sleep 10;
hideBody _this;  //or hideBody _this select 0;
_guard = [getMarkerPos "Post1Spawn", east, ["O_Soldier_F"]] call BIS_fnc_spawnGroup;
leader _guard addEventHandler ['killed', {(_this select 0) execVM 'AiKilled.sqf'}];
_wpMove = _guard addwaypoint [_wp, 0]; 
_wpMove setWaypointType "MOVE";
_wpMove setWaypointBehaviour "SAFE";

and this in the init field of the unit?:

_guard addEventHandler ['killed', {(_this select 0) execVM 'AiKilled.sqf'}];

---------- Post added at 12:48 ---------- Previous post was at 12:46 ----------

Or would I need a precompile or compilefinal anywhere to make it faster/more efficient or to put less stress on the server or whatever?

Share this post


Link to post
Share on other sites

The precompile and compilefinal stuff are for if you wanted to use an actual SQF separate file as a function. You can safely ignore those for now. The way I described you don't need to do that. For the kinds of things you're doing just an inline function would be perfectly fine.

The method I posted is exactly the same as yours, I just built in a lot of different options if you wanted to use the one function to spawn any number of AI at any spawnpoint and the like. So instead of always spawning at Post1Spawn you could spawn some elsewhere via:

_null = [someObject, 10, "myOtherSpawnMarker"] spawn aiKilled; 

The 10 in there would be the delay. It defaults to 10 unless you put another number there.

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  

×