Skip to content

02. The module

RhettTR edited this page Jan 16, 2024 · 1 revision

The module (the specific implementation of a game on the game engine) must contain the Luau scripts and the game resources, which in this case are images files (jpg, png and gif). There are images for maps, counters, cards, image-buttons, etc. made by the module developer.

Resources are best organized in directories with a standard top directory ('images') and subdirectories based on the natural grouping of similar images.

The Luau scripts are stored in a flat standard 'script' directory. The script of a module consists of many individual script files and has a standard base script (or main script) called 'module.luau'.

A simple module will consist of the two items:

  • images (a directory holding all the resources)
  • script (a directory holding all the script files)

These two directories make up the zip file of the module.

zip -r module images script

The C++ part of the engine must

  • open the zip file
  • load the resources into memory
  • make sure the resources can be accessed by their file names from the script
  • load the script files
  • compile the script files
  • start the Luau Virtual Machine (VM)

A simple C++ program running on Linux does the following. It opens and reads the module zip file using libarchive.

Script and resources are stored in memory using a simple struct.

A global is created for each script file (each chunk) so that other script files can reference it.

for (auto &element : scripts) 
{

	cout << element.chunkName << endl;

	bytecode = luau_compile(element.chunk.c_str(), element.chunk.length(), NULL, &bytecodeSize);
	assert(luau_load(L, element.chunkName.c_str(), bytecode, bytecodeSize, 0) == 0);
	free(bytecode);	
	
	// set global to make other script files use this script file
	lua_setglobal(L, element.chunkName.c_str());
}

Two exposed C functions will create the main window and display a resource file as background.

	lua_pushcfunction(L, setbackground, "setbackground");
	lua_setglobal(L, "setbackground");
	
	lua_pushcfunction(L, setwindow, "setwindow");
	lua_setglobal(L, "setwindow");

The two script files look like this:

bg.luau

local Background = {}

function Background.setBackground(fileName)
    setbackground(fileName)
end


return Background

module.luau

b = bg()

f = function()	
    b.setBackground('background.jpg')
end

setwindow()

module

Running module.cpp displays

screen shot


sudo apt-get install libgtk-3-dev

sudo apt-get install libgdk-pixbuf2.0-dev

CPLUS_INCLUDE_PATH=/usr/include/gtk-3.0:/usr/include/glib-2.0:/usr/lib/x86_64-linux-gnu/glib-2.0/include:/usr/include/pango-1.0:/usr/include/harfbuzz:/usr/include/cairo:/usr/include/gdk-pixbuf-2.0:/usr/include/atk-1.0;export CPLUS_INCLUDE_PATH

LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu;export LD_LIBRARY_PATH

g++ -Wall -o module module.cpp -I/home/me/luau/VM/include -I/home/me/luau/Compiler/include -L/home/me/luau/build/release -lluauvm -lluaucompiler -lluauast -lisocline -lgtk-3 -lgio-2.0 -lgobject-2.0 -lgdk_pixbuf-2.0 -larchive

Clone this wiki locally