Best way to use functions?
Blitz3D Forums/Blitz3D Programming/Best way to use functions?
| ||
i am planing to write a game with the following structure: 1. graphic setup 2. globals 3. constants 4. main loop call_function1 ; keyboard config call_function2 ; map loading call_function3 ; setup collisions call_function4; conditions for finishing the current map call_function5; highscore 5. functions (function1, function2, function3, etc.) the idea is to use only call to funcionts in the "main loop area". can somebody tell me if it is good structure or make an adjustment to this strcuture? thanks a lot! B! |
| ||
I guess there's no hard and fast rules but consistency makes development quicker. I generally use this kind of format with all globals / consts & types declared at the top of the code. This snippet is from the source to my tanks game .. All the functions are declared after the main loop. GAMEinit() Repeat GAMEstart() Repeat WaitTimer(FrameTimer) BULLETSupdate() PARTICLESupdate() BONUSupdate() TANKSupdate() UpdateWorld() RenderWorld() Delay 5 Flip Until QUIT Or GAMEOVER If GAMEOVER GAMEend() Until (1=2) |
| ||
Repeat Until 1=2... wtf? |
| ||
Same as forever .. just didn't know that keyword existed at the time of coding! |
| ||
That structure looks fine. Personally I prefer to keep my main loop really short and have everything happen within functions, but it doesn't really make any difference. I mean, in my way I'd only call one function in the main loop, but that one function calls a bunch of other functions, so it's really the same thing. I suppose the one advantage to my approach is that you can use a Select/Case to determine which batch of functions to go to depending on the game's state. In other words, the main loop would determine if the game is on the main menu, loading a level, or playing a level, and go to the appropriate function for those updates. pseudocode While Not KeyHit(1) Select gameState Case 1 UpdateMenu() Case 2 LoadLevel() Case 3 UpdateGameplay() End Select Wend Function UpdateMenu() stuff for main menu if Start selected then gameState = 2 End Function Function LoadLevel() load assets for level gameState = 3 End Function Function UpdateGameplay() handle input and AI and whatnot if the player loses all their lives then gameState = 1 End Function |
| ||
I tend to put the following in main loops (note that it does differ very much between projects; this one used a Windows API GUI, so input was fairly complicated): UpdateInput UpdateObjects Render UpdateFrameLimiter Your structure looks fine to me, but I am wondering why you put a highscore update in there... wouldn't that only be necessary if "function_4" evaluates to true? (Thus justifying, in my opinion, another function which calls the map completed function and then if true calls "function_5") Hey, Stevie! I recognize that code :P |
| ||
I would adjust it so your Consts/Globals are before your graphics setup (and consts before globals): 1. constants 2. globals 3. graphic setup 4. main loop call_function1 ; keyboard config call_function2 ; map loading call_function3 ; setup collisions call_function4; conditions for finishing the current map call_function5; highscore 5. functions (function1, function2, function3, etc.) Where are Types? I would place them just after Constants. |
| ||
I know it makes no difference, but I tend to keep Pre-render function before the main loop, and post-render functions after the main loop |
| ||
very good comments! i think the best described my needs is the following: 1. constants 2. types 3. globals 4. graphic setup 5. main loop keyboard_config() map_loading() setup_collisions() conditions() highscore() 6. functions keyboard_config() map_loading() setup_collisions() conditions() highscore() thanks a lot! Matt. |
| ||
your not going to put all those functions you listed in the main loop are you?, you should call those before it or else your loading the same thing over and over again or as Stevie G puts it, as many times as it take for 1 to equal 2, that by the way is true, or might be according to this thread: http://www.blitzbasic.com/Community/posts.php?topic=58343#649830 |
| ||
i was thinking to put all those functions in the main loop :( so, now i understand it is not correct, thanks for saying it, but will you please tell me what would be the correct way to use this functions or maybe an idea why this functions dont go in the main loop? thanks btw. |
| ||
those functions you mentioned sound like functions that would be called before or after the main loop is finished. eg: you probably dont want to load the map over and over again, that would (assuming your using 3d) raise your polycount with duplicates.; you only need to setup collisions once unless you call ClearCollisions; Usually you have the user setup the keyboard config before they start the game; You usually check to see if the user is on the highscore list after they finish playing.; ect. ect. ect. usually you want to call those functions you listed when you need them, not every reiteration of a loop. |
| ||
ok, so it will be somthing like this? 1. constants 2. types 3. globals 4. graphic setup 5. keyboard_config() 6. display_hud() 7. map_loading() 8. setup_collisions() 9. ---main loop ---------conditions() ------end loop highscore() 10. functions keyboard_config() display_hud() map_loading() setup_collisions() conditions() highscore() is it better? matt. |
| ||
The Call to DisplayHud probably wants to go in the main loop (ie Diplay every frame) 1. Constants 2. Globals 3. Types 4. Things that only need to be done once. 5. Start Main Loop 6. Things that need to be done Before frame is drawn 7. Draw Frame (Render) 8. Things that need to be done after frame is drawn (ie HUD) 9. Go to 5 Normaly I have 1 and 2 in the main program, 3 in an include, 4 in an include with all the functions needed for 6, 5 and 7 in main file and 8 in another include. (Not really in includes, but in a project manager as seperate files) If you are realy stuck, I would recomend downloading "Protean" which has an excelent prototype 3d and 2d template. (and its a good ide anyway). And has a project manager to ease includes (see above) |
| ||
You probably don't want map loading before the main loop like that, since you can only load one map and then the game never changes. Rather, the approach would be something like call map_loading() from inside conditions() if the condition where a new map is needed is true. In a similar way, setup_collisions() should be called from inside map_loading since that is part of setting up a level. |