Jump to content
Sign in to follow this  
epicgoldenwarrior

Hunger/Thirst System (Done but with issues)

Recommended Posts

Hello everybody!

Now, I wanted to add a hunger/thirst system to my mission, but it has proven to be more difficult than expected.

First, I couldnt find someones code to use that D: lol. So, I used the base of a weak one to make it more complicated.

The scripts are really simple, but I'll go over the issue, maybe someone can help me as I cannot see it here -.-

--------------------------------------------------------

Init.sqf:

thirst = 100;

hunger = 100;

inv_food = 0;

inv_drinks = 0;

If inv_drinks > 0 then P2 addAction ["Drink", "drink.sqf"];

If inv_food > 0 then P2 addAction ["Eat", "eat.sqf"];

---------------------------------------------------------

Now, that being done, eat.sqf and drink.sqf are pretty much the same, so I'll only post one:

------------------------------------------------------

Eat.sqf:

inv_food = inv_food - 1;

hunger = hunger + 5;

hint "Youre eating food";

P2 switchMove "mychosenanimation";

----------------------------------------------------

The issue is, I cant seem to run eat.sqf because I can't seem to get food items. I have a shop where you can buy food (made my own cash system too, but ignore that part)

you pretty much walk up to the guy and I have an addaction to buy food for money. Here it is:

---------------------------------------------

buyfood.sqf:

inv_food = inv_food + 1;

cash = cash - 10;

Hint "you bought food";

---------------------------------------------

I do the action, and it runs the script (I assume) but I dont have the action to eat food, and my cash balance is still the same. Does anyone see an issue here?

I apologies for any sloppiness, but I'm pretty new to the forums and I'm not super experienced with scripting, since I've recently learned some for Arma and want to implement it.

If you need clarification on anything please let me know.

Thanks

Share this post


Link to post
Share on other sites

Well if your init.sqf actually looks as you put it, there are a few syntax errors (fixed below):

thirst = 100;
hunger = 100;
inv_food = 0;
inv_drinks = 0;

if (inv_drinks > 0) then {P2 addAction ["Drink", "drink.sqf"]};
if (inv_food > 0) then {P2 addAction ["Eat", "eat.sqf"]};

Could be the reason you don't have any action to eat/drink.

As for the cash, you may want to declare it along with your other variables (thirst, hunger, inv_drinks, inv_food) in the init.sqf so that each script sees and understands what "cash" is, because if you declared it in your cash system, without having a call to that script in your init.sqf, well, it's essentially just a .sqf file that's in the mission folder, because there is no call to it.

And if you could link all files associated with this (including cash system and such), we may be able to help you out a bit more, it's better than us assuming something based on something we haven't seen.

Edited by JShock

Share this post


Link to post
Share on other sites

I have a lot to say on this matter, but none of it is in intelligent language.

Take a look inside this mission I just whipped up (Dropbox)

Just unarchive that inside the same folder where your other missions are saved

Run over to the other guy and he will give you food and drink for free (I did not implement a money system)

Share this post


Link to post
Share on other sites

if you have this in the init, it is only checked once.

If inv_drinks > 0 then P2 addAction ["Drink", "drink.sqf"];
If inv_food > 0 then P2 addAction ["Eat", "eat.sqf"];

i believe what you are looking for is a condition in the addaction that will continually check if the value of food and drink increases above 0.

player addaction ["Drink","drink.sqf",[],5,false,true,"","inv_drinks >0"];

You can change it all for food as well with the same kind of command.

I suggest, using this to your advantage and putting the option in there to monitor your hunger and thirst as well.

example

player addaction ["Drink","drink.sqf",[],5,false,true,"","inv_drinks >0 && thirst <=25"]

Share this post


Link to post
Share on other sites

Thank you for your replies, I'll go in the order they were answered in;

Jshock - thanks for the update, I'll add those in, but I don't think they make a difference but I could be wrong so I'll give it a shot.

Cash is also in the init but I excluded it since I wanted it to be focused on the food and not confuse anyone. I have that down, thank you though

DreadedEntity - I pretty much have the gain food down, its just not...working. I think. Idk I'll check it out and thanks for the help :)

KevsnoTrev - Yes, more than once is what I want. Thanks for bringing this up otherwise I never would have figured that out (not really there yet but thanks)

although I dont understand the last part with the monitor your hunger stuff. ._. sorry. If you could explain that'd be lovely xD

Also I have another question - some of these scripts are really small, any way I could just do something like {P2 addaction ["Buy food", cash = cash - 10, food = food + 1]} or something like that?

So I wouldnt have to make a script for everything -.-

Thanks

Edited by EpicGoldenWarrior

Share this post


