• 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 Can you get a value from an object without referencing it in code?

L

LuckyOwl

Guest
Due to how easy it may be to misunderstand the question, let me elaborate.

for example, I want my cursor to draw the name of the object it is colliding with, one way I know how to do this, is to simply preform a simple collision check, as follows

if (place_meeting(x,y,exampleobject))
{
Draw_text(x,y,string(exampleobject.displayname);
}
Doing this is easy, yet time consuming, especially when the project may require more than 10 items, is there any way to get the object that is being collided with and maybe set it as a variable?
 
L

LuckyOwl

Guest
Use a collision function that returns the id instead of a boolean.
Thanks for the reply, Ive only recently come back to GMS2 so Im kind of inexperienced when it comes to code that isnt just "the basics", could you maybe give an example of a function like this?
 

FrostyCat

Redemption Seeker
You need to learn the difference between objects and instances. If exampleobject.displayname makes even the slightest sense to you in that context, you have work to do.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ but don't end in _list all return instance IDs. Save that instance ID into a variable (e.g. var inst_enemy = instance_place(x, y, obj_enemy);), then use that as the subject to work with (e.g. inst_enemy.hp -= 10;). Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
GML:
var inst = instance_place(x, y, exampleobject)
if (inst != noone)
{
    draw_text(x, y, inst.displayname);
}
 
Top