GameMaker Is there a way to shorten the code?

sinigrimi

Member
Hello!

I have 560 ready rooms for the test rooms for the player (this is only at the first level, LOL), but to enter them as the example below seems to be hell. Is there a way to speed it up or will you have to spend a lot of time?

I also have a fork between the scripts, it will not affect the performance and speed of the code?

Code:
if random_beeRoom ==0{ //#1 bee
    instance_create_layer(180,180,"Instances",oBee);
    instance_create_layer(1740,180,"Instances",oBee);
    instance_create_layer(180,300,"Instances",oStone);
    instance_create_layer(300,300,"Instances",oStone);
    instance_create_layer(420,300,"Instances",oStone);
    instance_create_layer(780,300,"Instances",oStone);
    instance_create_layer(1140,300,"Instances",oStone);
    instance_create_layer(1500,300,"Instances",oStone);
    instance_create_layer(1620,300,"Instances",oStone);
    instance_create_layer(1740,300,"Instances",oStone);
    instance_create_layer(540,420,"Instances",oStone);
    instance_create_layer(660,420,"Instances",oStone);
    instance_create_layer(1260,420,"Instances",oStone);
    instance_create_layer(1380,300,"Instances",oStone);
    instance_create_layer(540,660,"Instances",oStone);
    instance_create_layer(660,660,"Instances",oStone);
    instance_create_layer(1260,660,"Instances",oStone);
    instance_create_layer(1380,660,"Instances",oStone);
    instance_create_layer(180,900,"Instances",oBee);
    instance_create_layer(1740,900,"Instances",oBee);
    instance_create_layer(180,780,"Instances",oStone);
    instance_create_layer(300,780,"Instances",oStone);
    instance_create_layer(420,780,"Instances",oStone);
    instance_create_layer(780,780,"Instances",oStone);
    instance_create_layer(1140,780,"Instances",oStone);
    instance_create_layer(1500,780,"Instances",oStone);
    instance_create_layer(1620,780,"Instances",oStone);
    instance_create_layer(1740,780,"Instances",oStone);
this is just one option, and when there are 20 of them it looks huge
 

Neptune

Member
If you're wanting to manually build the rooms, then thats the way... But 560 seems very excessive.
If you want to randomly spawn stuff scattered about:
Code:
for(var i = 0; i < 100; i++)
{
    var xx = irandom_range(0,room_width);
    var yy = irandom_range(0,room_height);
    //create instance at xx,yy
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
You'll be spending a lot of time one way or another - whether that's spent on placing instances in template rooms or layers which you then iterate over to re-create them in place, write thousands of lines that spawn instances or make up your own file format which you can then load into your game with a single function call.

If all you want is to reduce the amount of text, you could simply add a script with the syntax spawn(x, y, obj) that calls the code you're repeating thousands of times, but doesn't require you to specify the layer or type out the whole function name every time.
 

sinigrimi

Member
oh my god, I had to split stable objects and enemies into two, because the case "enemy" was cleared when I killed monsters and fixed objects disappeared as well

Code:
  var room_type = irandom(9);//irandom(559);
  case:"fixed":
             if (up == true) and (bottom == true) and (left ==false) and (right ==false){
    ffixed_BeeRoom(room_type);
             }
             break;
    case "enemy": .
         if (up == true) and (bottom == true) and (left ==false) and (right ==false){
             BeeRoom(room_type);
 

sinigrimi

Member
You'll be spending a lot of time one way or another - whether that's spent on placing instances in template rooms or layers which you then iterate over to re-create them in place, write thousands of lines that spawn instances or make up your own file format which you can then load into your game with a single function call.

If all you want is to reduce the amount of text, you could simply add a script with the syntax spawn(x, y, obj) that calls the code you're repeating thousands of times, but doesn't require you to specify the layer or type out the whole function name every time.
I do not quite understand what you mean, here is an example of what I did (my templates for rooms of different types of monsters)

if there is a way to enter it into the code quickly, but now in two parts split tell me please
Photoshop_4Ql9HCPkJZ.png Photoshop_AAx00OXwhT.png Photoshop_bJH2rNiYIH.png Photoshop_opBCtwwS3r.png Photoshop_S48gj6cJWs.png

About the script, are you talking about this?
Code:
instance_create_layer(argument0,argument1,"Instances",argument3);
I also understand that this will be a large code in which only one option will be selected, what should I use switch or if
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
I do not quite understand what you mean, here is an example of what I did (my templates for rooms of different types of monsters)
if there is a way to enter it into the code quickly, but now in two parts split tell me please
You can't really turn images into code. I mean, you could totally write a parser that imports an image, checks specific regions on it and does something based on that, but that would not only require you to ship these images with the game, but also be rather slow. You could do this, then save the result of it (to a data structure and then to a file, for example), then ship those files with your game and load them back into the game at run time.

That would essentially eliminate the performance hit of loading a room at run time, still let you design rooms in Photoshop and even automate translating from image to game-readable data structure (if set up properly).

About the script, are you talking about this?
Code:
instance_create_layer(argument0,argument1,"Instances",argument3);
Yup, except you might want to replace argument3 with argument2.

I also understand that this will be a large code in which only one option will be selected, what should I use switch or if
For semantics, switch.
If you decide to go down the route I suggested above, neither, as you'll pretty much end up only calling the same routine that loads a room file every time you want to load one, just with different IDs.
 

sinigrimi

Member
You can't really turn images into code. I mean, you could totally write a parser that imports an image, checks specific regions on it and does something based on that, but that would not only require you to ship these images with the game, but also be rather slow. You could do this, then save the result of it (to a data structure and then to a file, for example), then ship those files with your game and load them back into the game at run time.

That would essentially eliminate the performance hit of loading a room at run time, still let you design rooms in Photoshop and even automate translating from image to game-readable data structure (if set up properly).


Yup, except you might want to replace argument3 with argument2.


For semantics, switch.
If you decide to go down the route I suggested above, neither, as you'll pretty much end up only calling the same routine that loads a room file every time you want to load one, just with different IDs.
Do you know if there is such a thing on the market? I would buy, since I myself am a noob :D. Thank you very much for the clarification, you are a big brain))
 

sinigrimi

Member
oh, this is horror, I made 400 lines and it really is exhausting! I am ready to buy a code that considers the coordinates of the pictures and writes them to a file so that I can import into gms. If anyone can do this, let me know and announce the amount
 

drandula

Member
Make yourself a ds_grid drawer, you can change the values on grid and then save the grid. Afterwards load things from grid. Now you don't write positions from the code, and works better and faster tham parsing an Image.
 

sinigrimi

Member
Make yourself a ds_grid drawer, you can change the values on grid and then save the grid. Afterwards load things from grid. Now you don't write positions from the code, and works better and faster tham parsing an Image.
if you can do it do it I'll pay
it is necessary that he read the center of the square and not the beginning of the square in the grid. 60 pixels wide squares
 

Attachments

TheouAegis

Member
Room Start Event of first level (make sure all levels are together and no rooms exist after the last level) BEFORE you compile your final project. You'll replace this code with the code after it.
Code:
var object_count = 0, OBJECT_ARRAY = 0;
while object_exists(object_count) object_count++;
for(var i=object_count-1; i|1; i--)
    OBJ_ARRAY[i,8] = 0;
for(var ob, n=0, rw=0, i=0, xx=60, yy=60; yy<room_height; {i++; xx+=120; if i==16 {i=0; xx=60; rw++; yy+=120;};}) {
    ob  = instance_position(xx,yy,all);
    if !ob continue;
    ob = ob.object_index;
    OBJ_ARRAY[ob,rw] |= pow(2,i);
}
var b = buffer_create(object_count*9+2,buffer_fixed,2);
buffer_write(b,buffer_u16,object_count);
for(var i=0,n=0; i<object_count; {n++; if n == 8 {n = 0; i++;};}) {
    buffer_write(b,buffer_u16,OBJ_ARRAY[i,n]);
}
buffer_save(b,room_get_name(room));
buffer_delete(b);
if room_next(room) room_goto_next();
else game_end();
That should (dunno, didn't test, just brainstormed) generate a file for each room. Copy those files to a safe location and then include them in the game's Included Files tab. To load them...

Code:
var rm = room_get_name(room);
if file_exists(rm) {
    var b = buffer_load(rm);
    var OBJ_ARRAY = 0;
    var object_count = buffer_read(b,buffer_u16);
    var n = 0, i;
    while n < object_count {
        i = 0;
        repeat 9 OBJ_ARRAY[n,i++] = buffer_read(b,buffer_u16);
        n++;
    }
    buffer_delete(b);
    n = 0;
    var a,xx,yy;
    while n < object_count {
        i = 0;
        repeat 9 {
            a = OBJ_ARRAY[n,i++];
            xx = 60;
            yy = 60 + 120 * i;
            repeat 16 {
                if a & 1
                    instance_create(xx,yy,n);
                a = a >> 1;
                xx += 120;
            }
        n++;
    }
}
And that should (again, not tested, just brainstormed) be able to open the file and load the objects into the room. You'd run this in the Room Start Event of your final project.
 
Top