Link to post
Share on other sites
Also I have another question - some of these scripts are really small, any way I could just do something like {P2 addaction ["Buy food", cash = cash - 10, food = food + 1]} or something like that?

No, addActions won't work like that, You could try using a case structure based on the addAction ID or a variable passed through the action, but that will take a bit of explanation...but below is an example:

This would be in a sqf file named: food_drink_handler.sqf

switch (_this select 3 select 0) do //Selecting the variable we will be comparing to the cases below.

{

Case "BuyFood": {cash = cash - 10; food = food + 1;}; //<<<basically saying if the variable coming in is equal to "BuyFood" then do blah (being change cash value and food amount), same goes for the other 3 cases below for each of their own separate variables.
//addAction line would be: "P2 addAction ["Buy food","food_drink_handler.sqf",["BuyFood"],5,false,true,"",""];

Case "Eat": {food = food - 1; hunger = hunger + 10};
//addAction line would be: "P2 addAction ["Eat","food_drink_handler.sqf",["Eat"],5,false,true,"","inv_food >0"];

Case "BuyDrink": {cash = cash - 10; drink = drink + 1;};
//addAction line would be: "P2 addAction ["Buy Drink","food_drink_handler.sqf",["BuyDrink"],5,false,true,"",""];

Case "Drink": {food = food - 1; thirst = thrist + 10};
//addAction line would be: "P2 addAction ["Drink","food_drink_handler.sqf",["Drink"],5,false,true,"","inv_drinks >0"];
};

If you read this page to see what each of the "sections" between the commas within the [ ] after the addAction, you may be able to understand more of what is going on within this script: https://community.bistudio.com/wiki/addAction

And what KevsnoTrev means by monitoring your hunger/thirst variables is that the action will only show up for the player to use under certain conditions/circumstances so the way he has the addAction "Drink" conditions setup above is that the action will only show up if you have a drink in your inventory (inv_drinks >0) and if your thirst level is equal to or below 25% (thirst <=25). So basically with his conditions that he put, the only time the player will get the action to drink is when he has a drink in inventory and when his thirst level is below, in this case, 25%. <-Redundant statement, I know...

I would say to leave these condition checks out for the buying of the food/drink and just have a hint pop up that the player doesn't have enough cash to buy food/drink, just my opinion.

That basically just helps reduce the number of "useless" calls, because if the player doesn't have a drink in his inventory, why give him the option to drink?

Edited by JShock

Share this post


Link to post
Share on other sites
No, addActions won't work like that,
? why not, they would only be changing global variables.. Admittedly as you have shown is a better way of doing it but there is nothing wrong with doing it in the action.

thirst = 100;
hunger = 100;
inv_food = 0;
inv_drinks = 0;
cash = 100;


player addAction ["Buy Food", {cash = cash - 20; inv_food = inv_food + 1; systemChat "You bought some food"}, [], 1, false, true, "", "cash >= 20"];
player addAction ["Buy Drink", {cash = cash - 10; inv_drinks = inv_drinks + 1; systemChat "You bought a drink"}, [], 1, false, true, "", "cash >= 10"];
player addAction ["Eat", {inv_food = inv_food - 1; hunger = hunger + 5; systemChat "You feel less hungry"}, [], 1, false, true, "", "inv_food > 0"];
player addAction ["Drink", {inv_drinks = inv_drinks - 1; thirst = thirst + 5; systemChat "You feel less thirsty"}, [], 1, false, true, "", "inv_drinks > 0"];


_simThread = [] spawn {
while {true} do {
	sleep 10;
	thirst = thirst - 5;
	hunger = hunger - 5;

	if (thirst <= 10) then {
		systemChat "You need to drink something before you die of dehydration";
	};
	if (hunger <= 10) then {
		systemChat "You need to eat something before you die of starvation";
	};
	if (cash <= 0 && {isNil "outOfCash"}) then {
		systemChat "You have spent all your money";
		outOfCash = true;
	};

	if ( { _x <= 0 }count [hunger, thirst] > 0) then {
		[ "END1", false, 5] call BIS_fnc_endMission;
	};
};
};

_monitorStats = [] spawn {
while {true} do {
	hintSilent format["Thirst\n%1\nHunger\n%2\n\nDrink\n%3\nFood\n%4\n\nCash\n%5", thirst, hunger, inv_drinks, inv_food, cash];
	sleep 1;
};
};

Share this post


Link to post
Share on other sites

Well...my excuse that I'm going with is that my mind wasn't working at the time...I was just set on arguements passed via arrays :p.

Share this post


Link to post
Share on other sites

Thanks a lot JShock! Seems a little unclear atm so I'll look into it 0_o pardon my noobness D:

The way Larrow did it is pretty much whats familiar to me as to what I know right now, plus thats like 80% of my code wth I am not too good at this -

