'Converting a "name" to a real, then convert it back to a string

I've ran into some issues with my current gamemaker project. I've setup a simple "merge" functionality in my game, and I'm trying to increase it's QoL

So what's happening when I'm merging, is this;

var sabatons = instance_place(x, y, object_index);
if (instance_exists(sabatons)) {
    instance_create_layer(sabatons.lastknownPosX, sabatons.lastknownPosY, "Instances", "");

    if (level >= 3) {
        scr_spawn_experience(); 
    }

    audio_play_sound(snd_sabatons_merge,10,false);

    instance_destroy();
    instance_destroy(sabatons);
}

So what the code above does, is to check if object_index matches with what I'm trying to merge it with, and if it does, it runs instance_create_layer - which is where my question comes in.

My objects are easily named with a digit at the end, to keep track of their "level" so basically, obj_sabatons_1 means it's the first item in a chain, obj_sabatons_2 is the second etc. Now what I need help with is to convert whatever the object is that I'm trying to merge with, to a string (object_index) but increase it with 1, so I then can put that result in my instance_create_layer so the game will create the next level of item, if I successfully merge my first two items :)

Thanks!!



Solution 1:[1]

Is there a reason that the level should be part of the name? If not, then you could convert the level into a variable of the object obj_sabatons, so you can keep all the code related to obj_sabatons in one place (And then remove all those copied objects with digits at the end)

Making multiple copies of the same object can be disadvantageous in numerous ways, For example, if you want to expand the limit, then you'd have to create a new object for each level, making a code change also means changing that code in every other object level (unless you're making use of parents/child objects), it would also make the progress of merging objects into new objects easier

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven