• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker combining strings into object name?

A

Anomaly

Guest
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!
 
A

Anomaly

Guest
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...

if place_meeting(x,y,oTeleporter_1) && (key_use)
{
teleporter = 1;
state = states.teleporting
}

if place_meeting(x,y,oTeleporter_2) && (key_use)
{
teleporter = 2;
state = states.teleporting
}

if place_meeting(x,y,oTeleporter_3) && (key_use)
{
teleporter = 3;
state = states.teleporting
}
ETC ETC ETC>....


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

g_Teleporter_At = asset_get_index("oTeleporter_" + string(teleporter));

string_char_at(g_Teleporter_At, 12);
string_digits( g_Teleporter_At )

if place_meeting(x,y,oTeleporter_Parent)
{ object_get_name (instance_nearest(oPlayer.x,oPlayer.y,oTeleporter_Parent."child"??)

object_index
object_get_name

instance_position(oPlayer.x, oPlayer.y, oTeleporter_Parent)
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 by a moderator:

Slyddar

Member
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:
A

Anomaly

Guest
perfect. thanks alot that saves a bunch of over thinking things hah!!
 

FrostyCat

Redemption Seeker
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.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
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:
A

Anomaly

Guest
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...
 

Slyddar

Member
The use of "other" in the code given above is wrong.
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.
 
A

Anomaly

Guest
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_t.y;
        }
    }
}
Sure, the cde is longer, but this is MUCH more flexible and you only need one teleport object in the whole game. :)

EDIT: Ninja'd by Frosty...


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

i will try your longer code also.
 
A

Anomaly

Guest
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!

:)
 
A

Anomaly

Guest
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.

yup that's my main goal.
 
A

Anomaly

Guest
Yes, typo, sorry. I've edited the post... :)
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.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
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.
 
A

Anomaly

Guest
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.

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.
 
A

Anomaly

Guest
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.
 
Top