GML How to figure out if ANY object of a specific subtype exists at position X,Y

Y

yinyie

Guest
Hey there sorry if the question is dumb, Im new and unsure about what functions are available to me at this point.

I have currently set up a "button" which when clicked spawns a new instance of an object at a specific position.

What i want to do is modify the position at which the instance is spawned IF any other instance already exists there.

I first tried DND with if "any object at place" which sounds like it does what i want to do until i checked the codeit creates and place_empty seems to return if a collision event is happening at the position (as one object doesnt collide with itself afaik ... this does something entirely different to what it claims it does)

I could try to use "get_position" if there was only one instance present which i potentially have to check for but as it stands the game im trying to create could potentially have multiple instances of multiple objects at any given time (say 50 objects and up to 20 instances of each) trying to get position of every single potentially existing instance is a complete waste of my time and ressources so im wondering if theres any function that could help me here.

My first idea is to spawn a test instance at the position first then use place_empty and delete the the test instance afterwards but that doesnt feel clean and more like im working around the code instead of with it.
 

O.Stogden

Member
Place_empty should return true if there are no objects there.

You could use parents to check for a certain group of objects.

Code:
if place_empty(x,y)
{
instance_create(obj,x,y)
}
else
{
instance_create(obj,altx,alty)
}
If using parents to detect object, you could use:

Code:
if !instance_position(x,y,par_object)
{
instance_create(obj,x,y)
}
else
{
instance_create(obj,altx,alty)
}
Or something to that effect.
 
Y

yinyie

Guest
The code you show me is pretty much exactly what i was using (well except that im using instance_create_layer as instance_create doesnt seem to exists o_O

This is the Description i found of place_empty


Description
You can use this function to check and see if the calling instance would collide with any other instance in your game. Now, it should be noted that for this to work, the instance running the code must have a valid collision mask (either for the sprite itself, or through the mask_index.) and it will only register collisions with those instances that also have a valid mask.

The function itself basically works by taking the instance and testing for collisions when placed at the position specified by the x/y arguments. The collision checking can be either precise or based on the bounding box of the instance, depending on what kind of collision mask has been selected.

Note that the given x/y coordinates will be floored to the nearest integer before the check is performed.
 

FrostyCat

Redemption Seeker
Y

yinyie

Guest
Thank you, i got it to work now :) I cant say what exactly the problem was tho, i just shuffled around the order i did things in after reading the link you sent me, thanks!
 
Y

yinyie

Guest
Or not ... any reason why:

Code:
if (!position_empty(300, 300))
{
    global.spawnposition_x += 100;
    if(!position_empty(400,300))
    {
        global.spawnposition_x += 100;
  
    }
}
else
{
    global.spawnposition_x = 300;
}
Doesnt work at all ... but:
Code:
if (!position_empty(300, 300))
{
    global.spawnposition_x = 400;
    if(!position_empty(400,300))
    {
        global.spawnposition_x = 500;
  
    }
}
else
{
    global.spawnposition_x = 300;
}
sorry im still getting familiar with this environment but it seems as if global.spawnposition_x can not really be modified and only changes if its set to a new value?!?!

ill have to do this check 10 times and want to while it but its not possible if += refuses to function.
 

FrostyCat

Redemption Seeker
Define "doesn't work at all." Using this phrase without further explanation is one of the biggest no-nos when asking for help with code.

If you mean getting a "variable not set" error, your first example is almost sure to invite one. += and all other "relative" operators requires the variable to already have a value. If you don't have something like global.spawnposition_x = 300; running ahead of time (here or an earlier event), that's where your code fails.
 
Y

yinyie

Guest
Define "doesn't work at all." Using this phrase without further explanation is one of the biggest no-nos when asking for help with code.

If you mean getting a "variable not set" error, your first example is almost sure to invite one. += and all other "relative" operators requires the variable to already have a value. If you don't have something like global.spawnposition_x = 300; running ahead of time (here or an earlier event), that's where your code fails.
Okay im sorry, unfortunately its totally unexplainable to me. The variable is being set for the first time in a setup room ahead of the main game together with all other global variables -so theres no issue there i guess.
I get no error message, nothing, but the result is that it always spawns at 300 300 no matter any coditionals (my head tells me both versions should produce the exact same result tho. I have dnd set in the main project, could that change something with how the creation of vars is treated?

to be frank i use dnd occasionally when im lazy and its quite helpful to learn about the existance of functions ect.(basically create dnd thingy -> convert to language, see how it operates, rewrite in gml is my process) tho i switch to gml in 99% of all cases when anything more then spawning a single instance is required.

Edit:
The code youre seeing here is being executed as a step event by my main player object, so the spawn location updates in real time.
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
The code youre seeing here is being executed as a step event by my main player object, so the spawn location updates in real time.
Do you mentally run your code line-for-line before actually running it? If you did, you should see the problem with the += version in less than a minute.

Every step when (300, 300) is occupied, you increase global.spawnposition_x by at least 100. With 30 or 60 steps per second, that value will be sky-high in a matter of seconds. It spawns once at (300, 300) and the second time well off the right side of the room.

Another potential issue with both versions is that creating an instance at (300, 300) won't always create a point collision at (300, 300). For example, if the object's sprite collision mask is a circle and the origin is at the top-left corner, creating an instance of it at (300, 300) will still leave (300, 300) empty. You need to either create in a different position such that (300, 300) is occupied, or check another position that would be occupied (say (350, 350) if it is 100 pixels wide and tall).

Computers do what you say, not what you want. People who see computers as slaves and themselves as masters always fall short. Only people who see computers as uncooperative workers on a work-to-rule strike and themselves as contract lawyers manage something useful.
 
Y

yinyie

Guest
Hmmm, thanks for your input yes it would be sky high but ive limited that in another section of my code but maybe theres an issue there i will check that, i kinda forgot about it.

The point collision problem is something i noticed during testing but is only an issue in my current test build the final version will not allow for any overlap to happen. Working on a grid system right now which the units will snap to.

Edit:

thinking about it... its a pretty 💩💩💩💩ty idea to run the limiting function in a seperate instance, im a retard and will merge the two then see if it works.
 
Top