Jump to content

[RELEASE] Food Spoils Script


Reaper5150

Recommended Posts

Hi all, Got another script I did for raw meats that will spoil after a set amount of time and removes the item from the gear and replaces it with bio meat. Don't know why this was never Implemented before so I went ahead and made one. :) I would love to add a count down timer on the hud for it but I'm not good at making those. You can customize this script by just changing the item classnames to what you want. 

 

Call it from init.sqf add at the bottom of the file something like  execVM "scripts\foodstats.sqf";

change the path to whatever you like or where you keep your scripts.

 

The script:

//================================================
//=========== -Written by Reaper5150 ================
//================================================

private ["_hasitem","_hasitem2","hasitme3","_hasitem4","_hasitem5","_hasitem6","_hasitem7","_hasitem8","_hasitem9"];

_hasitem = "FoodBaconRaw" in magazines player;
_hasitem2 = "FoodBeefRaw" in magazines player;
_hasitem3 = "FoodGoatRaw" in magazines player;
_hasitem4 = "FoodMuttonRaw" in magazines player;
_hasitem5 = "FoodChickenRaw" in magazines player;
_hasitem6 = "FoodRabbitRaw" in magazines player;
_hasitem7 = "FoodRawTrout" in magazines player;
_hasitem8 = "FoodRawSeaBass" in magazines player;
_hasitem9 = "FoodRawTuna" in magazines player;

