1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Adding rooms with persistent objects programmatically.

Discussion in 'Programming' started by Jorg Gjer, Oct 5, 2018.

  1. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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 ?
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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()?
     
    Nocturne likes this.
  3. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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.
     
  5. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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 likes this.
  7. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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.
     
  9. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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 ?
     
  10. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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 likes this.
  11. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  12. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  13. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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 likes this.
  14. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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)
     
  15. Jorg Gjer

    Jorg Gjer Member

    Joined:
    Nov 8, 2017
    Posts:
    30
    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.
     
  16. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    981
    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 :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice