(Solved) help with Global variables, please

Started by BlueAngel, Thu 02/12/2010 15:09:53

Previous topic - Next topic

BlueAngel

Hello
I need help with Global variables, please. This is the first I have made.

I have made a global variable named Hunger (int) with a value of 100.

In function room_RepExec() I got this code:

Code: ags

function room_RepExec()
{
  if(Hunger>150)
  cEgo.LockView(3);
  cEgo.Animate(0, 5, eOnce, eBlock);
  Hunger-=50;
  cEgo.UnlockView();
}


Now I want the player to do a animating if his hunger is greater than 100 and the stop. But as soon as the game starts the player starts walking and hunger is counting down.
If I put:

Code: ags

function room_RepExec()
{
  if(Hunger<150)
  cEgo.LockView(3);
  cEgo.Animate(0, 5, eOnce, eBlock);
  Hunger-=50;
  cEgo.UnlockView();
}


… the player does the animating endlessly and hunger is counting down. What do I wrong?


Gilbert

I don't quite understand what you want.

Are there any other places where Hunger would be modified that you have not posted here? Otherwise both sets of codes won't work for obvious reasons.

Note that Hunger is only continuously decreased in game (and in a VERY fast rate too, given its initial value of 100 it will reach 0 in 2 frames since the count-down starts, which would be only 1/20 second under default settings) and in your initial set of codes you check whether it will be larger than 150 at some point of the game, which would never happen as its initial value (100) is already smaller than 150 and it's just being decreased over time.

The next set of codes won't work either, as the variable is always smaller than 150 during the whole game, so the animation will only be played over, over and over again.

Khris

I guess the problem is the missing brackets:

Code: ags

function room_RepExec()
{
  if(Hunger>150)
  {                        // <- open command block
    cEgo.LockView(3);
    cEgo.Animate(0, 5, eOnce, eBlock);
    Hunger-=50;
    cEgo.UnlockView();
  }                        //  <- close command block
}


With your code, only cEgo.LockView(3); was subject to the condition; the other commands were executed every game loop regardless of the value of Hunger.

Also, you'll probably want to move code like that to the global repeatedly_execute. Otherwise you'd have to put it in every room.

BlueAngel

Thanks it was the missing brackets that did it!  :D
I knew it was something simple  ::)

(Off topic, is it only me that’s having trouble logging in to the forum this week?)

SMF spam blocked by CleanTalk