- Edited
Okay, just to give a bit of background, now that I've got a basic idea of my overall game loop I've been experimenting with making sure that blocks of code I execute only execute once. I looked up the idea of booleans and integers being used as stoppers to prevent code from executing multiple times which I have seen happen thanks to some debugging with print but also when switching up my environments they would add 16 times or so which I think is due to the code executing over multiple frames.
To confirm this I put in an execution stopper using a simple integer like how some people do with booleans. This works really well considering and I'm pretty pleased with the results as when the in-game clock ticks over to the correct time everything executes once and only once and this has happened each time I've run the game which is going to help optimise my AI and in-game day/night cycle a lot. However, one thing I am concerned about is the idea of using an if not statement to reset the stopper once the code has executed. Is there a better way of optimising this code? Or is the resources usage from the integer being reset to 0 constantly when the if statements aren't being executed so negligible it's not worth worrying about?
if hours == 7 && minutes == 0 && seconds == 0 && executionOrderStopper == 0:
SetDay()
godHandNightLight.visible = false
isNight = false
isDay = true
executionOrderStopper = 1
if not hours == 8 && minutes == 0 && seconds == 0 && executionOrderStopper == 1:
executionOrderStopper = 0
if hours == 8 && minutes == 0 && seconds == 0 && executionOrderStopper == 0:
SetNight()
godHandNightLight.visible = true
isNight = true
isDay = false
executionOrderStopper = 1
if not hours == 8 && minutes == 0 && seconds == 0 && executionOrderStopper == 1:
executionOrderStopper = 0
if dayNightTimer.hours == 8 && dayNightTimer.minutes == 0 && dayNightTimer.seconds == 0 && executionOrderStopper == 0:
isSleeping = true
isWondering = false
isWalking = false
speed = 0
villagerStatusText.text = "Sleeping"
MaleVillagerSleepingAnimation()
print ("Sleep Executed")
executionOrderStopper = 1
if not dayNightTimer.hours == 8 && dayNightTimer.minutes == 0 && dayNightTimer.seconds == 0 && executionOrderStopper == 1:
executionOrderStopper = 0
print ("Sleep Code Stopped")
if dayNightTimer.hours == 7 && dayNightTimer.minutes == 0 && dayNightTimer.seconds == 0 && executionOrderStopper == 0:
MaleVillagerGettingUpFromSleepingAnimation()
isSleeping = false
isWondering = true
isWalking = true
speed = 500
villagerStatusText.text = "Wondering"
hasAnimationPlayed = false
executionOrderStopper = 1
if not dayNightTimer.hours == 7 && dayNightTimer.minutes == 0 && dayNightTimer.seconds == 0 && executionOrderStopper == 1:
executionOrderStopper = 0
Edit: Although now I think about it I should just put a print to debug the if not statement and double check if it is indeed doing an infinite loop.
Edit 2: Hmm, yep resetting the executionOrderStopper is an obstacle for me right now, the code works best without it being reset so that's frustrating, any if statements I put in instantly switch the integer which then executes the code again for those small amounts of frames the time is equal to what I've written. I'm probably going to have to put something extra in to make sure the clock has gone past but if not isn't really doing it.