Newbie Needs Help (v3.0 addAction and Cursor Targets)

ITr1ckst3rI

Member
So, i have been trying to mess around with adding actions to in-game objects. Specifically the vending machine. I know addAction has conditions that, if met, will add the action. I also know that if the action is added to the object, and not the player, then the player has to be looking at the object and within 15 meters.

My issue is I have no clue how to execute any of this xD

lets say i have "vending.sqf" in a folder called "custom".

i would add the code
Code:
[] execVM "custom\vending.sqf";
to my init file.

Now contained within this vending.sqf would be this
Code:
_vm1 = "MAP_vending_machine";
_vm1 addAction ["Buy A Drink ", "path\to\menu.sqf",  "cursorTarget isKindOf 'MAP_vending_machine' && _target distance cursorTarget < 5"];
_vm1 addAction ["Buy A Drink 1", "path\to\menu.sqf",  "cursorTarget isKindOf 'MAP_vending_machine' && (_target distance cursorTarget) < 5"];
_vm1 addAction ["Buy A Drink 2", "path\to\menu.sqf",  "(_target distance cursorTarget) < 5"];
_vm1 addAction ["Buy A Drink 3", "path\to\menu.sqf",  "_target distance cursorTarget < 5"];
_vm = "vending_machine";
_vm addAction ["Buy A Drink 4", "path\to\menu.sqf",  "cursorTarget isKindOf 'vending_machine' && _target distance cursorTarget < 5"];
_vm addAction ["Buy A Drink 5", "path\to\menu.sqf",  "cursorTarget isKindOf 'vending_machine' && (_target distance cursorTarget) < 5"];
_vm addAction ["Buy A Drink 6", "path\to\menu.sqf",  "(_target distance cursorTarget) < 5"];
_vm addAction ["Buy A Drink 7", "path\to\menu.sqf",  "_target distance cursorTarget < 5"];

Know i have the following attempts in these lines.
1) The check for distance with ( ) and without
2) Cursor Target iskindof (this shouldn't be needed because it object has the action)
3) MAP_vending_machine and vending_machine.


I know addaction is a local command but because this is executed on the client side (downloaded with mission) there shouldn't be an issue.


annnnnnnd none of them work *facepalm* need help.

EDIT: Success. The Following code worked to add the action to the vending machine.
Code:
//====================Vending Machine===============
if ((cursorTarget isKindOf "MAP_vending_machine") && (player distance cursorTarget < 5) && (speed player <= 1)) then {
        if (s_player_vending < 0) then {
        s_player_vending = player addaction[("<t color=""#ff0000"">" + ("Buy Drink") +"</t>"),"custom\buydrink.sqf","",5,false,true,"", ""];
        };
} else {
    player removeAction s_player_vending;
    s_player_vending = -1;
};

//==================================================
HOWEVER
Where i had my vending machine, its very very hard to target it. SO!
I made this to give players the option when near it. So even if I can't target it, i will still get the option
Code:
_list0 = position player nearObjects ["MAP_vending_machine",3];
_amount = count _list0;
if ((_amount >= 1) && (speed player <= 1)) then {
        if (s_player_vending < 0) then {
        s_player_vending = player addaction[("<t color=""#ff0000"">" + ("Buy Drink") +"</t>"),"admintools\AdminToolsMain.sqf","",5,false,true,"", ""];
        };
} else {
    player removeAction s_player_vending;
    s_player_vending = -1;
};
 
Last edited:
Oh my OH MY!
I fooled with something similiar on my Takistan server where I had a bunch of NPC's wandering around in a refugee camp and wanted to be able to hire them. That was a while ago so I am a bit fuzzy off the top of my head about the issues.

Here is my guess. the vending.sqf is in your mission but that doesnt mean its running LOCAL. the mission file is executed on both the server and client. So the vending machine is local to the SERVER and that is where the addactions are located ... again, guessing, but if the vending machine is created during server startup .. its local to the server and not your client so I don't think you can add actions directly to it.
Why not simply add the actions to the player when the conditions are true. The fn_selfactions.sqf file runs every frame so use it to your advantage.
Code:
if (cursorTarget isKindOf 'MAP_vending_machine' && _target distance cursorTarget < 5) then
      {
      _vendingmachine = player addAction ["Buy A Drink ", "path\to\menu.sqf"  ];
      }
else
     {
     player removeaction  _vendingmachine;
      };

*all code is untested and written on the fly. not guaranteed to do anything except cause errors
 
Last edited:
Oh my OH MY!
I fooled with something similiar on my Takistan server where I had a bunch of NPC's wandering around in a refugee camp and wanted to be able to hire them. That was a while ago so I am a bit fuzzy off the top of my head about the issues.

