Jump to content
seed

Need some help with a function

Recommended Posts

Hello everyone. As the title suggests need some help with a function. For my current mission that i am creating i wanted to implement a "loot" system that the "loot" refreshes every x seconds.The function that i have written works fine as been executed once at the start of the mission.

_Loot_Chance_Medical = 40; //Chance for medical

_Loot_Chance_Food = 45; //Chance for food

_Loot_Chance_Drink = 50; //Chance for drink



_gamelogic = CENTER;

_radius = 15000;



Seed_fn_Haus1 = {
//Search for a specific haustype on map.
_Haus1 = nearestObjects [getPosATL _gamelogic, ["Land_HouseV_1I4"], _radius];
					
{					//Create objects on haustype
					_obj1 = createVehicle ["Land_PlasticCase_01_large_F", (_x modelToWorld [-4.5,-3,-1]), [], 0, "CAN_COLLIDE"];
					_obj1 setDir (getDir _x - 180);
					_obj2 = createVehicle ["Land_Chest_EP1", (_x modelToWorld [-2,-1,-2.5]), [], 0, "CAN_COLLIDE"];
					_obj2 setDir (getDir _x - 90); 
					
				//Select Random loot from array
					_lootChoiceM = _Loot_Medical call BIS_fnc_selectRandom;
					_lootChoiceF = _Loot_Food call BIS_fnc_selectRandom;
					_lootChoiceD = _Loot_Drink call BIS_fnc_selectRandom;
//defince chance for medical loot in object					
if ( random 100 > _Loot_Chance_Medical ) then {
					//add loot "medical" to object
					_obj1 addItemCargoGlobal [_lootChoiceM, 1];
	};	
//define chance for food loot in object			
if ( random 100 > _Loot_Chance_Food ) then {
					// add loot "food" to object
					_obj1 addItemCargoGlobal [_lootChoiceF, 1];
	};	
//define chance for drink in object
if ( random 100 > _Loot_Chance_Drink ) then {
					//add loot "drink" to object
					_obj1 addItemCargoGlobal [_lootChoiceD, 1];
	};	

}forEach _Haus1; //run for every haus found on map.
};
//calling the function
call Seed_fn_Haus1;

How can i insert on the above a "loop" that refreshes the "loot" every x seconds ?

 

Regards

 

Seed

Share this post


Link to post
Share on other sites

if the script never needs to 'end' in order for something else to occur, then you can just run the loop like this:

while{true} do
{
    myCodeHere;
    sleep howLongToWait;
};

Otherwise you can spawn the loop in a seperate thread by spawning it:

[] spawn
{
    while{true} do
   {
      myCodeHere;
      sleep howLongToWait;
   };
};

Share this post


Link to post
Share on other sites

How can i insert on the above a "loop" that refreshes the "loot" every x seconds ?

 

Hello seed, as austin_medic already mentioned, you can loop the call to your function after the first run by adding this code:

[] spawn {
        while {true} do {
                call Seed_fn_Haus1;
                sleep 600;
        };
};

Before you start looping like that, you might want to delete the objects created with previous runs ?

 

Consider this approach:

 

Since the function is placing the objects in the same positions inside the same houses every time, you could split your function to three separate functions like this, for example: 1. Create the objects inside houses, 2. Add random cargoitems to objects, 3. Clear cargoitems, keep objects in place

 

Thus, you could call function #1 once, then loop functions #2 and #3 with set time interval.

 

Hope this helps,

 

Nikander

Share this post


Link to post
Share on other sites

also unless the location changes you should make an array of the house locations so as not to keep running nearestobjects which is quite a load on the CPU.

Share this post


Link to post
Share on other sites

Thank you all for your help sofar. After spending some frustrating time as there was some conflicts with other stuff, i decided to split the function as suggested. Here is what i have sofar.

//First Function to place the objects in building(Runs only once)
Seed_fn_Haus1 = {
//Search for a specific haustype on map.
_Haus1 = nearestObjects [getPosATL _gamelogic, ["Land_HouseV_1I4"], _radius];
					
{					//Create objects on haustype
					_obj1 = createVehicle ["Land_PlasticCase_01_large_F", (_x modelToWorld [-4.5,-3,-1]), [], 0, "CAN_COLLIDE"];
					_obj1 setDir (getDir _x - 180);
					_obj2 = createVehicle ["Land_Chest_EP1", (_x modelToWorld [-2,-1,-2.5]), [], 0, "CAN_COLLIDE"];
					_obj2 setDir (getDir _x - 90); 
				
				

}forEach _Haus1; //run for every haus found on map.
};
call Seed_fn_Haus1;

//Loop Function for adding items.
Seed_fn_loop1 = {

					_lootChoiceM = _Loot_Medical call BIS_fnc_selectRandom;
					_lootChoiceF = _Loot_Food call BIS_fnc_selectRandom;
					_lootChoiceD = _Loot_Drink call BIS_fnc_selectRandom;

//Debug Hint to check if the selection working
hint format ["Objects: %1 %2 %3",_lootChoiceM, _lootChoiceF, _lootChoiceD ];

//defince chance for loot					
if ( random 100 > _Loot_Chance_Medical ) then {
					//Clear cargo of _obj1
                                           clearItemCargoGlobal _obj1;
                                         //Add item to _obj1
					_obj1 addItemCargoGlobal [_lootChoiceM, 1];
	};				
if ( random 100 > _Loot_Chance_Food ) then {
					//Clear cargo of _obj1
                                         clearItemCargoGlobal _obj1;
                                        //Add item to _obj1
					_obj1 addItemCargoGlobal [_lootChoiceF, 1];
	};	
if ( random 100 > _Loot_Chance_Water ) then {
					//Clear cargo of _obj1
                                        clearItemCargoGlobal _obj1;
                                        //Add item to _obj1
					_obj1 addItemCargoGlobal [_lootChoiceD, 1];
	};	



};
//Loop that refreshes every 300 seconds
while{true} do
{
  call Seed_fn_loop1;
    sleep 300;
};

