Hey everyone, I'm currently trying to find out how to work with GDScriptFunctionStates GDNative C++, so I've written the following test code in GDScript:
extends Node
func _ready() -> void:
print("Starting test.")
var state = FunctionStateTest.new()
print("Created test object.")
var result = state.test(self)
print(result)
func test_func_1() -> float:
yield(get_tree().create_timer(0.5), "timeout")
print("Completed: test_func_1")
return 1.0
func test_func_2() -> float:
yield(get_tree().create_timer(2), "timeout")
print("Completed: test_func_2")
return 4.0
And the following code in GDNative C++:
#include "GDScriptFunctionStateTest.h"
using namespace godot;
void godot::FunctionStateTest::_register_methods()
{
register_method("test", &FunctionStateTest::test);
register_method("_on_v1_calculated", &FunctionStateTest::_on_v1_calculated);
register_method("_on_v2_calculated", &FunctionStateTest::_on_v2_calculated);
}
void godot::FunctionStateTest::_init()
{
}
Variant godot::FunctionStateTest::test(Object* test_object)
{
double value1 = 0.0, value2 = 0.0;
Variant v1 = test_object->call("test_func_1");
if (v1.get_type() == Variant::Type::OBJECT && ((Object*)v1)->get_class() == "GDScriptFunctionState") {
Godot::print(String("V1 Connect to GDScriptFunctionState"));
Ref<GDScriptFunctionState> result = Ref<GDScriptFunctionState>(GDScriptFunctionState::_new());
Ref<GDScriptFunctionState> state1 = v1;
state1->connect(String("completed"), this, String("_on_v1_calculated"), Array::make(Dictionary::make("test_object", test_object), result));
return result;
}
else {
value1 = v1;
}
Variant v2 = test_object->call("test_func_2");
if (v2.get_type() == Variant::Type::OBJECT && ((Object*)v2)->get_class() == "GDScriptFunctionState") {
Godot::print(String("V2 Connect to GDScriptFunctionState"));
Ref<GDScriptFunctionState> result = Ref<GDScriptFunctionState>(GDScriptFunctionState::_new());
Ref<GDScriptFunctionState> state2 = v2;
state2->connect("completed", this, "_on_v2_calculated", Array::make(Dictionary::make("test_object", test_object, "value1", value1), result));
return result;
}
else {
value2 = v2;
}
return value1 + value2;
}
void godot::FunctionStateTest::_on_v1_calculated(Variant result, Dictionary parameters, Ref<GDScriptFunctionState> total_result)
{
Godot::print("completed called");
Object* test_object = parameters["test_object"];
double value1 = result, value2 = 0.0;
Variant v2 = test_object->call("test_func_2");
if (v2.get_type() == Variant::Type::OBJECT && ((Object*)v2)->get_class() == "GDScriptFunctionState") {
Godot::print(String("V2 Connect to GDScriptFunctionState"));
Ref<GDScriptFunctionState> result = Ref<GDScriptFunctionState>(GDScriptFunctionState::_new());
Ref<GDScriptFunctionState> state2 = Object::cast_to<GDScriptFunctionState>(v2);
state2->connect("completed", this, "_on_v2_calculated", Array::make(Dictionary::make("test_object", test_object, "value1", value1), result));
}
else {
value2 = v2;
total_result->emit_signal("completed", value1 + value2);
}
}
void godot::FunctionStateTest::_on_v2_calculated(Variant result, Dictionary parameters, Ref<GDScriptFunctionState> total_result)
{
double value1 = parameters["value1"], value2 = result;
total_result->emit_signal("completed", value1 + value2);
}
However, the method _on_v1_calculated seems to never have been called, as the resulting output is:
Starting test.
Created test object.
V1 Connect to GDScriptFunctionState
Null
Completed: test_func_1
My question is, am I doing something wrong and if so, what, or is it that the GDScriptFunctionState doesn't work in GDNative C++?