Here is my guess. the vending.sqf is in your mission but that doesnt mean its running LOCAL. the mission file is executed on both the server and client. So the vending machine is local to the SERVER and that is where the addactions are located ... again, guessing, but if the vending machine is created during server startup .. its local to the server and not your client so I don't think you can add actions directly to it.
Why not simply add the actions to the player when the conditions are true. The fn_selfactions.sqf file runs every frame so use it to your advantage.
Code:
if (cursorTarget isKindOf 'MAP_vending_machine' && _target distance cursorTarget < 5) then
      {
      _vendingmachine = player addAction ["Buy A Drink ", "path\to\menu.sqf"  ];
      }
else
     {
     player removeaction  _vendingmachine;
      };

*all code is untested and written on the fly. not guaranteed to do anything except cause errors

Wow I didn't even think to use the self actions xD Sometimes i get too caught up in my little box. Will try that out and edit this post with the results.
 
Also, having another issue with something easier.
(this script is being called with execVM so i dont think i can use_time?)
Code:
_time
for "_i" from 30 to 0 step -1 do {
sleep 1;
_time = _i;
cutText [format[_time, "Seconds until vehicle spawn", name player,"\nPlease make sure you have room in your toolbelt! For the key!"], "PLAIN"];
};

I shouldn't even have to explain my goal :p How can i have this loop go once per second and then continue the script

will i have to put a
Code:
waitUntil{_time == 0}

EDIT: Fixed it!
Code:
    _fnc_timer = {
    _cnt = 30;
    for "_i" from 1 to 30 do
    {
        cutText [format["Spawning %1 in %2 Seconds! MAKE ROOM IN YOUR TOOLBELT FOR THE KEY!!!",_veh,_cnt], "PLAIN DOWN",1];
        sleep 1;
        _cnt = _cnt - 1;
    };
    call _fnc_spawncar;
  };
This waits 30 seconds then calls another block of code. Great for making a timer!
 
Last edited:
Updated Original Post to include working script.

notes for @ShootingBlanks : The format that fn_SelfActions use is as follows (Just incase you didn't know, but I'm sure you did)

You need to have the conditions
Code:
if ((cursorTarget isKindOf "MAP_vending_machine") && (player distance cursorTarget < 5) && (speed player <= 1)) then {
if (s_player_vending < 0) /*<--- this */ then {
        s_player_vending = player addaction[("<t color=""#ff0000"">" + ("Buy Drink") +"</t>"),"custom\buydrink.sqf","",5,false,true,"", ""];
        };
} else {
    player removeAction s_player_vending;
    s_player_vending = -1; //<-- and this
};
 
Last edited:
I respectfully disagree. As per https://community.bistudio.com/wiki/addAction only the actions title and the script/code to be executed is required. Everything else is optional.

I created a video for fn_selfactions and will upload to the website later on ..
Yes everything else is optional for the addaction. However i *think* for the fn_selfAction's you need those two conditions. I didn't code the game so im not 100% sure but it seems like everyone uses those conditions. Even within the script. Though i'm wrong a lot so i probably am again :p
 
My video tutorial on fn_selfactions.sqf and addaction is viewable at http://iaretech.com/phpbb3/viewtopic.php?f=8&t=22&p=23#p23
You are correct, everyone uses those settings and I show why in the video although they are not required. And if you are testing a script you would want the least amount of things you can screw up, so put just the basics and when its working correctly you can fix up the command line.

So for the benefit of those who don't want to watch a 15 minute video for a simple answer:
You can use an addaction simply with
Code:
player addaction["Menu Command","scripts\menucommandscript.sqf"];

BUT the 5th optional parameter shows the menu in the center of the screen (showwindow) which is NOT what most people want so they will set that to 'false'. But to set the 5th parameter you will have to set all the previous parameters too, you can't just skip to whatever option you want, they are in a specified order.
object addaction["Title","code","arguments",priority,showwindow]; which will look like this when you create an actual addaction.
Code:
player addaction["Menu Command","scripts\menucommandscript.sqf","",0,false];
In your code you also add the 6th parameter as true which is to keep the menutitle visible so you can quickly click it again, and then you add "","" which are two empty values and therefore unneeded since you aren't setting them to anything (they are a keypress shortcut, and the condition that is required for the addaction to be enabled).

And after reviewing my post and the addaction documentation again https://community.bistudio.com/wiki/addAction I noticed that when you put parameter 6 to TRUE, that is the default value. So you could stop at the parameter 5 set to false and it would be the same as what you have now..
 
Back
Top