Hi, I want to know if anybody has implemented event queue on smilebasic. I did that before for my megaman battle network clone but I want to know if there is a better implementation.
Here is my code.(Warning: this include Lowerdash syntax)
Spoiler
MODULE EVENTS VAR curFrame VAR FRAME[0] VAR FUN$[0] VAR ARG$[0] 'Function triggered when a new screen is created. STATIC DEF onCreate_ EVENTS_reset END 'Function triggered when the screen is destroyed. STATIC DEF onDestroy_ EVENTS_reset END STATIC DEF update_ INC _.curFrame WHILE LEN(_.FRAME)>0 && _.FRAME[0]<_.curFrame EVENTS_exec WEND END 'Reset the values of this module STATIC DEF reset _.curFrame=0 _.FRAME=UTIL_malloc(0,"") _.FUN$=UTIL_malloc(0,"$") _.ARG$=UTIL_malloc(0,"$") END 'put a value on the argument variable STATIC DEF put ARG$,V VAR T=typeOF(V) VAR Q$=CHR$(34) 'Q$=" IF LEN(ARG$)>0 THEN INC ARG$,"," 'IF T==2 THEN V is a string. Else V is a number. IF T==2 THEN INC ARG$,Q$+V+Q$ ELSE INC ARG$,STR$(V) ENDIF END 'Add an event in the queue STATIC DEF add F,F$,A$ PUSH _.Frame,F + _.curFrame PUSH _.FUN$,F$ PUSH _.ARG$,A$ SORT _.Frame,_.FUN$,_.ARG$ END 'Use the page 1 to execute an event. STATIC DEF exec VAR F$,V$ EAT SHIFT(_.Frame) F$=SHIFT(_.FUN$) A$=SHIFT(_.ARG$) PRGEDIT 1 PRGDEL -1 PRGSET F$+" "+A$ EXEC 1 ENDUsage
EVENTS_onCreate_ 'Print hello world after 100 frames VAR ARG$="" EVENTS_put ARG$,"hello world" EVENTS_add 100,""PRINT",ARG$ 'Make a sound after 200 frames ARG$="" EVENTS_put ARG$,10 EVENTS_add 200,"BEEP",ARG$ ' "game loop" WHILE 1 EVENTS_update_ VSYNC WEND