Author Topic: Best way to modularize your code?  (Read 481 times)


  • Newbie
  • *
  • Posts: 40
    • View Profile
Best way to modularize your code?
« on: June 25, 2016 »
Obviously keeping one big file is less than ideal, especially for bigger games and not just fooling around. If it was Pascal, I'd use units, if it was C, I'd use header files. But what it is for NaaLaa? How do you modularize your code in it so you have nice project structure with logically named files each representing one part of your game (e.g. physics, effects, resource managing, etc.) where main game file is just a glue between them?


  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: Best way to modularize your code?
« Reply #1 on: June 26, 2016 »
Currently there's no other way than to create and import libraries. Let's say you want your resource management in a separate file, create res.txt and compile it to a library (F8):

Code: [Select]
' Resource manager.

' Images.
LOGO_IMG       101

BIG_FONT       1


' Load resources.
procedure LoadResources()
load image BACKGROUND_IMG, "assets/background.png"
load image LOGO_IMG, "assets/logo.png"
create font 0, "arial", 12, true
create font 1, "arial", 24, true

' Delete resources.
procedure DeleteResources()
free image LOGO_IMG

Then, in your game:

Code: [Select]
' Import resource manager.
import "res.lib"

' Load resources.

' Draw some images and write some stuff.
draw image BACKGROUND_IMG, 0, 0
draw image LOGO_IMG, (width(primary) - width(LOGO_IMG))/2, 100
set caret width(primary)/2, 240
set font BIG_FONT
center "OPTIONS"
set font SMALL_FONT
center "Start Game"
center "Exit"

wait keydown

Note though, that if library A imports library B, library B will be compiled INTO library A. If your main program imports library A it will automaticly get access to library B and should NOT import library B by itself.

There's also a thing called "program units" (see documentation), which allows you to terminate the existing program and start running another one. This was more or less deprecated when libraries were introduced, but it should still work.


  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Best way to modularize your code?
« Reply #2 on: June 26, 2016 »
I see. Could we get something along the lines of C's #include? Having to recompile libs each time you have to change them is a little bit wasteful.