How To

is there anything like a “WAIT UNTIL” statement?

2

DJMoffinz 2022-06-08 09:21

hi guys, i’m back sorta.
i’ve been making music using the sound composer recently, and want to make it so it plays when the program is run, which for some reason is slightly more complicated than just going “MUSIC 0”.

i currently have a quite cumbersome way of achieving this, by calling MUSIC 0 and then having an empty REPEAT UNTIL loop, with the condition being MUSIC(2)=song length in ticks.

this is quite resource intensive, which kind of doesn’t really matter seeing as it’s only music, but i like optimised code, and was wondering if there was a better way of doing this.


was8bit 2022-06-08 11:19 (Edited)

While in SONG EDITOR, in the first pattern page, tap the ARROWS until the left arrow which points to the right is highlights... then in your last pattern, tap ARROWS until the right arrow which points to the left highlights...

Now one command MUSIC will play forever :)

See my example...

Forever music.nx | Open in app
2022-06-08 11:19

Timo 2022-06-08 11:24

In any case, if you need a loop waiting for something, put a WAIT VBL inside. Otherwise it can run several thousands times per frame.


was8bit 2022-06-08 11:30

Here is an advanced example... TAP the screen to toggle between the 2 songs... and also note that the game can still have sound effects while the music plays ;)


was8bit 2022-06-08 12:03 (Edited)

Its also best to have one main game loop

DO
game code
WAIT VBL
LOOP


then use

IF
condition
THEN
do code
END IF

to prevent the game from halting to a stop until something happens...

You can use multiple main loops, like one for game intro with options, one for thr main game, one for a menu controller, one for end of game with options...

As Timo mentioned, be very careful how you use loops...

I might use WHILE WEND for something like finding an empty spot on the screen... but best practice would include a time-out feature so as not to hang the code infinitely... example..

ILOOK=0
C=1
WHILE C>0
X=RND(19)
Y=RND(15)
C=CELL.C(X,Y)
INC ILOOK
IF ILOOK=100 THEN C=-1
WEND
IF C=-1 THEN
PRINT "ERROR 1"
END
ELSE
game code
END IF

Now, my loop doesnt use WAIT VBL ... however it can catch a runnaway loop, and in all likelyhood will only run the loop a few times in most cases... and if my code is bad and it would have hung, but my code cuts out after 100 runs and posts me a warning ;) alternatively, your cutout code might not post a warning, it might do something else, but the idea is that you cannnot allow a loop to run excessively, IF your loop can or does run excessively, you must either have a cutout feature, or at least as Timo said, add a WAIT VBL which slows it down to 60 loops/second


DJMoffinz 2022-06-08 22:36

thank you so much, both of you. all of this is really useful!


was8bit 2022-06-09 07:41

:)


Log in to reply.