Jump to content
Sign in to follow this  
LJF

Check Inventory for items? (Food system)

Recommended Posts

Hi,

I want to create a food system for a mission. The best I've come up with is using flares or something like that in the inventory, they could get removed every minute or so (or picked up in crates) - this part I can do - but I wanted the player to take damage or die when there are none left,

Does anyone know how to check if a unit has an item in their inventory? Or if you have any ideas on a better food system that would be awesome,

Thanks.

Share this post


Link to post
Share on other sites

OK, I've decided that that system is absolutely impossible to do.

Now I'm trying to work out how to make a countdown timer using a script that will increase by a given amount every time the player initiates an action (addaction on an object - food container or some such). I have no clue as to how to do this, anyone?

The other thing I need to do is to have the amount of food remaining be read out to the player in the form of a simple text message, probably once every minute or so. The thing is, it needs to be specific to that player, everyone needs to get their own individual food messages once a minute, it can't be a global "player1 has X food left".

Share this post


Link to post
Share on other sites

Okay, the first think you try is possible... but you need to make your own magazines like "toast" or "cola" to make it realistic.

In a Armed-Tactics mission we have chocolatbars and chewinggum and you can eat it^^

To check, if the player has some kind of mag (food):

player hasweapon "blah";
player hasmagazine "blah";

For your contdown script just make a script with a loop and a global (but not global over MP) variable like "hunger" or something:

if (not(_this select 0)==player) exitwith {}; //this is for checking if unit is player, this makes the loop local on a client

hunger=0;

while {alive player} do {

hunger=hunger+1;

if (hunger>10) then {player setdammage (getdammage player) + hunger/100;};
sleep 10;
};

You have to start this script in the init line of a player:

dumvar = [this] execVM "script.sqf";

Every 10 seconds the "hunger" will increase. If hunger gets higher than 10, the player will get some dammage (hunger/100... if hunger is 10 he will get 0.1 dammage).

To eat something, add a action entry to a object:

this addaction ["Eat something","eat.sqf"];

Now you need th script, that is executed from the actionentry:

if (not(_this select 1)==player) exitwith {}; //this checks if the player has activated this actionentry. Otherwise a player will be able to command the AI to eat for him^^

hunger=hunger-5;
if (hunger<0) then {hunger=0;};

The var "hunger" is global on the client, so it can be changed in every script. The loop that runs simultanious will get the new and lower hunger-var.

I hope you get it (yes i know, my english is very bad)

Share this post


Link to post
Share on other sites

Like NeoArmageddon said, it is possible to do that.

I only wonder if you're going to make an mission that lasts for several days ? Otherwise I wouldn't see much need in a food-system, except maybe for RPG-gamemodes.

Share this post


Link to post
Share on other sites

Wow, thanks guys, that is brilliant! I'll try it right away.

It doesn't last for days, but it is a kind of RPG mission in a way, I'm attempting to create a horror mission :D I need something to force the players to move between towns (scavenging for food while trying to survive for a certain time).

Thanks again to you both.

Edit: I can't seem to get it working. I tried removing the first line of the first script and that seemed to get it working, but then the second one didn't work. When I left both intact there were no effects of hunger so I couldn't tell if "eat.sqf" worked or not. The first script is brilliant by the way, especially since the player displays the effects of hunger.

Edited by LJF

Share this post


Link to post
Share on other sites
(scavenging for food while trying to survive for a certain time)

How about scavenging for ammo ?

There aren't many missions where ammo is scarce but it would make things more interesting and intense.

Share this post


Link to post
Share on other sites
How about scavenging for ammo ?

There aren't many missions where ammo is scarce but it would make things more interesting and intense.

I want to have both :)

I needed the food because otherwise people would just sit tight and hold out in a house. I've got a "creature" (just a dude in a guillie suit actually) who kills on contact, but unless the players need to go between the towns for something it won't really work - food being the likely candidate despite the time issue, but it will still work, they just digest really fast :D I'll probably try some kind of fast time system, maybe 10 minutes per day or something, but that will only come after the mission works properly, for now it's all night.

Share this post


Link to post
Share on other sites

OK, I've got the system working now, that's a great script there btw. I had to comment out the first line of each though, not sure why, but it seems to work. Here's the modified code I used:

"Food.sqf":

//if (not(_this select 0)==player) exitwith {}; //this is for checking if unit is player, this makes the loop local on a client

hunger=0;

while {alive player} do {

hunger=hunger+1;

if (hunger>10) then {player setdammage (getdammage player) + hunger/100;};

sleep 1;

};

"Eat.sqf":

//if (not(_this select 1)==player) exitwith {}; //this checks if the player has activated this actionentry. Otherwise a player will be able to command the AI to eat for him^^

hunger=hunger-5;

if (hunger<0) then {hunger=0;};

I tested it with 2 players (editor) but only I died, there was no effect on the AI soldier. I'm curious to know if without that line "hunger" is global or not. Will it work with multiple players, will they have independent hunger values or a single global value? (especially seeing as I don't have that first line in the code anymore).

I was also wondering if it were possible to have a character in the game who was immune from hunger, or a way to select which character the script acted on (instead of just the player character).

Share this post


Link to post
Share on other sites

I tested it with 2 players (editor) but only I died, there was no effect on the AI soldier. I'm curious to know if without that line "hunger" is global or not.

Did you call the scripts in the AI's Init box?

Surely because they are not Players but AI just running the script itself will not affect them? I am new to all this scripting so I could be wrong.

Share this post


Link to post
Share on other sites

Well a human can survive many days without food. Water would make more sense (you can put triggers that reset the "sustenance meter" everytime a player stands in front of a well or lake).

