Windows Create instance in new room

M

Michał

Guest
Hi,
I have problem with creating instances in created rooms.

Code:

Code:
var  _MENU_ROOM;
_MENU_ROOM = room_add();
var width = 800;
var height = 600;
room_set_width(_MENU_ROOM, width);
room_set_height(_MENU_ROOM, height);

window_set_size(width,height);


surface_resize(application_surface, width, height);

room_goto(_MENU_ROOM);

var windowed_block = instance_create(0,0,some_object);
The some_object is not attached to just created room.

From thread I posted before I know that all scripts (of previous room) has to be done and then global variables such room (room id) will change.

How to solve it? How to add instance to new room? Maybe it is possible to overwrtite 'creation code' of the room?
 
A

Aura

Guest
That's not how GM works. Even if you're calling room_goto() before creating the instance, it does not mean that the instance would be created in the room you are moving to. All of that code would be executed in the same room. The instance is being created in the same room, then you're moving to the new one.

Use room_instance_add() instead.

Code:
room_instance_add(_MENU_ROOM, 0, 0, some_object);
 
M

Michał

Guest
That's not how GM works. Even if you're calling room_goto() before creating the instance, it does not mean that the instance would be created in the room you are moving to. All of that code would be executed in the same room. The instance is being created in the same room, then you're moving to the new one.

Use room_instance_add() instead.

Code:
room_instance_add(_MENU_ROOM, 0, 0, some_object);

I have problem with it.

Code:
var obj = room_instance_add(_MENU_ROOM, 0,0,test_object5);
obj.local = "value";
Unable to find any instance for object index '100002'
at gml_Script_createMenuRoom (line 28) - obj.local = "value";
 
A

Aura

Guest
The Manual entry on room_instance_add() said:
With this function you can add an instance into any room other than the current one and at any position within that room. The function returns the unique id of the instance which can then be used in further function calls to set properties etc... of that instance, but only once the game has entered the specified room. If you wish to create an instance in the current room you should be using the function instance_create.
Also, local is a built-in constant, you can't change its value.
 
M

Michał

Guest
Sorry for mislead you. I just changed this variable to underline that it is internal property of created object. In general I am creating dictionary variable in obj.

Code:
var obj = room_instance_add(_MENU_ROOM, 0,0,test_object5);
obj.dictionary= "value";

Unable to find any instance for object index '100002'
at gml_Script_createMenuRoom (line 28) - obj.dictionary = "value";
 
A

Aura

Guest
That is what the Manual means. The room_instance_add() function returns the id of the instance that is created in the given room. But since an instance of that object does not exist in the current room and that's where all of that code gets executed, GM throws an error. You can't alter the properties of an instance of a room from a different room.

What you can do is store the string in a global variable, then move to that room and use the Create event of test_object5 to set dictionary to that value. Note that you should declare the global variable at such a place where it does not get redeclared, otherwise data might get lost. Declare it only once at the game start using the creation code of the very first room.
 
Top