2 Step Forwards, 1 Step Back

Blitz3D Forums/Blitz3D Programming/2 Step Forwards, 1 Step Back

boomboom(Posted 2008) [#1]
Hi. Everytime I code, I seem to be breaking something when I add something new. To say my code is robust would be a big lie.

has anyone got any tips or reading material that can help me out on this and how to make better code?

I have 'Code Craft' on order, but wondering if anyone has any tips?


KillerX(Posted 2008) [#2]
Save a backup copy of your code.


Ross C(Posted 2008) [#3]
What i've been doing more and more often is creating my function away from my main code. I also make sure my functions rely very little on global variables. Then, if something is breaking, you can narrow it down and thoroughly test the function away from the main code.

Another good tip i got once, was to make sure your functions were never greater than the height of the page, so you can see the whole function.

Erm, something else i do is to use a game mode variable, not just for games though. The main loop would consist of something like:


If mode = 1 then
   Show_Menu()
ElseIf mode = 2 then
   Load_level(current_level)
ElseIf mode = 3 then
   Main_Game(Main_Mode)
ElseIf mode = 4 then
   Splash_loading_screen()
ElseIf mode = 5 then
   End Credits()
End if


Where the Main_Game() function would have more modes inside it:


Function Main_Game(Main_Mode)

   If Main_Game = 0 then
      Play_Game()
   ElseIf Main_Game = 1 then
      Play_cutscene()
   ElseIf Main_Game = 2 then
      Pause_game()
   ElseIf Main_game = 3 then
      Show_Inventory()
   End If

End Function


And again, each of the modes above would have their own modes within the functions. Simplify your code and sticks specific bits of code into functions. It saves commenting your code too.


;this code does something
for loop = 1 to 10
   do stuff
   do more stuff
   if loop2 = 1 to number
      check stuff
   next
next
; --------------------------



You could easily stick that into a function, and just write:

Do_Stuff() ; this code does something


Makes it far easier to read, and miss out too, if your trying to sort problems using the debugger.

Hope some of that helps. It's pretty standard stuff, but that's what helps in my opinion :o)


GfK(Posted 2008) [#4]
Use Subversion.

Commit changes when your code works, revert back to the last version when you break it.


JA2(Posted 2008) [#5]
I like to keep my code very tidy, proper indentation, capitalise(?) all functions and globals, only use local variables inside functions...

I also write a "create" and "delete" function for each type. Just helps to keep everything tidy :D Also do what Gfk suggests, make regular backups that you can fall back to when the code breaks...


GfK(Posted 2008) [#6]
Yeah. I'd also recommend TortoiseSVN because it adds easy to use context menus whereas Subversion is commandline. They both have good and bad points so its nice to have access to both.

You can right-click a versioned BMX file and click "Diff", TortoiseSVN then shows you the differences between your most recent backup and your working copy. Handy if you've broken your code and can't remember what you've changed.


Rob Farley(Posted 2008) [#7]
Break up your code into separate files and include them, modular code makes life simpler.

I usually have all my types in their own file along with all the functions that create / delete / update the type.

For example they might be an Alien type so the file would hold the type declaration and Alien_New(x,y), Alien_Kill(a.Alien), Alien_Update() etc etc. So I know that anything about Aliens, controlling, updating, loading, killing etc is all in that one file.

On top of that I'd have a file for Input, which will deal with input devices regardless of what that might be, so the game itself functions on common commands. Eg, I have an X/Y axis variables (-1 to 1) regardless on if I'm using keyboard, mouse or joypad. So the game works based on those variables rather than directly querying your input device.


Ross C(Posted 2008) [#8]
Pity it ain't bmax :oP

Does that work with blitz3d?


boomboom(Posted 2008) [#9]
Thats a good idea Rob, I saw someone using source files for each type (or system) and it did seem quite logical and easy to work with, had about 15.

Atm, each 'system' has a type (if needed) and usually 3 main functions under it, a create, update and destroy.

Then if needed i usually have a return functin (to return that type) and initialise.

I am using IDEal, and unfortunaly it automatically (and no way that i can see to close a file) opens each file in a project as a tab. So if I do split things up any more (i have a file for scene loading, a mainloop file, a gui file, a rough dev file....)it will get too crowded to work with.

I have been looking at the other b3d IDE's but IDEal seems to be the only one that has a 'strict' mode and shows up undeclared variables, which I have found to be a life saver. Does anyone know if any other IDE's have a 'Strict' feature?

I would really like to split my code up as you suggest, maybe in subfolders too, but the Strict is too useful to loose.


Gabriel(Posted 2008) [#10]
Does that work with blitz3d?

You mean Subversion? Yeah it works with anything. Images, sounds too if you want.

I am using IDEal, and unfortunaly it automatically (and no way that i can see to close a file) opens each file in a project as a tab.

Ack, what a nightmare. Have you asked the author of IDEal about that? I have a couple of hundred source files, not including third party libraries, and that would be ridiculous.


boomboom(Posted 2008) [#11]
Yea, I don't think the author is supporting IDEal any more, he hasn't done anything too it for 6 months or so.