GML Create rectangle made of objects

G

Galahas

Guest
I am trying to make a management game. I need the player to be able to assign different "areas" like in Prison architect or Rimworld, by pressing and holding a mouse button to draw a rectangle that defines an area.
Since I'll need objects to detect wether they are in an area or not I'm trying to stay away from 'draw' functions like draw_rectangle.

Is there a way to say 'instance_create_rectangle' or 'instance_create_line'?

I'd like the instances to simply be tiled horizontally and vertically to fill that rectangle.
 

NightFrost

Member
Instances must be created one at a time, but you can simply run a loop that spawns the necessary instances. For drag-selecting an area, you need to figure out the grid coordinates. Well, I don't know how Prison Architect works, but Rimworld just runs on a grid, and everything is grid-aligned. When you detect a drag-start, save start x and y. When drag ends save end x and y. Now div those with grid's cell size to arrive at coordinates. Run a loop to spawn required stuff within the defined area. In pseudocode segments
Code:
// When mousedrag starts
dragx1 = mouse_x,
dragy1 = mouse_y;

// When mousedrag ends
dragx2 = mouse_x;
dragy2 = mouse_y;

// Get grid coords
gridx1 = dragx1 div gridsize;
gridy1 = dragy1 div gridsize;
gridx2 = dragx2 div gridsize;
gridy2 = dragy2 div gridsize;

// Spawn loop
for(gridx1 to gridx2){
    for(gridy1 to gridy2){
        // instance create stuff here
    }
}
Although, going by what I've myself planned for management game systems, I wouldn't run world objects as instances. Since you'd do grid movement, you never need to run collision checks, which removes the biggest reason to instantiate stuff. Let stuff exist just as data in DSes and have landscape draw routines slap down some sprites when necessary.
 
G

Galahas

Guest
Thank you so much! I understood the pseudocode up until the 'for' loop, which I only understood conceptually. I only used 'for' loops a few times so I revised the help section but I'm having trouble translating this
Code:
// Spawn loop
for(gridx1 to gridx2){
    for(gridy1 to gridy2){
        // instance create stuff here
    }
}
into a syntactically correct GML 'for' loop.
 

NightFrost

Member
A loop requires start value, end value and value increase, plus a variable to track that value inside the loop. A simple loop would be:
Code:
for(var i = 0; i <= 9; i++){ // Start at zero, end after 9, increase by one.
    // Variable i will step through values 0 to 9 for code inside the curly braces. The loop will be run in its entirety before code execution continues past it.
}
The idea of a double loop is for each step of the outer loop running the inner loop once. So the grid loop goes:
Code:
for(var xx = gridx1; xx <= gridx2; xx++){
    for(var yy = gridy1; yy <= gridy2; yy++){
        instance_create_layer(xx, yy, "Instances", obj_whatever);
    }
}
For example if gridx1 = 3, gridx2 = 5, gridy1 = 5, gridy2 = 8, the values for inner loop passes would go:
Code:
xx = 3, yy = 5 to 8
xx = 4, yy = 5 to 8
xx = 5, yy = 5 to 8
In other words, each unique coordinate position gets used once.
 
Top