SOLVED: Stop counter going down / showing beyond 0 when saying etc

Started by barefoot, Thu 23/06/2011 06:48:14

Previous topic - Next topic

barefoot

Hi

I have a counter which when reaches 0 proceeds with events. I have it in rep exec ==0.

My problem is that if you have say it still counts down ie -1 -2 before events happen. I need it to stop once it reaches 0 and the events to follow. It runs ok if you are not saying anything etc. I even have  LScore.Visible=false; but you still see it.

This is what i have in rep exec:

Code: ags

if (game.score == 0){
	 
	 LScore.Visible=false;
	 object[12].SetView(19, 0, 0);
	 object[12].Animate(0, 3, eRepeat, eNoBlock);
	 cFrankj.FaceLocation(185,126);
	 aExplodes.Play();
	 aGameover.Play();
	 aGameover.Stop();
	 cFrankj.LockView(29);
	 object[5].Visible = true;
	 object[8].Visible = true;
	 object[37].Visible = true;
	 cbjorn.Say("Your time is up. Boom!!");
         cFrankj.Animate(0, 4, eOnce, eBlock);
	 Wait(20);
	 object[34].Visible = false;
         Display("You have been blown to pieces!");
	 SetFadeColor(200, 0, 0);
	 cFrankj.ChangeRoom(3);
	 
}


Help appreciated.

barefoot

I May Not Be Perfect but I Have A Big Heart ..

Khris

1. Don't use game.score all the time. Just create a global variable and use that.

