GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-06-21 20:11:21

flexaplex
Member
Registered: 2008-12-11
Posts: 72

room_tile_add - doesn't return tile id

Brief Description:            room_tile_add - doesn't return tile id
GM Versions:                   GM5, GM6, GM7
References:                     [1] [2] [3]
Example File:                   room_tile_add doesn't return ids.gmk
Bug Confirmed:                Yes
Hardware Dependent:     No

Main Info:

There is a bug in the room_tile_add function which makes it return an index (0,1,2..) instead of an id (1000001,1000002,1000003....) when it is called. This bug is already mentioned in the other room_tile_add bug topic however I think it deserves it's own topic since it is a different bug.

With some basic testing it appears the first time the function is called the index returned is 0 and then increases by 1 with each subsequent call. However when you have added tiles in the room manually in which case the index starts at the number of tiles that are placed in the room. To note this index value is not affected at all by any tiles placed in previous rooms or any room_tile_add functions calls made to previous rooms. So unlike any other index in GM it appears to be local to the rooms and not globally cumulative.

I would also like to make a note of the terminology used in the manual for both room_tile_add and room_instance_add:

room_tile_add(ind,back,left,top,width,height,x,y,depth) Adds a new tile to the room at the indicate position. It returns the index of the tile. ...

room_instance_add(ind,x,y,obj) Adds a new instance of object obj to the room, placing it at the indicate position. It returns the index of the instance.

In both functions it uses index as the terminology instead of id. If you read the room_tile_add description alone you could think that the function was intentionally made to return the index instead of the id since it says so in the manual, however since it also says index instead of id for the room_instance_add function (which does actually return the id) Mark has probably just made a mistake in terminology in the manual also.

Workaround:

There is no nice workaround to this as the returned index is pretty useless. You may think of adding 10000001 to the index to get the tile add however this is very dodgy and not very good programming. It will also not give you the correct id in the case where you have added tiles into more than 1 room manually or deleted tiles from a room manually.

As the other bug in the function is likely to be a problem anyway so it is probably best just to switch to using the tile_add (which does not have this bug in function) in the room creation code. Doing this you can make your own replacement room_add_tile function like so:

Expand//room_tile_add(ind,back,left,top,width,height,x,y,depth)
room_code[argument0] += 'tile_add('+string(argument1)+','+string(argument2)+','+string(argument3)+','+string(argument4)+','+string(argument5)+','+string(argument6)+','+string(argument7)+','+string(argument8)+');';
room_set_code(argument0,room_code[argument0]);

In order to use this script you must first initialise a room_code array which is used to track the room creation code string for each room. In the case where you are already using instance creation code in a room you need to set the array to the corresponding code string used there otherwise just initialise it as an empty string. For example:

Expand//note: this should be initialised in a controller object at the start of the game.

globalvar room_code;
room_code[room1] = "";
room_code[room2] = "instance_create(20,20,obj_player);";

This would be the initialisation if you had no instance creation code in room1 and the instance creation code:  instance_create(20,20,obj_player);  in room2.

Last edited by flexaplex (2009-06-21 20:40:37)

Offline

Board footer

Powered by FluxBB