But it would be even better if the players scavenged for some sort of andidote or something until they get a cure?

Dunno just saying.

Share this post


Link to post
Share on other sites
Did you call the scripts in the AI's Init box?

Surely because they are not Players but AI just running the script itself will not affect them? I am new to all this scripting so I could be wrong.

I think you're right, I'm now worried because when I switched players I still had the same hunger value, I only hope this is due to teamswitch and not some the indication of a global variable - which would make MP impossible.

Share this post


Link to post
Share on other sites

The script i wrote for you can just affect players... ai wont make many sense in a mp coops. NO player wants to play a mission where he must always keep an eye on the ai, that they dont die in case of hunger... Thats not much fun... i already tried something like this.

You must start the script in the INIT line of a player. The player himself is trasported as parameter to the script. The first line checks, if the the script is running on the computer of the player. With your version the script will kill you on a dedicated with 5 or more players in seconds if you reach the hunger limit cause EVERY client is running a loop for you with your hunger level and if it reachs the dangerous level every client rises your damage... thats not the best way in MP^^

The script will break, if the unit that calls the script is not equal to the player on the specific computer.

The first line in the "eat" script checks, if the player is the guy who called the eating-action. This is very important... otherwise its possible that anybody can eat somethink for everybody.

The hunger-var is global in a client, not in a MP session. Every player can have his own hunger var. So if onw player in mp eats something and another dont, the second one will die and the other on survive.

If you want a hunger level for EVERY unit, not just for every player, the script will be much bigger and more complicated.

Share this post


Link to post
Share on other sites

Thanks, I was a little confused.

The first line checks, if the the script is running on the computer of the player.

Does this mean I must somehow activate the script somewhere other than the player or does it simply refer to the player Init script?

If I add the "eat.sqf" to an object it will be able to decrease only the value of the player who initiates the action entry?

I'm not sure if I did something wrong or not, here's what I've got:

- 2 soldiers named "soldier1" and "soldier2" each with "dumvar = [this] execVM "hunger.sqf";" in init.

- I water barrel object with "this addaction ["Eat something","eat.sqf"];" in init.

- Both [of your original] scripts in the main mission folder.

After 3 minutes nothing has happened to me. Does this mean I must initialize the script first or something? Sorry, I'm not very good at this am I :(

Edit: Ah, I see, because the variable "hunger" is global on the client then updating hunger through eat.sqf will only affect it, not the other players. I still can't see any effect of the script though, but when I remove the first line I take damage after a certain time, this makes me think I've done something wrong somewhere.

Edited by LJF

Share this post


Link to post
Share on other sites

Great, you get the point^^

I cant test this myself cause my ram is broken. But i dont see a mistake in the script.

Try the execVM with the soldiername as param:

"soldier1" has the initline : dumvar = [soldier1] execVM "hunger.sqf";

"soldier2" has the initline : dumvar = [soldier2] execVM "hunger.sqf";

And please have a look in your arma.rpt

Share this post


Link to post
Share on other sites

I still can't get it to work, it's here:

if (not(_this select 0)==player) exitwith {};

It must be something to do with that, I can't think of anything else. Of course, my knowledge is rather limited so I can't be sure.

Share this post


Link to post
Share on other sites

Okay... (_this select 0) is the unit, in whichs init line the script has been executed. If this is NOT equal to the player, the script will be aborted.

Maybe the "not" changes the (_this select 0) to an boolexpression... try this:

if (not((_this select 0)==player)) exitwith {};

One more braket^^

If this dont work, lets try some debuging:

Replace the "if"-line with this:

hintc format ["script has been exec from unit %1",_this select 0];

And for the eat script:

hintc format ["script has been exec from unit %1",_this select 1];

Share this post


Link to post
Share on other sites

Wow thanks! I think that worked! I need to test it mp of course but that worked for me in the editor.

Wow, an extra bracket ...

Edit: OK, the problem now is that "eat.sqf" doesn't work ... I even tried adding the new brackets to that as well.

Edit2: Does this mean anything to you?

File C:\Users\LJF\Documents\ArmA 2\missions\Hunger-Script.utes\eat.sqf, line 1

Error in expression <issions\Hunger-Script.utes\eat.sqf"

if (not(_this select 1)==player) exitwith {}>

Error position: <not(_this select 1)==player) exitwith {}>

Error not: Type Object, expected Bool

Edited by LJF

Share this post


Link to post
Share on other sites

Yes... wrong brakets^^

if (not((_this select 1)==player)) exitwith {};

"not" needs a boolexpression (true or false). In our case (without brakets) "not" thinks (_this select 1) is the expression (but its not a bool-value). ((_this select 1)==player) is a bool-value.

Share this post


Link to post
Share on other sites

Aha! Brilliant! It works now :) Thanks heaps for your scrips, I don't think I'd ever have been able to do it without your help.

Share this post


Link to post
Share on other sites

No problem.... you will learn to write your own script just by using scripts from other people (like me).

Share this post


Link to post
Share on other sites

Hmm, I played around with it for a bit but can't get it to work in MP, I tried it lan and the hunger script only seemed to affect me, the other player was immune.

Is there a way to start the script on the other player's computer? I'm pretty sure it's somehow only working on mine, and because it's local it doesn't affect anyone but me.

I was thinking of using an action menu entry, if the other players all had to start the script then would it load it for them too and not just me? Or would the same thing happen still?

Share this post


Link to post
Share on other sites

Oh ... right, sorry about that. I'll try it once I can get someone to test it out. Thanks again.

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
Sign in to follow this  

×