2. I don't know how you're changing game.score (you didn't post that part; why, I do not know) but assuming that one can write directly to game.score:

Code: ags
  if (game.score > 0) game.score--;


Seriously, again, does it get more straightforward than this?

Only decrement it if it is greater than zero. Simple as fuck.

monkey0506

#2
Where are you actually setting the player's score? If the player (or any other character) is speaking after you set the score (but before that function finishes) then repeatedly_execute won't have a chance to run until that function is finished.

Seeing as you're using the score though you don't even need to use repeatedly_execute for this. Use on_event instead:

Code: ags
function on_event(EventType event, int data)
{
  if (event == eEventGotScore)
  {
    // your code here
  }
}


I'd have copy/pasted your code myself but I'm on my phone. Anyway, I'm pretty sure on_event gets called before GiveScore finishes, otherwise this would have the same problem. If it does, then you should use repeatedly_execute_always instead.

Edit: Khris posted while I was typing, and raises a couple of very valid points. Like questioning why you're even using the score for this.

Also, regardless how you're actually setting the score (or any other variable), it's not that hard to figure out how to check a variable's value prior to decrementing it. Oh, and my code here only applies to setting the score via GiveScore.

barefoot

Thanks for your help guys.

I have created a Global variable... score, int, 300.     it works fine except it does not continue the countdown through 'Says'  like it did using Give Score method (as per monkey's comment). It now appears blocked.

Code: ags

function repeatedly_execute_always()
{
	
	 LScore.Text = String.Format("Seconds remaining: %d", score);

}


Code: ags

function room_RepExec()
{

  if (IsTimerExpired(1) == 1) {
	
  score=(score -1);
	SetTimer(1, 40);
 {
		
		
 if (score  == 0){
	
	object[12].SetView(19, 0, 0);
	 object[12].Animate(0, 3, eRepeat, eNoBlock);
	 cFrankj.FaceLocation(185,126);
	  cbjorn.Say("Your time is up. Boom!!");
	 aExplodes.Play();
	 aGameover.Play();
	 aGameover.Stop();
	 cFrankj.LockView(29);
	 object[5].Visible = true;
	 object[8].Visible = true;
	 object[37].Visible = true;
        cFrankj.Animate(0, 4, eOnce, eBlock);
	 Wait(8);
	 object[34].Visible = false;
	  Display("You have been blown to pieces!");
	 SetFadeColor(200, 0, 0);
	 cFrankj.ChangeRoom(3);



My venture into variables is at an early stage so kid gloves would be preferable.

cheers

barefoot





I May Not Be Perfect but I Have A Big Heart ..

Khris

You already know about repeatedly_execute_always.
So the solution is to decrement score and update the label in there.

Code: ags
function game_start() {

  score = 300 * GetGameSpeed();
}


Code: ags
function repeatedly_execute_always()
{
  if (score > 0) {
    score--;
    LScore.Text = String.Format("Seconds remaining: %d", score/GetGameSpeed());
  }
}


I'm using score to count frames, then I won't have to use a timer.

You can leave your room_RepExec as it is, just remove the first of block (the one that handles the Timer).

monkey0506

You're still mixing up opening and closing braces. But I imagine that's just a typo not actually in the code.

If you want the score variable to update during blocking functions then the normal or room rep ex functions will not work. You must use rep ex always. So just move the code that updates the variable..not everything..just those few lines..into rep ex always.

Khris beat me again. Bah-humbug. :P

barefoot

I May Not Be Perfect but I Have A Big Heart ..

Khris

In case this isn't clear: you can have a repeatedly_execute_always in your room script, it doesn't have to be in Global.asc.

barefoot

Hi

sorry to be a pain (lad in classroom who sticks his hand up  8) )

I have created a Global Pane variable: score, int, 300...

Countdown after every game loop by timer on rep exec from 300 to 0 with a label updating.. So far so good..

I want/need to put a closure on this as I'm still a little confused.com as it still goes over the 0 (if 'talking' it keeps counting down passed 0 ie -1 -3 etc)

Once 'talking' has stopped it then proceeds to events as it's supposed to do.

I need to nail the counter to stop and label off when score reaches 0.

I'm unsure about how to implementing monkey's on event method and feel like my feet are in toffee... but thanks for help so far guys..


Hopefully there is a layman's way of dealing with this crises.

In room_RepExec:

Code: ags

function room_RepExec()
 {
  
 if (IsTimerExpired(1) == 1) {
   score= (score -1);
   SetTimer(1, 40);


{
	
if (score  <  0){
  score--;
   SetTimer(1, 0);
   Display("Times up!");
   LScore.Visible=false;
   object[12].SetView(19, 0, 0);
  object[12].Animate(0, 3, eRepeat, eNoBlock);
  cFrankj.FaceLocation(185,126);
  aGameover.Play(); 
  aExplodes.Play();
  cbjorn.Say("Your time is now up. Boom!!");
  aGameover.Stop();
  cFrankj.LockView(29);
  object[5].Visible = true;
  object[8].Visible = true;
  object[37].Visible = true;
  cFrankj.Animate(0, 4, eOnce, eBlock);
  Wait(8);
  object[34].Visible = false;
  Display("You have been blown to pieces!");
  SetFadeColor(200, 0, 0);
  cFrankj.ChangeRoom(3);

 }
 }
 }


Then in repeatedly_execute_always()

Code: ags

function repeatedly_execute_always()
	 
{
if (IsTimerExpired(1) == 1) {
	 score= (score -1);
	 SetTimer(1, 40);
	 
}
	

  LScore.Text = String.Format("Seconds remaining: %d", score);

}


Sorry if I give you a headache  :=

barefoot





I May Not Be Perfect but I Have A Big Heart ..

Khris

Really? Are we starting all over again?

Alright:
Quote from: Khris on Thu 23/06/2011 10:14:42
Only decrement it if it is greater than zero. Simple as fuck.

monkey0506

#10
Do me a favor. Get a pen and paper. Go ahead and assume that score is 5. There's no characters speaking. Now go through each line of your code, and keep track on the paper every time score is changed. Don't worry about anything else going on in the game, just keep looping through your room_RepExec and repeatedly_execute_always functions.

Now after you've done that and score has reached -1 and then -2 and then -3 in the same game loop, set score back to 5, but this time assume a character is speaking. That means room_RepExec won't be running at all, but you can still loop through repeatedly_execute_always.

When score again reaches -1 (but not -2 this loop) then you can stop.

If you can't see the huge, glaring, Grand Canyon of a hole in your logic after this exercise, then I honestly don't believe you will ever be able to solve this type of logic problem yourself, so I'll oblige and give you the answer, but I'm willing to give you a chance to figure it out yourself.

HINT: Khris already gave you the solution three times.

Edit: Khris is a friggin post-ninja!

barefoot

I May Not Be Perfect but I Have A Big Heart ..

monkey0506


SMF spam blocked by CleanTalk