Godot Version
4.3
Question
I’m trying to write a GDExtension that exposes a Component
class that inherits from Object
. The intention is that in GScript the user will inherit Component
and add their own data to it.
What I want is a function that returns the list of user added non-function properties and the size of them. So far if I print the property list of a class that inherits Component
from C++ then the property list is empty.
If I call get_properties_list()
directly from GDScript then I get the additional types that are added to the class, but I also get several additional properties script
or built-in-script
.
So rather than ask for a solution, I am hoping that someone can explain the propety list system to me so that I can write a proper solution that does what I want.
1 Like
I’m a bit confused what you are trying to do, but you can use the class keyword to create objects that inherit from object.
E.g.
test.gd
class_name Test
class Result:
var code: int=0 # Error Code
var description: String="" # Error Message
var value: Variant # Value if no error
func print_me():
print(self.description)
static func get_props(the_class:Variant) -> Dictionary:
var props={}
var flags=PROPERTY_USAGE_SCRIPT_VARIABLE
for prop in the_class.get_property_list():
if(prop.usage & flags>0):
props[prop.name]=the_class.get(prop.name)
return props
some script that uses classes inside test.gd
extends Node2D
func _ready():
var res:Object=Test.Result.new()
res.code=-1
res.description="Error"
res.value=null
print(Test.get_props(res))
res.print_me()
Sorry, I don’t think I explained it quite well. What I am trying to do is have a base class that has a function that only returns the data properties in the classes that derive from it.
E.g.:
// in .cpp
class Base : public Node {
/* Godot boiler plate */
PropertyList get_property_list() // <-- returns the data types stored in a derived class
}
And then in Godot you would have something like:
class_name Derived extends Base
var code: int=0 # Error Code
var description: String="" # Error Message
var value: Variant # Value if no error
extends Node
func _ready():
var test: Derived = Derived.new()
var properties = test.get_property_list()
for property in properties:
print(property)
Which would end up printing something along the lines of :
"code" : Int
"description": String
"value": Variant
But doesn’t include the additional properies like script
or built-in-script
etc.