Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get id of type by instance is impossible #82519

Open
iRumba opened this issue Sep 29, 2023 · 3 comments
Open

Get id of type by instance is impossible #82519

iRumba opened this issue Sep 29, 2023 · 3 comments

Comments

@iRumba
Copy link

iRumba commented Sep 29, 2023

Godot version

4.1.1.stable

System information

Windows 10, GLES2, Intel(R) UHD Graphics 630

Issue description

I have class A

class A extends Object:
    pass

I have instance

var a = A.new()

I have some configuring for types

var dict: Dictionary = {
    A: 1,
    MyOtherClass: 2
}

So I can get config for A var conf = dict[A]
But I can't get config for instance var conf = dict[a]

I understand that 'a' and 'A' are diferent objects, but in C# I can do this

private Dictionary<Type, int> dict.....

Then I can fill dict by typeof(A), typeof(MyOtherClass) etc...

After it I can get conf by var conf = dict[a.GetType()]

I can't do it in GDScript

I don't want add in my classes func

class_name A

func get_my_type():
    return A

What can I do outside class script to get what I want (get A from a)?

Steps to reproduce

Nothing

Minimal reproduction project

Nothing

@iRumba
Copy link
Author

iRumba commented Sep 29, 2023

I found one solution.

var a = A.new()
var script: GDScript = A
print (a.get_script() == script) #true

So, I can indexing my config by GDScript or script.get_instance_id()

@dalexeev
Copy link
Member

See also:

Godot has a three-level type system:

  1. Variant types, for example TYPE_INT. You can get this via typeof().
  2. Native types (only for TYPE_OBJECT), for example Node. You can get this via get_class() and check it via is_class() (with inheritance).
  3. Script types (only for TYPE_OBJECT, optional). You can get this via get_script().

There are also typed arrays (they have get_typed_*() methods for the three levels) and "ephemeral" types: enumerations and bit masks (at runtime they are just int).

In GDScript you can use the is operator (and is_instance_of() function for dynamic types) to check, it works with all 3 levels and typed arrays. But there is no uniform way to get the type of an value as some first-class value (a type representing a type).

I found one solution.

Will the issue be considered resolved?

@iRumba
Copy link
Author

iRumba commented Sep 29, 2023

Godot has a three-level type system:

Variant types, for example TYPE_INT. You can get this via typeof().
Native types (only for TYPE_OBJECT), for example Node. You can get this via get_class() and check it via is_class() (with inheritance).
Script types (only for TYPE_OBJECT, optional). You can get this via get_script().
There are also typed arrays (they have get_typed_*() methods for the three levels) and "ephemeral" types: enumerations and bit masks (at runtime they are just int).

In GDScript you can use the is operator (and is_instance_of() function for dynamic types) to check, it works with all 3 levels and typed arrays. But there is no uniform way to get the type of an value as some first-class value (a type representing a type).

Yeah, I tryed different variants.

  • get_class() returns String and not guaranted unique
  • "instance_of" and "is" make using type dictionary not possible. I need enumerate my dict for finding my type. Also this methods return true with base class (a is B return true if B extends A)

Will the issue be considered resolved?

I dont know. May be someone find more clear method for my task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants