Adding rooms with persistent objects programmatically.

Jorg Gjer

Member
Hi.

I want to be able to create a rom,
Put layer and objects in it.
Bring objecjst from the room i'm in , to the created rom.
Play, have fun , and return back.

All this programaticaly.

I have added a room.
Then in in code.

Code:
myroom = room_duplicate(room_roomtodublicate);
Then adding a objet into the room.

Code:
room_instance_add(myroom, 100, 100, obj_player);
Set the player object to persistent
then
Code:
room_goto(myroom);
I get an error about missing variables.
Why is there missing variables when the player already has been running around , and is persistent ?

When i turn off persistent , the code runs fine , but data is deleted and its a new object.

What is a good aproach\practice to bring objects from room to room , that has set variables ?
 

chamaeleon

Member
Code:
room_instance_add(myroom, 100, 100, obj_player);
Won't this add a new instance that is separate from the persistent instance you already had? Being persistent does not mean it is a singular instance only. My expectation would be that when you jump to the room, you'd have two instances of obj_player, one being the persistent instance from a previous room, and one that you just created which may not be initialized properly.Maybe you "just" need to set x and y on your already existing obj_player instance to 100, 100 (or whatever), in some room start event instead of using room_instance_add()?
 

Jorg Gjer

Member
Yes. I think i make a new instance too , but that is not what i want. I want to use the same player object with all its data.
I think the way is to set them up as global instances , but then , how do i add that instance to the room , without creating a new instace ?

Thank you for help.
 

chamaeleon

Member
Yes. I think i make a new instance too , but that is not what i want. I want to use the same player object with all its data.
I think the way is to set them up as global instances , but then , how do i add that instance to the room , without creating a new instace ?

Thank you for help.
If you have an instance of obj_player in an earlier room and you go to your newly created room, that instance should be there automatically if obj_player is in fact persistent.
 

Jorg Gjer

Member
I tryed that. I dont think it is logic that the object is allready in the room if it is persistent.

This is what i want to do.

Code:
room_instance_add(myroom, 100, 100, global.Hero);
The code is not working because global.Hero is a variable and not an object.
 

chamaeleon

Member
I tryed that. I dont think it is logic that the object is allready in the room if it is persistent.

This is what i want to do.

Code:
room_instance_add(myroom, 100, 100, global.Hero);
The code is not working because global.Hero is a variable and not an object.
Given a room that contains a persistent object, a blank/empty room named rm_empty..
Create event for persistent object
Code:
mydata = "Important data";
Mouse released event for persistent object
Code:
new_room = room_duplicate(rm_empty);
show_debug_message("Jumping to room " + string(new_room));
room_goto(new_room);
Draw event for the persistent object
Code:
draw_self();
draw_text(x + 20, y, "Room id = " + string(room));
draw_text(x + 20, y + 20, "Data = " + mydata);
Works ok for me and shows an increased value in the room id and the "important data" I wish to carry along with the instance as I click on the instance to activate the jumping to a newly created room.

If you already have an instance of the player object why are you looking at the function room_instance_add()? You shouldn't have to, given persistence being enabled.
 

Jorg Gjer

Member
Import data. That should not be necessary. I'm quite shure the point of persistence is that you dont have to import the data. It's just me that don't get how to do it.
 

chamaeleon

Member
Import data. That should not be necessary. I'm quite shure the point of persistence is that you dont have to import the data. It's just me that don't get how to do it.
Important.. Not import. It was just an example of a string being initialized and later used in the draw event in a different room from the room the persistent object was originally created in.
 

Jorg Gjer

Member
OK , i understant what you mean, but if i try to copy what you do here , i don't see the persistant object in the new room.
But is the mecanic like that. All persistent objects appear in a new created room ?
Strange to me , but i guess you are right.

When i try it , i don't get that reslut. Is there som room setting or general setting that can give different result ?
 

chamaeleon

Member
OK , i understant what you mean, but if i try to copy what you do here , i don't see the persistant object in the new room.
But is the mecanic like that. All persistent objects appear in a new created room ?
Strange to me , but i guess you are right.

When i try it , i don't get that reslut. Is there som room setting or general setting that can give different result ?
I used no special settings except enable the persistent option for my test object. I placed an instance of this object in the first room. I had a blank room as well to serve as my template to create a new room dynamically That is all plus the event code I posted. No other options were changed from their default.
 

Jorg Gjer

Member
Ok , i found my mistake. I forgot to add the layer for the player to the newly created room.
I did'not get the point that persistent objects will appear in new rooms.

Thank you for help.
 

Jorg Gjer

Member
I get this to work at intenden with my Player object.
But i also have a second objet , the boat the player is riding , witch has an array that holds the parts for the boat.
When I move on to the next room , i get an object index undefined error on the array;

So its working with the Player object but with the object with an array gets a problem.

I would be glad if anyone can help.
 

chamaeleon

Member
I get this to work at intenden with my Player object.
But i also have a second objet , the boat the player is riding , witch has an array that holds the parts for the boat.
When I move on to the next room , i get an object index undefined error on the array;

So its working with the Player object but with the object with an array gets a problem.

I would be glad if anyone can help.
Rather unclear what is not valid. Perhaps you can include the error that usually pops up on undefined content? Does the array contain elements that are instance ids of instances that are not persistent?
 

Jorg Gjer

Member
This is code from the step event of the boat.

Code:
if(array_length_1d(arrayInstanceMap) > 0){
    for(t=0; t < array_length_1d(arrayInstanceMap);t++){
        arrayInstanceMap[t].x = x + (arrayInstanceMap[t].i * 32);       // This is line 62
This is working just fine in the first room.

Error message.


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object oBoatHolder:

Unable to find any instance for object index '100098' name '<undefined>'
at gml_Object_oBoatHolder_Step_0 (line 62) - arrayInstanceMap[t].x = x + (arrayInstanceMap[t].i * 32);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oBoatHolder_Step_0 (line 62)
 

Jorg Gjer

Member
I guess your right , that it contains instance id.
So i think i have to set all the created items to be persitent too.

Yes that fixed the problem.

Than you Sooo much.
 

chamaeleon

Member
arrayInstanceMap[t].x = x + (arrayInstanceMap[t].i * 32);
What object have you put in arrayInstanceMap and have you enabled persistence either in the object settings or by setting the persistent flag on each instance manually with code for each instance?

Edit: I'm happy to hear you have resolved it :)
 
Top