is it possible to just copy and paste that in the init (with my own modifications of course)?

JShock I feel like I'm sounding like I'm not giving you any credit but I really appreciate your help

Share this post


Link to post
Share on other sites
Thanks a lot JShock! Seems a little unclear atm so I'll look into it 0_o pardon my noobness D:

The way Larrow did it is pretty much whats familiar to me as to what I know right now, plus thats like 80% of my code wth I am not too good at this -

is it possible to just copy and paste that in the init (with my own modifications of course)?

JShock I feel like I'm sounding like I'm not giving you any credit but I really appreciate your help

It's all good Warrior, I thank you for your kindness but you don't realize that this is how I'm learning all this stuff too (that's why most the time awesome people like Larrow come in and fix my mistakes in the effort to help people like you out and me, a win win).

And yes if you are going to use Larrow's chunk of code you can just copy/paste into your init.sqf.

Share this post


Link to post
Share on other sites

TBH if i was Warrior i would go with something like JShocks example. Its clearer to read and as you come to add bits your not having to fiddle around editing addAction lines.

There was nothing wrong with your code JShock was just surprised that you said it couldn't be done in the action, but i totally understand your excuse :D and is easy to get blindsided when there are so many different ways of doing the same thing. Your all welcome by the way i just like scripting and if helps people to learn to do something then all the better.

Edited by Larrow

Share this post


Link to post
Share on other sites

:D

New questions in the meantime, kind of off topic but I have yet to test the new code atm :S

How do I make it so that an AI doesnt target another AI/Player? I'm getting shot at from what are supposed to be friendlies on another mission for killing too many civs (Grand Theft Altis :P)

And, is it possible to do something like if Player walks near ANY opfor unit then exec something?

Ex. If player > 5m OpFor then Opfor switchMove "melee attack" and Player health - 10?

Thats for something else too 0_o lol.

As for now, Im gonna get working on this hunger system stuff.

Its such a force for me -.- haha.

---------- Post added at 20:42 ---------- Previous post was at 20:26 ----------

Also, I'd like to add something so in case i dont have enough cash I cant buy it. :/

But, just tested it and, tbh no change, my known issue:

I have the money and wtv, I go to buy the food and my food doesnt + 1 and I dont lose my cash. It seems like its not executing the file? ??

Id offer the file up but this is for a workshop mission so...yea.

What have I done adding all this food and hunger, I could have had so many missions done by now D':

Share this post


Link to post
Share on other sites

The easy answer to your questions above is yes. Sorry I can't help out much more than that at the moment (on my phone) and I am going out of town til Sunday, so....yea no access to a computer to test out theories :).

Share this post


Link to post
Share on other sites

I suggest looking at addrating Warrior for that.

Use a killed eventhandler to add to your rating. By default I think you get two friendly or three civ kills before your team mates turn on you.

try something like this to your civ spawn, or in the init in the editor - no idea where you have them. :-)

_civ addeventhandler ["killed", {_this select 1 addrating 2000}];

Share this post


Link to post
Share on other sites

You can pretty much use the code Larrow posted to completely finish your hunger/thirst system (with his permission). I made a few tiny edits

thirst = 100;
hunger = 100;
inv_food = 0;
inv_drinks = 0;
cash = 100;

foodPrice = 5;
drinkPrice = 5;


player addAction ["Buy Food", 
{
if (cash >= foodPrice) then
{
	cash = cash - foodPrice;
	inv_food = inv_food + 1;
	systemChat "You bought some food";
}else
{
	systemChat "You don't have enough money to buy food, you jerk!";
};
}, [], 1, false, true, "", "cash >= 20"];

player addAction ["Buy Drink", 
{
if (cash >= drinkPrice) then
{
	cash = cash - drinkPrice;
	inv_drinks = inv_drinks + 1;
	systemChat "You bought a drink";
}else
{
	systemChat "You don't have enough money to buy a drink, you jerk!";
};
}, [], 1, false, true, "", "cash >= 10"];

player addAction ["Eat",
{
if (inv_food > 0) then
{
	inv_food = inv_food - 1;
	hunger = hunger + 5;
	systemChat "You feel less hungry";
}else
{
	systemChat "You don't have any food to eat, you noob!";
}
}, [], 1, false, true, "", "inv_food > 0"];

player addAction ["Drink",
{
if (inv_drinks > 0) then
{
	inv_drinks = inv_drinks - 1;
	thirst = thirst + 5;
	systemChat "You feel less thirsty";
}else
{
	systemChat "You don't have anything to drink, you noob!";
};
}, [], 1, false, true, "", "inv_drinks > 0"];

