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

GML Simple Problem: place_free() checking

Posho

Member
Hello,

I'm stuck with the simplest of things. I'm looping through the whole room to add objects on empty spots like this:
GML:
for (var i=0; i<room_width; i+=len)
    for (var j=0; j<room_height; j+=len)
        if (place_free(i, j))
            instance_create_depth(i, j, 8, obj_water);
^ THIS works, but if I change the if condition from if (place_free(i, j)) to if (!place_free(i, j)) to add objects on top of non-empty spots it doesn't do anything. Why? :confused:
 

Nidoking

Member
What is the bounding box of the object running this code? Is it an obj_water, or an object that has the same bounding box as an obj_water? Or is it something else?
 

Posho

Member
What is the bounding box of the object running this code? Is it an obj_water, or an object that has the same bounding box as an obj_water? Or is it something else?
The object running the code is spriteless. I thought it was somehow an issue involving collision considering the manual section for place_free() mentions it, so I assigned a sprite the same size as all the other objects in the game, but the outcome is still the same.

[Edit]
Also doesn't work if I make all the other objects solid.
 

Nidoking

Member
Well, place_free basically says "Do I, the instance executing this function, lie on top of a solid instance when placed at these coordinates?" Make sure that in addition to the sprite, the object running the code also has a bounding box the same size as the instances you're trying to place. You might also consider collision_rectangle instead of instance_place, so you don't need an actual instance with a bounding box to run the check.
 

Posho

Member
What is the bounding box of the object running this code? Is it an obj_water, or an object that has the same bounding box as an obj_water? Or is it something else?
For some reason it started working as intended when I added a sprite to the object. It didn't save the first time but it's working now.
THANK YOU đź‘€
 

Sabnock

Member
Hello,

I'm stuck with the simplest of things. I'm looping through the whole room to add objects on empty spots like this:
GML:
for (var i=0; i<room_width; i+=len)
    for (var j=0; j<room_height; j+=len)
        if (place_free(i, j))
            instance_create_depth(i, j, 8, obj_water);
^ THIS works, but if I change the if condition from if (place_free(i, j)) to if (!place_free(i, j)) to add objects on top of non-empty spots it doesn't do anything. Why? :confused:
Looks like you forgot your {}

GML:
for (var i=0; i<room_width; i+=len) {
    for (var j=0; j<room_height; j+=len) {
        if (place_free(i, j)) instance_create_depth(i, j, 8, obj_water);
   }
}
although i have just tried some code without {} and it works so go figure ???!!!
 
Last edited:
Looks like you forgot your {}

GML:
for (var i=0; i<room_width; i+=len) {
    for (var j=0; j<room_height; j+=len) {
        if (place_free(i, j)) instance_create_depth(i, j, 8, obj_water);
   }
}
although i have just tried some code without {} and it works so go figure ???!!!
If there's only a single line to be executed after an if, for, etc statement, you can omit the curly brackets and it will run fine. IMO, though, it's better to be consistent and always include curly brackets just so you don't cause yourself an unnecessary bug by doing something silly while omitting the brackets, but that's just me.
 

Sabnock

Member
it's better to be consistent and always include curly brackets just so you don't cause yourself an unnecessary bug
i couldn't agree more. it looks confusing to me and would make it much harder to read if looking for errors but each to their own. if it works it works.
 
Top