Forum > Game Maker Help and Discussion

Creating and loading room files

(1/1)

lordcyber:
Game Maker has options to load external resources,like sounds,sprites,backgrounds and other things.But still doesn't have functions that deal rooms as external files.If your game project is a little big,can be bothersome the loading time everytime that you load your game to test it.
This tutorial will explain how creating binary files containing room's data and loading later.My purpose is turning a little more simple for people that want use it in the projects and showing that this method isn't impossible or too much advanced.


Part 1-Prerequisites

I know 2 alternative room level editors:Gmare(http://gmare.codeplex.com/ ) and Ogmo editor (http://ogmoeditor.com/ ).The former gives problems when I try load one my GM8.1 files,so I use the latter.
I will suppose that you have some experience with Ogmo Editor.If you don't have,it has nice tutorials in his site,and can read a tutorial about creating rooms from XML files(http://brod.net16.net/OE4GM/ ).If many people have problems,I can create another tutorial focusing with the Ogmo Editor.
Will be neccesary the gmXML extension (http://gmc.yoyogames.com/index.php?showtopic=462882 ),that turns more easy to read info in the XML format.

Part 2-Binary stuff
(click to show/hide)Reading a binary file is more fast than reading a text file.
Because I'm not a professional about the coding details,my view can be alittle noobish,but practical.
A binary file can be viewed as a sequence of bytes.A byte is a group of 8 bits,which each one can be 1 or 0, like 10011010.So you can have 256 types of bytes,from 00000000 to 11111111.So if you need store a number larger than 256,you will need a second byte(unless if the number is bigger than 256^2,will need a third byte too,and so on...).
In a simple way,the number in the decimal base is put a binary base.(If the humans born with only 2 fingers,won't be necessary this conversion)
A char can be stored in a byte, so a string of n chars will need n bytes.
Game Maker has some functions that deal with binary files.

file_bin_open(fname,mod):open a bin file.fname is a tring with bin file's location and mod is mode that the file will be used(0=reading,1=writing,2=reading and writing).It will return the fileid
file_bin_write_byte(fileid,byte): write a byte to file.
file_bin_read_byte(fileid):read a byte from the file.
file_bin_close(fileid):close the file with this fileid.

A warning about binary files manipulation.The way that the byte is read/written a sequential.
In simple way,if you open a binary and call the write/read function,it will be executed on the first byte.If you call one of them again,it will be executed on the second byte's file,and so on.It won't have a pointer that can move to a specific place.For example,if you write the room's name,room width and room height in a bin file,you need know that you will read the values in this sequence.

Now I show some scripts that can be useful(they were made by Guinea,programmer of Midas Machine,so give him credit if you use it).if you want more details about bitwise operator,you can find in http://testgmc.yoyogames.com/index.php?showtopic=473795.It can be useful if you want do more tweaks and overcome the limitations of these scripts.
file_bin_write_int16  //It write a value between 0 and 255*255.
(click to show/hide)
--- Code: ---// args: file, integer
file_bin_write_byte(argument0,(argument1 >> 8) & 255);
file_bin_write_byte(argument0,(argument1) & 255);

--- End code ---

file_bin_read_int16  //it read a value between 0 and 255*255 from the bin file
(click to show/hide)
--- Code: ---// args: file

var byte;
byte[0] = 0;
byte[1] = 0;
byte[2] = file_bin_read_byte(argument0);
byte[3] = file_bin_read_byte(argument0);

return (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];


--- End code ---
file_bin_write_string  //It write a the size of the string and the content of string(with max size of 255)
(click to show/hide)
--- Code: ---// args: file, string
// 1st byte: length of string (upto 255 chars)
// rest: the string itself.
var len, i;
len = string_length(argument1);
file_bin_write_byte(argument0,len);
for (i=1; i<=len; i+=1) file_bin_write_byte(argument0,ord(string_char_at(argument1,i)));

--- End code ---

file_bin_read_string //it reads a string from the bin file(with max size of 255).
(click to show/hide)
--- Code: ---// args: file
// 1st byte: length of string (upto 255 chars)
// rest: the string itself.

var len, i, str;

len = file_bin_read_byte(argument0);
str = "";
for (i=0; i<len; i+=1) str += chr(file_bin_read_byte(argument0));
return str;

--- End code ---

Part 3-Mapping
(click to show/hide)Because Ogmo Editor generate level files in XML, we need convert them to bin files one time.First we need associate the words used in the XML files with index of objects,tilesets,sprites and other stuff.that why we need use a map.A map stores pairs consisting a key and a value.A simple example is:

--- Code: ---globalvar oel_objects, oel_tilesets,oel_rooms;
oel_objects=ds_map_create();
ds_map_add(oel_objects, "ninja", obj_ninja_spaw);
ds_map_add(oel_objects, "music_player",obj_music_player);
ds_map_add(oel_objects, "draw_background", obj_draw_background);
ds_map_add(oel_objects, "block1x1", obj_block);
oel_tilesets=ds_map_create();
ds_map_add(oel_tilesets,"t_world1",t_world1);

oel_rooms=ds_map_create();

--- End code ---
  In this code I create 3 maps.The 2 first is to associte the tag names used of XML files with the index of the object/tileset(like a "translator").The third map will be used to store the room's id created with keys that will be the room's name.
The way that I added values in map is the hard way to do ,because if you have a big quantity of objects or other stuff,can be tiresome.I didn't have time to test easy way,but the idea is use function object_get_name() or background_get_name() (used as keys),and using the trick of create a object to get his id,that has max value(because it was the last object created),and destroyed, and use this value to limit a for loop.



to be continued...(I'm writing small pieces daily)

Gibbering Mouther:
Of course, you can also make and edit .bin files using a hex editor like HxD.

I should make a tutorial on how to use knowledge of editing binary files to play all the songs stored in NSF files and such.

lordcyber:
But this useless for this tutorial.

Anyway,sorry for the delay, guys and girls.
Instead  posting big pieces of text in post,I decide organize in pdf file.Sorry for errors in the English language.Maybe I miss some little pieces of info,but the text is tolarable.I hope that it helps someone.

Obreck:
Interesting. How are the external room load times with this system? I made a working external room system a while ago, but booting up the rooms felt like waiting for a PS1 game to load so I scrapped it.

lordcyber:
At first, I created a system that create rooms directly from the xml files.but I discovered that reading the info from a binary file is more fast.I didn't make precise test involving time loading,but, from my experience with beta testing of Midas Machine,I believe that can be fast enough to you want use it.Anyway, is really better loading a group of rooms instead the whole game(even if you are testing).Of course that depend how you define wisely the variable's names that you write in bin files,that it will take less time.
It took a good time to make it works.And avoids  the Ninjular's development goes to the limbo for lack of pacience in waiting the game loading.

About the guide,I think that I need someone to improve the text.

Navigation

[0] Message Index

Go to full version