The above runs as it supposed and also displays the "Debug Hint" once at the "start" and every 300 seconds.. The problem is that it doesnt add any items at all to "_obj1". I am supposing that the problem is the "_obj1" from the second function as it does not knows where to place the objects.. If that is the case then  how can i add "items" in the "_obj1" defined on the first function from the loop function.

 

Regards

 

Seed

Share this post


Link to post
Share on other sites

Thank 

The above runs as it supposed and also displays the "Debug Hint" once at the "start" and every 300 seconds.. The problem is that it doesnt add any items at all to "_obj1". I am supposing that the problem is the "_obj1" from the second function as it does not knows where to place the objects.. If that is the case then  how can i add "items" in the "_obj1" defined on the first function from the loop function.

 

Regards

 

Seed

You could pass _obj1 into the second function. looking at your current setup thats going to be super awkward to do. At least from my brain, maybe somebody else could do it.

Share this post


Link to post
Share on other sites

how can i add "items" in the "_obj1" defined on the first function from the loop function.

 

Hello seed, I suggest you modify function "Seed_fn_Haus1" so that it will return an array of created objects. First, declare the array as empty:

Seed_fn_Haus1 = {
        private "_array";
        _array = [];
...

Then, insert each created object to the array with the pushBack command:

...
        _obj1 = createVehicle ...
        _array pushback _obj1;
...

Lastly, as a result your function should output the array:

...
        } forEach _Haus1;
        _array
};

Now you are able to store the objects to a global array and send to function "Seed_fn_loop1" for processing:

...
        } forEach _Haus1;
        _array
};
missionNamespace setVariable ["myobjects", call Seed_fn_Haus1];

Modify function "Seed_fn_loop1" so that it can process the input array using a forEach loop. (replace "_obj1" references with _x)

 

Seed_fn_loop1 = {
        {
                if ... then {
                        clearItemCargoGlobal _x;
                        _x addItemCargoGlobal [_lootChoiceD, 1];
                };
        } forEach _this;
};

Finally, modify your loop accordingly:

while {true} do {
        myobjects call Seed_fn_loop1;
        sleep 300;
};

Have fun !

 

Nikander

  • Like 1

Share this post


Link to post
Share on other sites

Thanks you very much for your explanation and help Nikander. Now it works as its supposed to work. So if i understand correctly you simply created a "variable" that stores the empty "_array" that it gets populated with the usage of "_x"  that in turn passes the  items from the "second function" to the created object from the "first function". Right ? I will make sure to note that for future references. Interesting to say at least the usage of functions. Again thank you all very much. For those that intersted here is the whole function.

Seed_fn_Haus1 = {

		private "_array";
		_array = [];

	_Haus1 = nearestObjects [getPosATL _gamelogic, ["Land_HouseV_1I4"], _radius];
{
			//Create objects on haustype
					_obj1 = createVehicle ["Land_PlasticCase_01_large_F", (_x modelToWorld [-4.5,-3,-1]), [], 0, "CAN_COLLIDE"];
					_obj1 setDir (getDir _x - 180);
					_array pushback _obj1;
					
					_obj2 = createVehicle ["Land_Chest_EP1", (_x modelToWorld [-2,-1,-2.5]), [], 0, "CAN_COLLIDE"];
					_obj2 setDir (getDir _x - 90); 
					_array pushback _obj2;
				
} forEach _Haus1;
_array
};
missionNamespace setVariable ["myobjects", call Seed_fn_Haus1];

Seed_fn_loop1 = {

					_lootChoiceM = _Loot_Medical call BIS_fnc_selectRandom;
					_lootChoiceF = _Loot_Food call BIS_fnc_selectRandom;
					_lootChoiceD = _Loot_Drink call BIS_fnc_selectRandom;



			{		
				//defince chance for loot					
				if ( random 100 > _Loot_Chance_Medical ) then {
					//Clear cargo of _obj1
                                           clearItemCargoGlobal _x;
                                         //Add item to _obj1
					_x addItemCargoGlobal [_lootChoiceM, 1];
				};				
				if ( random 100 > _Loot_Chance_Food ) then {
					//Clear cargo of _obj1
                                         clearItemCargoGlobal _x;
                                        //Add item to _obj1
					_x addItemCargoGlobal [_lootChoiceF, 1];
				};	
				if ( random 100 > _Loot_Chance_Water ) then {
					//Clear cargo of _obj1
                                        clearItemCargoGlobal _x;
                                        //Add item to _obj1
					_x addItemCargoGlobal [_lootChoiceD, 1];
				};
			} forEach _this;		
					
					
};
    while {true} do {
            myobjects call Seed_fn_loop1;
            sleep 300;
    };

One problem less from my list.

 

Regards

 

Seed

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

×