1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 combining strings into object name?

Discussion in 'Programming' started by Anomaly, Sep 25, 2017.

  1. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    Hi...

    Is there any way to combine names into one?
    Like say... var thing = 2
    How do I make the object name cited be garden_door_2 named that in a statement just because thing = 2 ?

    Like... ("obj_garden_door_" + thing) make it's name
    Trying to make a bunch of repetitive code more efficient.
    Make thing a string also, then convert the focal result into the actual object name desired?

    (There already exists obj_garden_door_2 waiting to be called)

    Thanks!
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    991
    You can use asset_get_index() to get the id of an object (or some other kind of resource) given its name as a string.
     
    Anomaly likes this.
  3. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    very cool thank you much.
     
  4. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    so i just condensed pages of code into one line.
    awesome..

    now i'm wondering if the REVERSE of this could happen..

    I have all my (MAANY) teleporter pads parented to oTeleport_Parent.

    is there a way I can find the exact name of the specific teleport pad just by touching the parent and looking for a number in the name of the instance I'm standing on?

    say i step on the object called oTeleporter_3

    can i find out that it's "3" and translate that info elsewhere?

    so INSTEAD of...

    ETC ETC ETC>....


    possibly i could do some type of detection using any of THESE in combination with one another by chance...

    FYI all the oTeleporter objects are their own objects..
    if i could find a way to do them as instances that would be cool too.. but i might not be able to.. i have a keypad input that the player chooses a teleport destination..

    i'm very excited about learning how to streamline my code for future writing instead of all these redundant copypastas.

    THANKS!
     
    Last edited: Sep 25, 2017
  5. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,000
    Every teleporter can still be an object. Have a teleporter for each location, eg obj_teleporter1, obj_teleporter2, which are children of one called par_teleporter. All the code you need goes in the parent. In the create event for each child teleporter you can just have it's id and the destination.
    Code:
    origin = 1;  //it's unique id
    destination = 3;  //unique id of where the teleporter takes them
    
    Then when you need to check you can use the parent, eg.
    Code:
    if place_meeting(x,y,par_teleporter) && (key_use)
    {
      var origin = other.origin;   //not sure how you are using this, but you now have the unique teleporters origin id
      var destination = other.destination;  //and the destination of that teleporter.
    //if you need to teleport the player, you could do that here with other.x, other.y as the destination.
      state = states.teleporting
    }
    
    And another method...
    Depending on how you are setting it up though, you could also establish all teleporter positions in a ds_grid at the start and just access it when needed.
    Code:
    pos = ds_grid_create(2,4);  //2 is 2 columns for x, y, and 4 is how many teleporters.
    pos[# 0,0] = 100;  //x position of teleporter0;
    pos[# 0,1] = 250;  //y position of teleporter0;
    pos[# 1,0] = 400;  //x position of teleporter1;
    pos[# 1,1] = 600;  //y position of teleporter1;
    and so on...
    
    If you know the teleporter touched or access has origin as id 1, using the method above, you can send the player there with:
    Code:
    obj_player.x = pos[# 1, 0];
    obj_player.y = pos[# 1, 1];
    
     
    Last edited: Sep 25, 2017
    Anomaly likes this.
  6. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    perfect. thanks alot that saves a bunch of over thinking things hah!!
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,684
    If you really want to streamline your code, you should learn the difference between an instance and an object.

    Don't create distinct children objects from a parent. Instead, have just one teleporter object and create multiple instances of it in the room. Use the Instance Creation code on each instance to customize the source and destination.
     
  8. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,033
    The use of "other" in the code given above is wrong. "other" is only valid in a collision event or within the context of a "with" statement... However the ide is sound, you'd just need to change the code a bit:

    Code:
    if key
    {
    var _i = instance_place(x, y, obj_TeleporterParent);
    if _i != noone
        {
        var origin = _i.origin;   //not sure how you are using this, but you now have the unique teleporters origin id
        var destination = _i.destination;  //and the destination of that teleporter.
        state = states.teleporting
        }
    }
    As for your question about doing the same in reverse, you can. You can use object_get_name() to get the object name as a string and then parse that. It's be slower though so I'd use the method described above, OR simply have a single variable and single teleporter object then set the variable in the room editor Creation Code:

    Code:
    // CREATE EVENT
    goto = 1;
    teleport_id = 0;
    
    // ROOM CREATE EVENT FOR INSTANCE OF TELEPORTER
    goto = 5; // or whatever
    teleport_id = 2;
    
    // STEP EVENT
    if in_use
    {
    var _t = noone;
    with (obj_Teleport)
        {
        if id != other.id
           {
           if teleport_id == other.goto
                 {
                 _t = id;
                }
            }
        }
    if _t != noone
        {
        // Teleport code here, eg:
        with (obj_player)
            {
            x = _t.x;
            y = _t.y;
            }
        }
    }
    Sure, the code is longer, but this is MUCH more flexible and you only need one teleport object in the whole game. :)

    EDIT: Ninja'd by Frosty...
     
    Last edited: Sep 25, 2017
    Anomaly likes this.
  9. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    hmm it's not working... "other" would be ... the player right? how's the code if place meeting lines designate the child's origin variable?

    is "other" somehow able to get the CHILD info?

    i'm getting this error...
    "
    Variable <unknown_object>.<unknown variable>(100027, -2147483648) not set before reading it.
    at gml_Script_scr_Player_Movement (line 115) - teleporter = other.origin;"

    even though every teleporter has origin = 1; or 2 or 3 in it's creation event...

    i even put origin = 0; in the parent to test but still same error..

    thoughts?


    oops i have not read nocturne's post yet...
     
  10. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    1,000
    Whoops, well if that code is placed in player in a collision event with par_teleporter it should work, although the change you made caters for it too. Cheers.

    For Anomaly, as FrostCat mentioned above though, using a single object called o_teleporter and just using the instance creation code with the unique id and destination in there is a much better solution.
     
    Anomaly likes this.
  11. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244


    yaaaassss
    thanks! i knew it was the instance_place... was reading about that on another thread.

    i will try your longer code also.
     
  12. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    Nocturne, is this a typo?...

    y = t_t.y; with the t before the _t?

    I'm probably going to have questions about this when I implement it!

    :)
     
  13. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,033
    Yes, typo, sorry. I've edited the post... :)
     
    Anomaly likes this.
  14. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244

    yup that's my main goal.
     
  15. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    it's working great with the instances..

    however i haven't found a way to make it just play a "nope" sound if you're at the same teleporter that you have chosen to go to. ie: can't teleport in place...
    it seems that
    Code:
    if teleport_id == other.goto
    would be that code right? but it seems to do the opposite...
    bit confused.
     
  16. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,033
    Well, using the code I have you'd do something like:

    Code:
    if _t != noone
        {
        // Teleport code here, eg:
        with (obj_player)
            {
            x = _t.x;
            y = _t.y;
            }
        }
    else audio_play_sound(snd_NOPE, 0, false);
    That should work fine.
     
  17. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244

    that's exactly what i have and it's actin purdy funny...

    i'll show you what i have... i'ts changed alot..

    Code:
      
    if place_meeting(x,y,oTeleporter) && (key_use)
           {
               g_Teleporter_Destination = noone;
              
               with (oTeleporter)
                   {
                       if id != other.id
                           {  
                               if (Teleporter_ID == g_Teleporter_Keypad_Button_Selected) // the keypad selected is the destination
                                   {
                                       g_Teleporter_Destination = id;
                                   }
                           }
                   }
          
               if g_Teleporter_Destination != noone
                   {
                       state = states.teleporting;
                   }
              
               else
                   {
                       audio_play_sound(aTeleport_Nope,3,false);
                   }  
           }
    
    
    it will still teleport from 2 to 2 or 3 to 3 etc..
    but only after i've selected a new destination..
    if i havent done that, it will do the nope sound.
     
  18. Anomaly

    Anomaly Member

    Joined:
    Aug 23, 2017
    Posts:
    244
    I'm just going to make it so the keypad triggers the teleporting immediately, and the teleport location you're at isn't offered as an option to choose from.

    Problem solved

    Aint nobody got time for that.
     

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