repeatedly execute issue, is there a better way?

Started by MrAbu, Sat 05/08/2017 02:05:25

Previous topic - Next topic

MrAbu

So what I wanted to do was set up a dialog when the player got a certain score, I thought the best way to do this was to make a function that repeatedly checks to see if the player reached the score. However, when the score is reached it only does the first part (the player.Say part) and then keeps repeating that one part. How do I make it do both parts? Was the repeatedly execute function the wrong way to go?

Code: ags

 function repeatedly_execute()
{
 if (act1score == 3){
 player.Say("I should get ready to go."); 
 dendact1.Start();
 }
}

Kara Jo Kalinowski

The problem you are having is that after that if statement resolves, act1score is still 3, so setting it to 4 inside the if statement will fix your problem. There are also other ways you could do it if that's a problem, but repeatedly_execute checks every game loop even after your if statement resolves.

Crimson Wizard

Game.DoOnceOnly function is for making sure something happens just once.

For example:
Code: ags

// this will happen only if act1score is 3, and "Act1Score3" tag was never checked before
if (act1score == 3 && Game.DoOnceOnly("Act1Score3"))

MrAbu

thanks! the Morgan's way worked and fixed the problem but the DoOnceOnly method did too, thanks both of you!

Khris

#4
An even better solution is to simply check right after changing the score.
Code: ags
  // something happened
  act1score++;
  CheckGetReady();


Edit: one can improve on that, btw.:
Code: ags
function ScoreUp(int act) {
  my_score[act]++;
  // special stuff
  if (act == 1 && my_score[1] == 3) {
    player.Say("I should get ready to go."); 
    dendact1.Start();
  }
}


Now one simply calls ScoreUp(1);

Crimson Wizard

#5
Quote from: Khris on Sat 05/08/2017 17:08:21
An even better solution is to simply check right after changing the score.

Yes, that's actually true. I noticed alot of people tend to do all the checks in repeatedly execute. My guess is that's simplier to just put everything there, instead of noting all the places where variables can be modified. Latter requires a good effort on systematizing the game logic, but it really pays off in the long run, especially with large projects.

MrAbu

Ohhhhh!!! that's sick! Thanks for all the different options! I'm currently working on a game while also learning how to make it and it's really fun to learn all this new stuff. and you guys are helping immensely. Thanks!

SMF spam blocked by CleanTalk