2021-09-08 18:11:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* example.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
#include "example.h"
|
|
|
|
|
|
|
|
#include <godot_cpp/core/class_db.hpp>
|
|
|
|
|
|
|
|
#include <godot_cpp/classes/global_constants.hpp>
|
|
|
|
#include <godot_cpp/classes/label.hpp>
|
|
|
|
#include <godot_cpp/variant/utility_functions.hpp>
|
|
|
|
|
|
|
|
using namespace godot;
|
|
|
|
|
2021-09-24 15:11:50 +00:00
|
|
|
ExampleRef::ExampleRef() {
|
|
|
|
UtilityFunctions::print("ExampleRef created.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ExampleRef::~ExampleRef() {
|
|
|
|
UtilityFunctions::print("ExampleRef destroyed.");
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
void Example::_bind_methods() {
|
|
|
|
// Methods.
|
|
|
|
ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
|
|
|
|
ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
|
2021-09-24 15:11:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
|
2021-09-28 13:49:53 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks);
|
2021-09-16 07:21:40 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
|
2021-08-19 17:51:41 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
MethodInfo mi;
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
|
|
|
|
mi.name = "varargs_func";
|
|
|
|
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Properties.
|
2021-09-21 13:55:05 +00:00
|
|
|
ADD_GROUP("Test group", "group_");
|
|
|
|
ADD_SUBGROUP("Test subgroup", "group_subgroup_");
|
2021-09-15 03:56:20 +00:00
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
|
2021-09-15 03:56:20 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
|
2021-08-19 17:51:41 +00:00
|
|
|
|
|
|
|
// Signals.
|
|
|
|
ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
|
|
|
|
ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
|
|
|
|
|
|
|
|
// Constants.
|
|
|
|
BIND_ENUM_CONSTANT(FIRST);
|
|
|
|
BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
|
|
|
|
|
|
|
|
BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
|
|
|
|
}
|
|
|
|
|
2021-09-16 07:21:40 +00:00
|
|
|
Example::Example() {
|
|
|
|
UtilityFunctions::print("Constructor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Example::~Example() {
|
|
|
|
UtilityFunctions::print("Destructor.");
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Methods.
|
|
|
|
void Example::simple_func() {
|
|
|
|
UtilityFunctions::print("Simple func called.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Example::simple_const_func() const {
|
|
|
|
UtilityFunctions::print("Simple const func called.");
|
|
|
|
}
|
|
|
|
|
|
|
|
String Example::return_something(const String &base) {
|
|
|
|
UtilityFunctions::print("Return something called.");
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
Viewport *Example::return_something_const() const {
|
|
|
|
UtilityFunctions::print("Return something const called.");
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
Viewport *result = get_viewport();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:11:50 +00:00
|
|
|
ExampleRef *Example::return_extended_ref() const {
|
|
|
|
return memnew(ExampleRef());
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:49:53 +00:00
|
|
|
Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
|
|
|
|
Ref<ExampleRef> ref;
|
|
|
|
ref.instantiate();
|
|
|
|
// TODO the returned value gets dereferenced too early and return a null object otherwise.
|
|
|
|
ref->reference();
|
|
|
|
UtilityFunctions::print("Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) {
|
2021-11-12 10:03:29 +00:00
|
|
|
UtilityFunctions::print("Varargs called with ", String::num((double)arg_count), " arguments");
|
2021-08-19 17:51:41 +00:00
|
|
|
return arg_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Example::emit_custom_signal(const String &name, int value) {
|
|
|
|
emit_signal("custom_signal", name, value);
|
|
|
|
}
|
|
|
|
|
2021-09-16 07:21:40 +00:00
|
|
|
Array Example::test_array() const {
|
|
|
|
Array arr;
|
|
|
|
|
|
|
|
arr.resize(2);
|
|
|
|
arr[0] = Variant(1);
|
|
|
|
arr[1] = Variant(2);
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Properties.
|
|
|
|
void Example::set_custom_position(const Vector2 &pos) {
|
|
|
|
custom_position = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Example::get_custom_position() const {
|
|
|
|
return custom_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Virtual function override.
|
2021-08-19 23:03:11 +00:00
|
|
|
bool Example::_has_point(const Vector2 &point) const {
|
2021-08-19 17:51:41 +00:00
|
|
|
Label *label = get_node<Label>("Label");
|
|
|
|
label->set_text("Got point: " + Variant(point).stringify());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|