Jump to content
slothstronaut

Calling an inbuilt function by stored name

Recommended Posts

Hey guys,

 

I'm storing an array of actions and objects, and rather than have to write a handler for each equipment type as it comes through, I'd like to be able to do something a little more generic.

 

Here's a small example of what I'm trying to do (please note, this is not production ready, just a test):

 

_command = "addGoggles=G_Aviator"; 
_split = _command splitString "="; 
 
_unit = player; 
_function = _split select 0;
_object = _split select 1;
 
>> insert part I don't know how to do but something like _unit _function _object or in pseudo "for this unit, call this function, with this argument"
 
I've looked into call and it doesn't seem to work with having an argument on the left hand side (such as player addGoggles "G_Aviator"; for example).
 
I can get it to work if I write a helper function, which looks like this:
 
kitAddGoggles = {
    params ["_unit", "_object"];
    _unit addGoggles _object;
};
 
_command = "kitAddGoggles=G_Aviator"; 
_split = _command splitString "="; 
 
_unit = player; 
_object = _split select 1; 
_function = _split select 0; 
 
_params = [_unit, _object];
_params call call compile _function;
 
But I'd prefer to not have to do a helper for every equipment type.
 
Anyone have anything they can suggest, or even if this is possible?
 
Cheers!

 

Share this post


Link to post
Share on other sites

You've basically got it right.

// Function declared in init.sqf or via the whole CfgFunctions thing*.
fn_kitAddGoggles = {
    params ["_unit", "_object"];
    _unit addGoggles _object;
};
 
// Call in a script somewhere to give player a pair of G_Aviator goggles.
[player, "G_Aviator"] call fn_kitAddGoggles;


CfgFunctions information.

Share this post


Link to post
Share on other sites

I was hoping to avoid having a helper function like fn_kitAddGoggles and just call addGoggles directly, but if this is the only way, it's not so bad.

 

Thanks for the reply!

Share this post


Link to post
Share on other sites

You can absolutely just do:

player addGoggles "G_Aviator";

The function is so that you can give any unit any goggle without having to type out addGoggles a zillion times.

Share this post


Link to post
Share on other sites

Oh, I know I can call it explicitly, but I was hoping to call it by reference, so, something like:

 

_unit = player;

_function = "addGoggles";

_object = "G_Aviator";

 

_unit _function _object;

Share this post


Link to post
Share on other sites
call compile ([_unit, _function, str _object] joinString " ");
//Result: call compile "player addGoggles ""G_Aviator"""
//Result: call {player addGoggles "G_Aviator"}

Final solution:

private _str = "player addGoggles ""G_Aviator""";
private _dta = _str splitString " ";
call compile (_dta joinString " ");

Share this post


Link to post
Share on other sites

That doesn't seem to work, unfortunately. Complains that there's a missing semi-colon, no matter how I rearrange things.

Share this post


Link to post
Share on other sites

That doesn't seem to work, unfortunately. Complains that there's a missing semi-colon, no matter how I rearrange things.

 

Just tested, both variants works fine for me

Share this post


Link to post
Share on other sites

Apologies, I didn't see your update. That definitely did work. Fantastic! Thank you very much!

Share this post


Link to post
Share on other sites

That sounds a bit extreme..

 

I agree. And even more, it sounds stupid. I apologize to all whom it will bring inconvenience

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

×