Godot Version
4.3
Question
In my extensions below, what is the correct way to connect the signal emitted from MyButton
to SpriteMoving
? Seems like it should be simple, what am I missing?
I’ve exhausted what documentation is available, pored over implementation in the source code, tried with singletons, etc.
As a side-note:
Are the variant/type constants in GDScript part of its syntax, or literals attached to variables/enums in the source code? ex: myVector = Vector2.UP
or PI
.
I’d like to write something like myVector = godot::Vector2::UP;
instead of myVector = godot::Vector2(0, -1);
in my extensions, without needing to define my own constants.
In the Godot Engine source code, I’ve viewed the implementation of: vector, input_enums, vector2, etc. But I didn’t find any constant variables/enums.
Thank you for your time.
[sprite_moving.h]
#pragma once
#include <godot_cpp/classes/sprite2d.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
namespace g = godot;
namespace Movement
{
class SpriteMoving: public g::Sprite2D
{
GDCLASS(SpriteMoving, g::Sprite2D)
public:
SpriteMoving()
{
connect("my_signal", g::Callable(this, "helloThere"));
}
~SpriteMoving()
{}
void _process(double delta) override;
void helloThere() const { g::UtilityFunctions::print("Hello there!"); }
double getFactor() const { return factor; }
void setFactor(const double p_speed) { factor = p_speed; }
protected:
static void _bind_methods(); // Required.
private:
double factor{1.0}; // Speed factor.
double speed{100.0};
double angularSpeed{3.141592653589793};
int direction{};
g::Vector2 velocity{};
};
}
[sprite_moving.cpp]
#include "sprite_moving.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/input.hpp>
namespace g = godot;
namespace Movement
{
void SpriteMoving::_bind_methods() // Required.
{
g::ClassDB::bind_method(g::D_METHOD("getFactor"), &SpriteMoving::getFactor);
g::ClassDB::bind_method(g::D_METHOD("setFactor", "p_speed"), &SpriteMoving::setFactor);
ADD_PROPERTY(g::PropertyInfo(g::Variant::FLOAT, "speed", g::PROPERTY_HINT_RANGE, "0,10,0.01"), "setFactor", "getFactor");
g::ClassDB::bind_method(g::D_METHOD("helloThere"), &SpriteMoving::helloThere); // Needed for signal?
}
void SpriteMoving::_process(double delta)
{
if (!g::Engine::get_singleton()->is_editor_hint()) // So code doesn't run in editor.
{
direction = 0;
if (g::Input::get_singleton()->is_action_pressed("ui_left")) { direction = -1; }
if (g::Input::get_singleton()->is_action_pressed("ui_right")) { direction = 1; }
rotate(angularSpeed*direction*delta);
velocity = g::Vector2(0, 0);
if (g::Input::get_singleton()->is_action_pressed("ui_up")) { velocity = g::Vector2(0, -1).rotated(get_rotation())*(speed*factor); }
translate(velocity*delta);
}
}
}
[my_button.h]
#pragma once
#include <godot_cpp/classes/button.hpp>
namespace g = godot;
namespace Movement
{
class MyButton: public g::Button
{
GDCLASS(MyButton, g::Button)
public:
MyButton()
{
//connect("my_signal", g::Callable(this, "helloThere"));
}
~MyButton()
{}
void _pressed() override;
protected:
static void _bind_methods(); // Required.
private:
};
}
[my_button.cpp]
#include "my_button.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/core/class_db.hpp>
namespace g = godot;
namespace Movement
{
void MyButton::_bind_methods() // Required.
{
ADD_SIGNAL(g::MethodInfo("my_signal"));
}
void MyButton::_pressed()
{
//g::UtilityFunctions::print("Hello there!");
emit_signal("my_signal");
}
}