Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Builder17 on December 09, 2017, 03:04:45 AM

Title: Turn based game exceeding max loop instructions
Post by: Builder17 on December 09, 2017, 03:04:45 AM
I am probably missing some obvious way how to fix this code. It crashes game when run.

Edit: Fixed it using lists from PAC AI.
Title: Re: Turn based game exceeding max loop instructions
Post by: GoodMorning on December 09, 2017, 05:01:58 AM
This:

999999 0 do


That's a crash by itself, without a break.
Title: Re: Turn based game exceeding max loop instructions
Post by: Builder17 on December 09, 2017, 05:22:18 AM
I see, thanks. That is intended to make code under it repeat forever until condition is reached, any better way to make that?
Title: Re: Turn based game exceeding max loop instructions
Post by: Karsten75 on December 09, 2017, 08:37:48 AM
Quote from: Builder17 on December 09, 2017, 05:22:18 AM
I see, thanks. That is intended to make code under it repeat forever until condition is reached, any better way to make that?
Remember - each CRPL core is scheduled to run on every frame of the game - that's 30 times a second. If this one core tries to "repeat forever", then nothing else gets to run and the game won't advance another frame.
Title: Re: Turn based game exceeding max loop instructions
Post by: Builder17 on December 09, 2017, 08:55:18 AM
Whoops.
Title: Re: Turn based game exceeding max loop instructions
Post by: kajacx on December 09, 2017, 09:20:42 AM
I use

while true repeat

endwhile

sometimes, and yes, I also often forget to break inside the loop :D Thankfully you will get the "execution limit of 1 000 000 isntruction exceeded" error that is often caused by an infinite loop, so it's not that hard to debug.
Title: Re: Turn based game exceeding max loop instructions
Post by: Builder17 on December 09, 2017, 09:33:43 AM
Quote from: kajacx on December 09, 2017, 09:20:42 AM
I use

while true repeat

endwhile

sometimes, and yes, I also often forget to break inside the loop :D Thankfully you will get the "execution limit of 1 000 000 isntruction exceeded" error that is often caused by an infinite loop, so it's not that hard to debug.

Sounds that this might work, thanks.
Title: Re: Turn based game exceeding max loop instructions
Post by: GoodMorning on December 09, 2017, 05:38:31 PM
Keep in mind - because CRPL for a frame needs to finish running before the game can update the screen, you have to terminate the script execution regularly.

Why not just run the inside of that loop once each frame?
Title: Re: Turn based game exceeding max loop instructions
Post by: kajacx on December 09, 2017, 07:35:25 PM
Quote from: GoodMorning on December 09, 2017, 05:38:31 PM
Why not just run the inside of that loop once each frame?

Is that possible? From what I tested in PRPL, 1 Delay will technicaly sleep for 2 frames (it will stop the execution on the current frame, then sleep on the next frame, and then resume the execution on the next frame), so that your code executes only every other frame and 0 Delay does nothing.

This was quite anoyying since I needed to execute code inside a loop every frame, so I had to do it every other frame instead which had some unfortunate consequences.
Title: Re: Turn based game exceeding max loop instructions
Post by: GoodMorning on December 09, 2017, 07:42:02 PM
I mean...


<-InLoop not if
#Pre
endif

<-InLoop if
#Loop body
endif

<-InLoop if return endif

#Rest of script


...but well-structured code should be able to update every frame in the natural way. Loops over multiple frames are a sign of bad code, and a Delay inside a loop can (will) break on save/load, possibly causing a crash due to stack-context clearing after :awake and :GameLoaded.
Title: Re: Turn based game exceeding max loop instructions [Solved]
Post by: Builder17 on December 10, 2017, 12:13:17 AM
Fixed this one already, new script attached to first post!
Title: Re: Turn based game exceeding max loop instructions
Post by: kajacx on December 10, 2017, 07:54:27 AM
Quote from: GoodMorning on December 09, 2017, 07:42:02 PM
a Delay inside a loop can (will) break on save/load, possibly causing a crash due to stack-context clearing after :awake and :GameLoaded.

Hm, I never knew that. Btw your solution looks interesting, might use it if I even needed. however the one time I needed to sleep inside a loop was when I wrote the PRPL debugger, it worked by adding a function call before every single command that would possibly pause the code execution unit the user took some action.

It was real fun, but I had to listen for mouse click every single frame to catch the user clicking on a button, in the end I didn't use the "GetMouseDown" and checked the change manually with "GetMouse", however for the future I have planned key input as well to modify the stack mid - execution with arbitary commands, and doing this with key input as well will be quitre a distaster, but hey, at least it would work.