![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Babko |
Here is how my class looks like;
#pragma once
#include "Common.h"
#include "KinematicBody2D.hpp"
#include "RandomNumberGenerator.hpp"
#include "KinematicCollision2D.hpp"
#include "Timer.hpp"
#define BLUEBERRY_ACCELERATION 900
#define BLUEBERRY_FRICTION 600
#define BLUEBERRY_MAX_SPEED 25
namespace godot {
class BlueberryNPC : public KinematicBody2D {
GODOT_CLASS(BlueberryNPC, KinematicBody2D)
private:
Vector2 velocity;
Vector2 inputVector;
godot::RandomNumberGenerator* RNG;
godot::Timer* timer;
bool move;
public:
int health;
BlueberryNPC();
~BlueberryNPC();
static void _register_methods();
void _init();
void _ready();
void _process(float delta);
const void take_damage();
};
}
So, I can easily call funtcions that derive from Godot::Node on my BlueberryNPC*:
auto collision = move_and_collide(velocity.normalized() * delta * speed);
if (collision != nullptr) {
godot::Node * collider = (godot::Node * ) collision -> get_collider();
if (collider -> is_in_group("BlueberryNPC")) {
godot::BlueberryNPC * myBlueberryNPC = (godot::BlueberryNPC * ) collider;
myBlueberryNPC -> queue_free();
}
}
but I can’t use variables defined in my class above (class BlueberryNPC : public KinematicBody2D):
auto collision = move_and_collide(velocity.normalized() * delta * speed);
if (collision != nullptr) {
godot::Node * collider = (godot::Node * ) collision -> get_collider();
if (collider -> is_in_group("BlueberryNPC")) {
godot::BlueberryNPC * myBlueberryNPC = (godot::BlueberryNPC * ) collider;
myBlueberryNPC->health = 10; // Doesn't crash but doesn't change the value either
myBlueberryNPC->take_damage(); // Calls the function fine but only things (by things I mean code inside my take_damage() function) that derive from Godot::Node itself work, like queue_free() or Godot::print()
}
}
I know that I can use something like godot::Node::set() to set my variables’ value, but I don’t understand why setting them this way doesn’t work. Is it even possible?
Btw, doubt that it matters, but I’m comipiling for Release.