GMS 2 How to determine an object's type after upcasting it?

M

mgast

Guest
SOLVED: The problem was that I forgot I had another class that was a child of oAnimal and parent of oCat/oDog. The nearest instance of oAnimal wasn't oCat or oDog, it was oSmallAnimal.

So like this: oAnimal -> oSmallAnimal -> oCat/oDog
_____________________________________________________________________________




I have an object oAnimal, with its two children oCat and oDog.

I want to see if the player is near either animal, so I do this in player's step:
Code:
var nearest = instance_nearest(x, y, oAnimal);
Now I want to change what happens based on what animal I'm near, something like this:
Code:
switch(nearest.object_index)
{
    case(oCat):
        break;
    case(oDog):
        break;
}
In my real code, I have too many children to try to get the nearest of each, so I need to be able to find the closest one in a single statement. Once the nearest thing is upcasted to the parent class though, it seems to lose the information of what it used to be.

How do I determine if "nearest" was an oCat or oDog?

Please help!
 
Last edited by a moderator:
M

mgast

Guest
Oh gosh, I tried that before but I just wasn't using it right. I fixed it now, thank you!
 
Last edited by a moderator:
Top