Is there a way to specify "any instance" with objects?

Awnser228

Member
I'm trying to make a tile-based movement, and in order to handle collisions before the player moves off the grid, I need to check if there is a "solid" object in front of them. My current solution is to assign the boolean variable "walkable" to an object and then compare against it, but I've hit a roadblock; all the functions I can find innate to GameMaker need you to specify a discreet instance of an object to compare against. How can I check fo any potential object that might occupy an area on the screen?

For completeness, here is my current code. I'm using "any_object" as a placeholder for what I want to do, though if you have a solution which gets around this then I'm happy to hear it.

GML:
//Right
if (position_meeting(x+96,y,"any_object")) && (instance_position(x+96,y,"any_object").walkable)
{
    if (keyboard_check(ord("D"))) && (!moving)
    {
        moving = true;
        target_x += 96;
        sprite_index = spr_playerright;
    }
} else { sprite_index = spr_playerright; }
 

Mike

nobody important
GMC Elder
have you tried "all"? This is a keyword meaning ALL objects (like "noone" is none..)

or if you want to specify a specific object type, use that object name.
 

Awnser228

Member
have you tried "all"? This is a keyword meaning ALL objects (like "noone" is none..)

or if you want to specify a specific object type, use that object name.
So this is what I mean; I don't want to specify all objects at once, I want to reference whatever object is at that location at the time.

And I don't want to specify the object by name because I want it to be generic code which can apply to any number of potential objects.
 

Mike

nobody important
GMC Elder
First, the more instances you put through these functions, the slower everything will go. If you try and put everything through it, then it may well test walls, text, file controllers etc (if you have these things of course).... you should really narrow it down to the "types" of objects you know will rest in this location. use Parent objects to help you do this.

Second... once you have a parent object that you can test, then do the instance_position() outside the IF and assign to a variable, then you can check it after you find one - or if there is nothing there, you'll get a "noone" back (I think). Not everything has to be on the same line....

But I'd seriously suggest getting some parent objects arranged so you can narrow down the objects/instances being tested....
 

Awnser228

Member
First, the more instances you put through these functions, the slower everything will go. If you try and put everything through it, then it may well test walls, text, file controllers etc (if you have these things of course).... you should really narrow it down to the "types" of objects you know will rest in this location. use Parent objects to help you do this.

Second... once you have a parent object that you can test, then do the instance_position() outside the IF and assign to a variable, then you can check it after you find one - or if there is nothing there, you'll get a "noone" back (I think). Not everything has to be on the same line....

But I'd seriously suggest getting some parent objects arranged so you can narrow down the objects/instances being tested....
Hmm ok, cheers for the advice. I'll do some research into parent objects!
 

Yal

šŸ§ *penguin noises*
GMC Elder
Parents are the way to go when you have natural categories (terrain, enemy, collectible)... it's a bit of a shirked concept these days when there's "aspect oriented" programming (e.g. you can have separate systems for "collides with terrain", "is flammable" and "can pathfind" - enemies have all three, but some enemies have metal armor and isn't flammable, and items collide with terrain but can't pathfind - so you basically can pick and mix behaviors as you see fit) but it works pretty well for simple cases where the hierarchy is clear from the beginning.
 
Top