- Home /
Making a hunger script
Hello again,
So I'm making a hunger script that's going relatively ok except for one small hiccup. It's essentially my health script in reverse, meaning that for every step, hunger increases, with the intention that when hunger reaches a certain point, the player's health will start to decrease. So, I'm halfway there. Hunger definitely increases with each step (rate of increase and cap level of when starvation occurs is currently irrelevant to the issue, I'll adjust those later and they're intentionally set low right now to test concept) but once max hunger is reached, nothing happens to player health. (Which is actually called from the Playerhealth script, specifically Playerhealth.curHealth) Am hoping someone might be able to point me in the right direction as to how to implement this properly. Bonus points for helping me learn how to place a simple text message at different benchmarks, (hunger @ 10 = "You should get a snack" hunger at 20 "you're starving" etc) though that's not necessarily my main focus yet. Thanks, and God bless.
static var curHunger : int = 0;
var maxHunger : int = 35;
var hungertext : GUIText;
function Start ()
{
hungerRegen();
}
function Update ()
{
hungertext.text = "hunger " + curHunger + " / " + maxHunger;
}
if(curHunger == 35 )
{
Playerhealth.curHealth -= 10;
}
if(curHunger > maxHunger)
{
curHunger = maxHunger;
}
function hungerRegen ()
{
for(i=1;i>0;i++)
{
yield WaitForSeconds(10.0);
if(curHunger < maxHunger)
{
curHunger++;
}
}
}
Why is your if(curHunger == 35 )
outside of any function? Put this and the if
statement that follows inside your hungerRegen()
and it should work.
Danny, thanks for the advice. For some reason I didn't get email notification, that's why it took me so long to check this out. I hate to admit this, but I tried every which way to implement this, and still nothing. I completely confess that this has nothing to do with your advice, and everything to do with user error. I'm trying to learn on the fly, and sometimes things work, sometimes it takes me a bit longer to pick up concepts, for which I apologize. While I'm just starting to grasp js, a lot still (and will for a long time) elude me. I definitely see what you mean about putting the if(curhunger) statement outside of any function, but am trying to grasp which hungerRegen to put it under, the first one, or the function update? And when you say the following if statement, I am wondering if you mean just the entirety of the if curHunger == 35 along with "PlayerHealth.curHealth," or if you're also meaning the curHunger > $$anonymous$$axHunger below it. I am so sorry, I know these are noob questions, and I am really trying to learn and not be a knowledge mooch here.
First thing you should learn when coding in SCOP$$anonymous$$ ex.
if(myBool) DoSomething();
DoAnotherThing();
The method call 'DoAnotherThing' is not in the same scope as 'DoSomething' in relation to the condition 'if myBool is true'.
This is what @DannyB is referring to, except in the context of a method/function.
function Update()
{
DoSomething();
//Code cannot be outside of function brackets
}
if(someCondition) // this will cause a compiler error
There are some exceptions to that rule, all of which should be at the top of your script. For example: Global Variables, statements such as #pragma, and using/import statements ( i may be forgetting some ).
I suggest learning c# as it is much more strict (much more difficult to learn at first), thus you will learn the proper way to code from the start, vs. trying to unlearn nasty habits :) But there is nothing wrong with javascript, there is just more support for c# across the board and encourages better coding.
about using C#- it is in general less confusing than UnityScript, not to mention generally better supported. In UnityScript, any code that is written outside of any functions is considered to be inside what would be in C# the 'Awake' function- that is to say, it will execute once, when the object is loaded, and then never again. This means that if you are checking the player's current hunger in 'Awake' it will never be above zero!
EDIT I just cleaned up the code in the original question, and the problem is suddenly much more obvious. $$anonymous$$ake sure you put the conditionals that check current hunger level inside of Update- otherwise they won't get checked every frame!
for the additional line "any code that is written outside of any functions is considered to be inside what would be in C# the 'Awake'"
1 more note before i leave you to your tasks.
C# and javascript use such a similar syntax that it is better to learn c# now, then migrate to js later rather than vice versa, just because you wont be tempted to develop bad habits. This is the most common 'nasty habit' i experience when converting other peoples javascript/unityscript
function Update()
{
var i;
var u;
// hundreds of lines of code!!
}
Looking at this code, it is impossible to deter$$anonymous$$e what the type of i & u are without digging through the additional hundreds of lines of code. Even then, you need very good intellisense or tracking skills to figure the proper types of those variables.
Answer by Conect11 · Sep 23, 2013 at 04:14 PM
Answered through pieces together lessons learned in comments. Here's what the script looks like:
static var curHunger : int = 0;
var maxHunger : int = 35;
var hungertext : GUIText;
function Start ()
{
hungerRegen();
}
function Update ()
{
hungertext.text = "hunger " + curHunger + " / " + maxHunger;
if(curHunger == 35 )
Playerhealth.curHealth -= 1;
}
if(curHunger > maxHunger)
{
curHunger = maxHunger;
}
function hungerRegen ()
{
for(i=1;i>0;i++)
{
yield WaitForSeconds(10.0);
if(curHunger < maxHunger)
{
curHunger++;
}
}
}
Note: speed of health loss has not been adjusted yet.
Your answer
Follow this Question
Related Questions
this is part of my health script it gives me a error unknown identifier GameScore pls help thxxx 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problem with Pause and Pause Menu Script 1 Answer
update highscore if current is higher than previous score 0 Answers
Heath system need help 1 Answer