_hasitem = "FoodBaconRaw" in magazines player;
if (_hasitem) then {
            systemChat "The raw meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodBaconRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };


_hasitem2 = "FoodBeefRaw" in magazines player;
if (_hasitem2) then {
            systemChat "The raw meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodBeefRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem3 = "FoodGoatRaw" in magazines player;

if (_hasitem3) then {
            systemChat "The meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodGoatRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem4 = "FoodMuttonRaw" in magazines player;

if (_hasitem4) then {
            systemChat "The meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodMuttonRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem5 = "FoodChickenRaw" in magazines player;

if (_hasitem5) then {
            systemChat "The meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodChickenRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem6 = "FoodRabbitRaw" in magazines player;

if (_hasitem6) then {
            systemChat "The meat in your inventory will spoil in 15 minutes";
            sleep 900;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodRabbitRaw";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem7 = "FoodRawTrout" in magazines player;

if (_hasitem7) then {
            systemChat "The meat in your inventory will spoil in 1 hour";
            sleep 3600;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodRawTrout";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem8 = "FoodRawSeaBass" in magazines player;

if (_hasitem8) then {
            systemChat "The meat in your inventory will spoil in 1 hour";
            sleep 3600;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodRawSeaBass";
            sleep 5;
            player addMagazine "FoodBioMeat";
        };

_hasitem9 = "FoodRawTuna" in magazines player;

if (_hasitem9) then {
            systemChat "The meat in your inventory will spoil in 1 hour";
            sleep 3600;
            systemChat "Your raw meat has spoiled";
            player removeMagazine "FoodRawTuna";
            sleep 5;
            player addMagazine "FoodBioMeat";
};
 

 

Link to comment
Share on other sites

I like the idea of the script. You have asked why nobody else has made a script like that already, well it is because this is a way more complicated as you perhaps think at the moment. First of all you are new to coding so you can acutally not know most of the things so do not worry about it.

Your script has a few problems:

1. The script runs for the server too. It is not really a problem for the script itself but if you run things for clients only use.

if (!isDedicated) then { code };

2. It only runs one time. A while loop would be required so the script can check the whole time the player is playing.

3. If one condition is true, lets say the first one, the script will wait 900s and will remove after that 'FoodBaconRaw'. But it does not check when the player got this item or the player even still have it. After removing the item it will add the biomeat no matter if the player really had the removed item. A few thoughts about that to make it more clear:

  • The player could have cooked the 'FoodBaconRaw' within the 900s. If the player gets a new 'FoodBaconRaw' within the 900s the script will remove it even it was in the player inventory only a few seconds.

4. The script will always remove only 1 item. If you have 2x 'FoodBaconRaw' only one gets removed.

5. If you relog everything gets resetted, the same if you died and you take back your gear. If you have died and took back your gear, it will only work after a relog because of the missing while loop.

6. Because of the high sleeps you are using the most parts of the script will never be checked. It just takes too long even if you would have added a while loop.

7. Some classnames are written wrong like FoodRawTuna is FishRawTuna.

To realize this script idea it will need a few variables and a complete different way to script it. I would call it expert level. Those are the key features you have to think about:

  • When has the player add the item to the inventory and how many items.
  • How to handle the removal to the items if one or more got removed already by another action.
  • How to handle if a new item of the same type got added within the time the other items will spoil.
  • If a player relogs or getting killed.
  • What about if the meat is inside the backpack.
  • Do not use sleep with those high numbers. Use diag_tickTime instead.

I've wrote a small script how it works better. But it still does not check the followong things:

  • Backpack
  • Time when the player got the items
  • Relog and death
// Set the spoil time at the start of the script.
local _spoilTickTime = diag_tickTime;
// Nested array of items that spoil within the inventory. [Spoilt item, New item].
local _spoilItems = [["FoodBaconRaw","FoodBioMeat"],["FishRawTuna","FoodBioMeat"]];
// spoil time in mins
local _spoilTime = 15;

while {1==1} do {

	// Every default: 15mins the items spoil in the inventory.
	if ((diag_tickTime - _spoilTickTime) > (_spoilTime*60)) then {

		{
			local _item = _x select 0;

			// Make sure that the player has acutally the item that spoils in their inventroy.
			if (_item in magazines player) then {			
				local _newItem = _x select 1;

				// Count the items that will spoil now to get them all.
				_qtyItems = {_x == _item} count magazines player;
				for "_i" from 1 to _qtyItems do {
					player removeMagazine _item;
					player addMagazine _newItem;
				};

				// Get display text of the spoilt items and inform the player.
				_displayName = getText(configFile >> "CfgMagazines" >> _item >> "displayname");
				systemchat format ["%1 %2 spoiled in your inventory.",_qtyItems,_displayName];
			};		
		} foreach _spoilItems; 

		// Reset the spoil time of the script.
		_spoilTickTime = diag_tickTime;
	};

	uisleep 5;
};

 

Link to comment
Share on other sites

3 hours ago, A Man said:

I like the idea of the script. You have asked why nobody else has made a script like that already, well it is because this is a way more complicated as you perhaps think at the moment. First of all you are new to coding so you can acutally not know most of the things so do not worry about it.

Your script has a few problems:

1. The script runs for the server too. It is not really a problem for the script itself but if you run things for clients only use.


if (!isDedicated) then { code };

2. It only runs one time. A while loop would be required so the script can check the whole time the player is playing.

3. If one condition is true, lets say the first one, the script will wait 900s and will remove after that 'FoodBaconRaw'. But it does not check when the player got this item or the player even still have it. After removing the item it will add the biomeat no matter if the player really had the removed item. A few thoughts about that to make it more clear:

  • The player could have cooked the 'FoodBaconRaw' within the 900s. If the player gets a new 'FoodBaconRaw' within the 900s the script will remove it even it was in the player inventory only a few seconds.

4. The script will always remove only 1 item. If you have 2x 'FoodBaconRaw' only one gets removed.

5. If you relog everything gets resetted, the same if you died and you take back your gear. If you have died and took back your gear, it will only work after a relog because of the missing while loop.

6. Because of the high sleeps you are using the most parts of the script will never be checked. It just takes too long even if you would have added a while loop.

7. Some classnames are written wrong like FoodRawTuna is FishRawTuna.

To realize this script idea it will need a few variables and a complete different way to script it. I would call it expert level. Those are the key features you have to think about:

  • When has the player add the item to the inventory and how many items.
  • How to handle the removal to the items if one or more got removed already by another action.
  • How to handle if a new item of the same type got added within the time the other items will spoil.
  • If a player relogs or getting killed.
  • What about if the meat is inside the backpack.
  • Do not use sleep with those high numbers. Use diag_tickTime instead.

I've wrote a small script how it works better. But it still does not check the followong things:

  • Backpack
  • Time when the player got the items
  • Relog and death

// Set the spoil time at the start of the script.
local _spoilTickTime = diag_tickTime;
// Nested array of items that spoil within the inventory. [Spoilt item, New item].
local _spoilItems = [["FoodBaconRaw","FoodBioMeat"],["FishRawTuna","FoodBioMeat"]];
// spoil time in mins
local _spoilTime = 15;

while {1==1} do {

	// Every default: 15mins the items spoil in the inventory.
	if ((diag_tickTime - _spoilTickTime) > (_spoilTime*60)) then {

		{
			local _item = _x select 0;

			// Make sure that the player has acutally the item that spoils in their inventroy.
			if (_item in magazines player) then {			
				local _newItem = _x select 1;

				// Count the items that will spoil now to get them all.
				_qtyItems = {_x == _item} count magazines player;
				for "_i" from 1 to _qtyItems do {
					player removeMagazine _item;
					player addMagazine _newItem;
				};

				// Get display text of the spoilt items and inform the player.
				_displayName = getText(configFile >> "CfgMagazines" >> _item >> "displayname");
				systemchat format ["%1 %2 spoiled in your inventory.",_qtyItems,_displayName];
			};		
		} foreach _spoilItems; 

		// Reset the spoil time of the script.
		_spoilTickTime = diag_tickTime;
	};

	uisleep 5;
};

 

You are totally correct. I'm still very new at this whole coding thing and can only really make real simple scripts atm. And yes! I was wondering why it was doing exactly what you described without the loop. I will incorporate what you said to do and post the updated script with the fixes. Thanks for your help! :) 

Link to comment
Share on other sites

It is a very complicated way to realize this script. The main point why it is so hard, is because the item like the food and all magazines and weapons a player can take in-game are just items and no objects. So you cannot set a variable on a single object like you can do it on a building, a vechicle or a player. To work around you have to set a variable on the player which saves what the player does and when with the items. Plus you have to save the state of the variables to the database.

From my point of view it is not worth the effort and the performace impact that this script would have for only this small feature. You could add the backpack checking to the script above but the rest I would skip.

Link to comment
Share on other sites

17 hours ago, A Man said:

It is a very complicated way to realize this script. The main point why it is so hard, is because the item like the food and all magazines and weapons a player can take in-game are just items and no objects. So you cannot set a variable on a single object like you can do it on a building, a vechicle or a player. To work around you have to set a variable on the player which saves what the player does and when with the items. Plus you have to save the state of the variables to the database.

From my point of view it is not worth the effort and the performace impact that this script would have for only this small feature. You could add the backpack checking to the script above but the rest I would skip.

I agree! It makes since that those items would indeed have to save to each player in the DB and I was just thinking that when you commented the first time then I went back looking at the script improvements you made and hours of sitting here trying to figure out a work around. The main factor is I need to keep in mind that anything that has to save to each player like the gear is gonna require sql functions to be added to the DB.

Well! it was worth a try I will set this aside and work on it again in the future when I'm a little better at coding. This is what I like about this community learning from the pro's so I thank you for taking time out of your day to teach me :)  I'll just stick to helping some of these guys having trouble with simple scripts I'm use to seeing and have worked with before. You can remove this one if you want but I really think it may also help someone who is also just starting out.

Thank again @A Man

Link to comment
Share on other sites

Also could I have permission to use your script and make changes later when I figure out how to do the backpack counting the items?

I will add the credits as you being the one who wrote it :)

// Set the spoil time at the start of the script.
local _spoilTickTime = diag_tickTime;
// Nested array of items that spoil within the inventory. [Spoilt item, New item].
local _spoilItems = [["FoodBaconRaw","FoodBioMeat"],["FishRawTuna","FoodBioMeat"]];
// spoil time in mins
local _spoilTime = 15;

while {1==1} do {

	// Every default: 15mins the items spoil in the inventory.
	if ((diag_tickTime - _spoilTickTime) > (_spoilTime*60)) then {

		{
			local _item = _x select 0;

			// Make sure that the player has acutally the item that spoils in their inventroy.
			if (_item in magazines player) then {			
				local _newItem = _x select 1;

				// Count the items that will spoil now to get them all.
				_qtyItems = {_x == _item} count magazines player;
				for "_i" from 1 to _qtyItems do {
					player removeMagazine _item;
					player addMagazine _newItem;
				};

				// Get display text of the spoilt items and inform the player.
				_displayName = getText(configFile >> "CfgMagazines" >> _item >> "displayname");
				systemchat format ["%1 %2 spoiled in your inventory.",_qtyItems,_displayName];
			};		
		} foreach _spoilItems; 

		// Reset the spoil time of the script.
		_spoilTickTime = diag_tickTime;
	};

	uisleep 5;
};

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...