Hmmmm, been messing with the binary file that GMare exports. I was wondering how I was going to use the creation code part of the instances. Since instances have no way of setting the creation code. I sort of have a hackish way of executing the code, but it doesn't mimic the behavior of native instance creation code. The code is executed right AFTER the the instance's create event. Which for me, is just fine, but it might not be for everyone else. Any solutions welcomed. Any who, I pretty much have that done. As well as a some sample projects on how to load a GMare .bin file.
The binary file load script: (Pretty straight forward)
//-----------------------------------------------------------------------------
// A standard room load from a GMare .bin file.
//-----------------------------------------------------------------------------
_file = argument0; // STRING: The GMare .bin file path.
_room = argument1; // STRING: The name of the room to load.
_background = argument2; // REAL: The background to use for the room.
// If the file does not exist, exit.
if (!file_exists(_file))
{
// Show error.
show_message("ERROR: The GMare binary file: " + _file + " does not exist.");
exit;
}
// Open the .bin file to read.
_stream = file_bin_open(_file, 0);
// The GMare binary file magic number.
_magic_number = "";
// Read four character bytes.
repeat (4)
{
_magic_number += chr(file_bin_read_byte(_stream));
}
// If not a valid GMare binary file, exit.
if (_magic_number != "GBIN")
{
// Show error, close file stream.
show_message("ERROR: The file is not a valid GMare .bin file.");
file_bin_close(_stream);
exit;
}
// The offset to the actual room data beyond the header.
_data_offset = -1;
// The amount of rooms in the file.
_room_count = scr_read_bytes(_stream, 2, true);
// Iterate through rooms in the GMare binary file header.
repeat (_room_count)
{
// Read room name.
_search = scr_read_string(_stream, 1);
// Read the room data offset.
_offset = scr_read_bytes(_stream, 4, true);
// If the search string matches the desired room string.
if (_search == _room)
{
// Set data offset, and break loop.
_data_offset = _offset;
break;
}
}
// If the desired room was not found, exit.
if (_data_offset == -1)
{
// Show error, close file stream.
show_message("ERROR: The room: " + _room + " was not found.");
file_bin_close(_stream);
exit;
}
// Seek, to the desired room data.
file_bin_seek(_stream, _data_offset);
// Room data:
_columns = scr_read_bytes(_stream, 2, true); // The number of tile columns in the room.
_rows = scr_read_bytes(_stream, 2, true); // The number of tile rows in the room.
_tile_offset_x = file_bin_read_byte(_stream); // the horizontal tile offset of the background.
_tile_offset_y = file_bin_read_byte(_stream); // The vertical tile offset of the background.
_tile_separation_x = file_bin_read_byte(_stream); // The horizontal separation of the background.
_tile_separation_y = file_bin_read_byte(_stream); // The vertical separation of the background.
_tile_width = file_bin_read_byte(_stream); // Width of a single tile in the background.
_tile_height = file_bin_read_byte(_stream); // Height of a single tile in the background.
_tile_columns = scr_read_bytes(_stream, 2, true); // The number of coulmns of the background.
_layer_count = scr_read_bytes(_stream, 4, true); // The number of layers in the room.
_instance_count = scr_read_bytes(_stream, 4, true); // The number of instances in the room.
_collision_count = scr_read_bytes(_stream, 4, true); // The number of collisions in the room.
// Iterate through layers.
repeat (_layer_count)
{
// Get layer depth.
_layer_depth = scr_read_bytes(_stream, 4, true);
// If the layer uses the sector method, instead of the tile method.
if (file_bin_read_byte(_stream) == 0)
{
// Iterate through rows.
for (_row = 0; _row < _rows; _row += 1)
{
// Iterate through columns.
for (_column = 0; _column < _columns; _column += 1)
{
// Get source tile id from file.
_tile_id = scr_read_bytes(_stream, 2, true);
// Calculate tile destination position.
_dest_x = _column * _tile_width + _tile_offset_x;
_dest_y = _row * _tile_height + _tile_offset_y;
// If the tile id is not empty.
if (_tile_id != -1)
{
// Calculate tile source position.
_src_x = (_tile_id - (floor(_tile_id / _tile_columns)) * _tile_columns) * _tile_width;
_src_y = floor(_tile_id / _tile_columns) * _tile_height;
// Adjust with tile offsets and separations.
_src_x += (floor(_src_x / _tile_width) * _tile_separation_x) + _tile_offset_x;
_src_y += (floor(_src_y / _tile_height) * _tile_separation_y) + _tile_offset_y;
// Add tile to room.
tile_add(_background, _src_x, _src_y, _tile_width, _tile_height, _dest_x, _dest_y, _layer_depth);
}
}
}
}
else // Tile mode.
{
// The number of tiles in tile mode.
_tile_count = scr_read_bytes(_stream, 4, true);
// Iterate through tiles.
repeat (_tile_count)
{
// Get source tile id from file.
_tile_id = scr_read_bytes(_stream, 2, true);
// Read destination position.
_dest_x = scr_read_bytes(_stream, 4, true);
_dest_y = scr_read_bytes(_stream, 4, true);
// Calculate tile source position.
_src_x = (_tile_id - (floor(_tile_id / _tile_columns)) * _tile_columns) * _tile_width;
_src_y = floor(_tile_id / _tile_columns) * _tile_height;
// Adjust with tile offsets and separations.
_src_x += (floor(_src_x / _tile_width) * _tile_separation_x) + _tile_offset_x;
_src_y += (floor(_src_y / _tile_height) * _tile_separation_y) + _tile_offset_y;
// Add tile to room.
tile_add(_background, _src_x, _src_y, _tile_width, _tile_height, _dest_x, _dest_y, _layer_depth);
}
}
}
// Iterate through instances.
repeat (_instance_count)
{
_object = scr_read_bytes(_stream, 4, true); // Instance object id.
_x = scr_read_bytes(_stream, 4, true); // Horizontal coordinate.
_y = scr_read_bytes(_stream, 4, true); // Vertical coordinate.
_code = scr_read_string(_stream, 4); // Instance creation code.
// If the object does not exist.
if (object_exists(_object) == false)
{
// Show error, continue.
show_message("ERROR: The object with id: " + _object + " does not exist.");
continue;
}
// Create a new instance of the object.
_inst = instance_create(_x, _y, _object);
// If there is creation code, execute it.
if (_code != "")
{
// A heck to make sure the code executes within the instance.
// Problem being is this executes after the creation event.
// Solution is needed to emulate native behavior.
_code = string_insert("with (_inst) {", _code, 1);
_code = string_insert("}", _code, string_length(_code));
execute_string(_code);
}
}
// Close file stream.
file_bin_close(_stream);
In the WIP, I use a script within GMare to move the selected shrub to the left.