GMS 2.3+ Getting the name of a constructor

netoxinaa

Member
Hello. I'm having a little issue trying to compare an item object with its constructor.
I have this constructor function called Cellphone and my player has in its inventory an object made from that constructor. So what I want is to check if the player has in its inventory the item Cellphone.
So I have this:
GML:
if (instanceof(itemHeld) == _item) return true;
where itemHeld it's the item in my inventory and _item it's an argument (in this case, it is the constructor's name: Cellphone).

instanceof (itemHeld) returns the string "Cellphone", which is ok, but _item returns an instance id (10027), so the check returns false. Changing _item to instanceof( _item ) just returns undefined.
I could easily just type if ... == "Cellphone" for this one but I want it to work with any other item possible.
Is there a way I can get the name of a constructor to achieve this? Something like how object_index works with normal instances?
 

Roldy

Member
if instanceof(_item) return undefined then _item is not a struct.

if _item is an instance of an object you will be unable to compare structs to instances without making some other mapping.
 

Roldy

Member
that's also something I don't understand, why the constructor's reference returns an id? shouldn't it just return its reference name?
You will need to explain what _item is; an instance, a string, a real, a method etc...

if _item references and actual constructor function then _item is a Real (a number representing a function script index).

GML is not strongly typed, and what you are trying to do has very little support in the language.

As I previously said, you probably want to make a more concrete mapping. Post more of your code so people can have an idea of what is going on.
 
Last edited:

netoxinaa

Member
@Roldy That's literally just all the relevant code :/ but it's good to know that my approach isn't the one. A solution I came up with is to create another object from the _item constructor and use it to compare it with itemHeld. I just wanted to know if my approach was possible or not. Thanks!
 
Top