GM:S 1.4 Persistent objects and variables

Discussion in 'Programming' started by PWL, Aug 2, 2017.

  1. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Quick question. Are variables supposed to carry over with persistent objects, or are they deleted when I chance rooms?
    And if they are destoryed, why do I need persistent objects if I have to use global variables anyway?

    Code:
    // Create event
    currentRoomX = 5;
    currentRoomY = 5;
    
    room_goto(room_level);
    Code:
    // Room start event
    show_message(currentRoomX); // <-- Instant error
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,244
    That's weird huh, I think it might have something to do that persistent objects don't rerun their creation code. But I don't know why it would forget them if they are persistent.
     
  3. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    582
    The variables should remain until the instance is destroyed.

    Regarding the error - So, you have the instance in a room, and you change rooms and get the error? Or, you get the error on the very first room?
    If the latter, it's because (I think) the room start event comes before the creation event.
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,244
    The manual states that the creation event is always first so it must be something else.
     
  5. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Thanks all for your replies.

    The create event should not fire when I enter the next room, because the object was created in the previous room.

    I can confirm that the create event has happened and that it happens once, then the room start event fires (in the next room I suppose, because I change rooms in the create event), and then get the error. The create event is not fired in the second room, and neither is it my intention.

    Edit: Correction, it is placed in the first room and not created by code. If that matters.
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,076
    Is the error due to trying to call show_message() with a number instead of a string? Try
    Code:
    show_message(string(currentRoomX));
    
     
  7. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Oh god.. It was a typo in my code there.

    And with that fixed, it lead me to the original error that caused me to test with a simple variable.
    My ds_grid did not exist. Reason: I destroyed them all in the Room End event to prevent memory leaks. :bash:

    Thank you all!
     
  8. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    703
    For the record, show_message will automatically cast a real value into a string.
     

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