Random Item on Random Unit AddAction

The init will randomly assign each item to a random civilian, assuming there are three of them named civ1, civ2, civ3. It will assign a variable to the player declaring which civ has each item for him.

Each civ is than given three action choices to ask about a map, watch or compass. If the civilian doesn’t have it (the variable isn’t correct) he’ll say he’s got nothing for him. If the variable is correct he’ll say he has the item and give it to the player. You’d of course replace that with whatever task you wanted them to complete.

Might not be the most elegant code, (but kinda is!) but works in single player at last and since everything is local should work as is in multiplayer I think.   Here’s a step by step explanation of how it all works.

init.sqf + Functions module on map.


waituntil {!isnil "bis_fnc_init"};

_civs = [civ1, civ2, civ3];
_watchHolder = _civs call BIS_fnc_selectRandom;
_civs = _civs - [_watchHolder];
_mapHolder = _civs call BIS_fnc_selectRandom;
_civs = _civs - [_mapHolder];
_compassHolder = _civs select 0;

player setVariable["watchHolder", _watchHolder];
player setVariable["mapHolder", _mapHolder];
player setVariable["compassHolder", _compassHolder];

// Debug :)
hint format["Watch = %1\n\nMap = %2\n\nCompass = %3", player getVariable "watchHolder",player getVariable "mapHolder", player getVariable "compassHolder"];

Init field of each civ unit:

this addAction ["Ask about a watch.","itemCheck.sqf",["watchHolder"]];
this addAction ["Ask about a map.","itemCheck.sqf",["mapHolder"]];
this addAction ["Ask about a compass.","itemCheck.sqf",["compassHolder"]];


private ["_item", "_player"];

_npc = _this select 0;
_player = _this select 1;
_action = _this select 2;

_npc removeAction _action;

_itemHeld = _this select 3 select 0;
_item = switch (_itemHeld) do {
	case "watchHolder": {"ItemWatch"};
	case "mapHolder": {"ItemMap"};
	case "compassHolder": {"ItemCompass"};

if (_player getVariable _itemHeld == _npc) then {
	hint "Found it!";
	_player addWeapon _item;
} else {
	hint "Sorry, I have nothing for you!";

Comments are closed.