Author Topic: @MainLoop Tutorial for newbies  (Read 2026 times)

Matteo

@MainLoop Tutorial for newbies
« on: May 03, 2012, 01:15:09 PM »
Hi all, you know the big question about scripts: mainloop style or tasks? I'm going to share my little knowlege. This tutorial is for beginners, i made it simple and essential in order to help you understand the basics.

I learned first tasks, than mainloop and finally i mixed both. But in order to learn basics of danmakufu i found "mainloop" an excellent teacher. By messing around it "teached" me what to do and what not, from simple crashes to complete freezes of the game  ::)

Oh, also forgive me:

1- for my bad english

2- if you read many italian words, that because i wrote memo on the script in order to remember how did i do the script.


So... this is the script:          -------------------         http://pastebin.com/Y6skaUsV   -----------------------


First thing to do in @MainLoop style scripts is to set the value. MainLoop works on "if value is.. < or > or == ... THAN what you write inside { } HAPPENS.

To set value as you know you have to write in "script_enemy_main" :

let "value" = number

MainLoop read "value" and do with it what you say to do. First thing to remember is to not think that only some value are admitted. For example, as the time flow, don't think that you can keep track of time only with values called "time" or "timer" or "frame".

No, you should think that in danmakufu time is freezed. You tell the engine that it should flow. How? With values.

Let's look the script.

i started setting the values:

        let frame = 0;
        let angle = 0;
        let lollo = 360;
        let angle= 0;
        let brollo = 500;

Why stupid names like lollo or brollo you may say? Simple, to show you that any name is a value for danmaku if you write "let"<space>"value". You can write "let supercalifragilistichespiralitoso" and danmakufu will agree with you. (But you will fell stupid for writing that, so try avoiding that).

Now, time is still freezed but in this universe called danmakufu it has a name: time is called "frame" "lollo" and "bollo". (angle is used for another purpose!).

Now you must say to time that it has to flow. So at the start of Mainloop you write "frame++;" or "lollo++;" or "bollo++;" choose what you like. I started with frame++; (even if i put it later in the script...my fault  :V).

Remember, put the "value++;" in the mainloop and not inside a if statement!!! That means 2 different things!

----------------------------------------

Writing frame++; time started to flow with a +1, remember that 60 = 1 second.

So if you want to do something at 2 seconds after the start of the script, you write  if("value" > 120){bullets, movements, bla bla bla...you choose what to do } .
If you write if("value" > 2), action start almost suddenly WRONG.

I wrote for example that bullets are shot if frame is 60 and if frame is 120. To repeat the action at the end of the "if...120" put frame = 0;  . That set frame at 0 but since you have "frame++;" in mainloop time will keep flowing so frame will do a loop... starting from 0 it increase until it reaches 120, then it return to 0 and so on. How to break the loop? Simple, set in another "if" "frame = 121" for example. The loop happens only if frame reach 120 but if you set frame = 121, frame will keep increasing allowing you to use again if statements with variable "frame".

REMEMBER, in "if" statement equal is written == not =. If you write = , game will crash. Also as Blargel and Naut and Hellepolis say, danmakufu is case sensitive so if you write let frame and then "if Frame..." the game will crash.

------------------------------------------------------------------------

Time in this universe flow with different names, and after "frame" i introduce you his brother "lollo". lollo flow exactly in the same way, you say it how to flow. I showed you a little trick.

First, let lollo = 360; this will set and freeze lollo at 360 (that are 6 seconds if we talk about time).

Than we set in Mainloop lollo--; This will keep lollo decreasing. And now the if!

if( lollo>180 && lollo<360){ -----> if time is between (there is a h somewhere in the between word, i wonder wehere...) 180 and 360, something happens.

if( lollo<180){ ------> means that lollo is decreasing, when it reaches 180 another action took place.

BIG NOTE with == it happens 1 time, with < or > it happens for each frame till the end of the statement.

if( lollo == 0){ ------> as you see, i did as before lollo = 360; .  Now we have lollo and frame that keep looping unless we do something.

--------------------------------------------------------------------------
Now i'm explaining faster...

Ok, now let's introduce "brollo" ! ;)

let brollo = 500;
brollo++;

i wrote a nice if:

if(brollo == 1000){
                lollo = 100000;
}

That means, we have brollo at 500 frames, we set brollo++; so it keep increasing and after 500 frames (more or less 7 seconds after the start of the script) we set lollo at 100000! This insanely big number means that lollo is disabled because it is bigger then 360 and action involving lollo start when lollo is < than 360.

------------------------------------------------------------------

Now that you have the basics, try to tell me what did i made in this statement:

if(brollo == 2000){
                lollo=361;
                brollo=500;
                }

 :V :V :V :V :V

-------------------------------------------------------------------------------------

OK, this cover the basis of mainloop scripting... this is the INFRASTRUCTURE of the mainloop.

Remember, more value you use more hard it becomes to keep track of them!!!!! (That's why tasks come in handy with mainloop)

Now, to better understand you can try the script.

-------------------------------------------------------------------------

I hope this will be usefull.

Matteo
« Last Edit: May 03, 2012, 01:20:36 PM by Matteo »