//spawns a new thread to take care of hunger/thirst simulation
_simThread = [] spawn
{
while {true} do
{
	sleep 10; //every 10 seconds, lose 5 hunger and thirst
	thirst = thirst - 5;
	hunger = hunger - 5;

	//added code to make sure hunger/thirst can't fall below 0
	if (thirst < 0) then
	{
		thirst = 0;
	};
	if (hunger < 0) then
	{
		hunger = 0;
	};

	if (thirst <= 10) then
	{
		systemChat "You need to drink something before you die of dehydration";
	};
	if (hunger <= 10) then
	{
		systemChat "You need to eat something before you die of starvation";
	};
	if (cash <= 0 && {isNil "outOfCash"}) then
	{
		systemChat "You have spent all your money";
		outOfCash = true;
	};

	/*
	if ( { _x <= 0 }count [hunger, thirst] > 0) then {
	[ "END1", false, 5] call BIS_fnc_endMission;
	};
	*/

	//replaced Larrow's mission end system with a system that damages the player 5% health every cycle
	//(supposedly)(untested) It is now possible to starve to death in your mission.

	//players can still use a First Aid Kit to get (almost) full health to prolong the time until they die
	//You could do something with (health = damage player) to prevent this kind of cheating, but
	//I doubt it matters enough to try to fix that.
	if (hunger <= 0 || thirst <= 0) then
	{
		player setDamage ((damage player) - 0.05);
	};
};
};

_monitorStats = [] spawn {
while {true} do {
	hintSilent format["Thirst\n%1\nHunger\n%2\n\nDrink\n%3\nFood\n%4\n\nCash\n%5", thirst, hunger, inv_drinks, inv_food, cash];
	sleep 1; //there is a small break in this loop to reduce the cost of this thread on the CPU. Also, the player doesn't need 100% accuracy anyway.
};
};

EDIT: After looking at the actions once again, I noticed the extra conditions at the end of each one. They invalidate my "if" statements because you won't even have the action anymore if you don't have enough money, or enough food/drink. Personally, I'd say go with the "if" statements so you don't have to explain to people why their actions aren't showing up anymore.

EDIT2: Realized that hunger/thirst can drop infinitely, making recovery almost impossible in certain cases. Created a floor so hunger/thirst can't get lower than 0 (technically they can, but they get nearly instantly reset to 0). Already edited the code to reflect this change.

EDIT3: Reformatted everything because Larrow uses spaces instead of tabs (shakes fist) :p

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

Wow! It works great!

The only issue is, I use hints as my cellular device, and since that updates every second it kind of interferes with the mission "texts"

if ya know what I mean.

0_o

So, creating a GUI isnt my strong point but I might have to do it ._. ima die.

But other than that it works GREAT! Thanks everyone!

If you have any suggestions on any way to do it other than hint that would be great xD

Share this post


Link to post
Share on other sites
EDIT3: Reformatted everything because Larrow uses spaces instead of tabs (shakes fist)
EW NO Never, i use tabs i think if you C&P from the forum everything is turned to spaces. After testing
 causes TAB->Space where as [code] keeps TABs. test for yourself below, both lines are TABHelloTABWorld.

[code]	Hello	World[/code]


[php]		Hello	World

It annoys the bajeezus out of me aswell, any code i copy for testing from a post i always end up having to reformat. :D

Share this post


Link to post
Share on other sites

@EGW use systemChat or sideChat to give information to your players. This has an added bonus of players being able to look back at them at any time.

It annoys the bajeezus out of me aswell, any code i copy for testing from a post i always end up having to reformat. :D

:p

Share this post


Link to post
Share on other sites
@EGW use systemChat or sideChat to give information to your players. This has an added bonus of players being able to look back at them at any time.

:p

Yea, was thinkin about usin command chat "since its yellow"

but I cant seem to copy the bottom half and use an add action to display it. 0_0 done for today but will check up again tomorrow.

thx for the advice

Share this post


Link to post
Share on other sites

Don't worry about trying to display chat. Players can press / to look at past messages (I think this only works in multiplayer though)

Share this post


Link to post
Share on other sites

That doesn't necessarily make sense but, it is ok sir I will attempt it in a bit, new update for Red Orchestra got me excited. xD

I wanna make zombies to this too.

No, Im not doing a dayz replica, just a minigame inside mission, but it sounds like it doesnt it haha.

Share this post


Link to post
Share on other sites
That doesn't necessarily make sense but, it is ok sir I will attempt it in a bit, new update for Red Orchestra got me excited. xD

I wanna make zombies to this too.

No, Im not doing a dayz replica, just a minigame inside mission, but it sounds like it doesnt it haha.

Possible zombies?: http://forums.bistudio.com/showthread.php?181485-Infected-Script-by-Gulozwood

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  

×