Accessing a custom object's (that derives from KinematicBody2D) variables, etc.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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.

:bust_in_silhouette: Reply From: Babko

Okay, I’ve managed to solve it myself;

    auto collision = move_and_collide(velocity.normalized() * delta * speed);

    if(collision != nullptr) {

        godot::Object* colliderObject = collision->get_collider();

        if(collider->is_in_group("BlueberryNPC")) {
            godot::BlueberryNPC* colliderBluberry = godot::Object::cast_to<godot::BlueberryNPC>(colliderObject); // this is how you're supposed to cast it seems

            colliderBluberry->health = colliderBluberry->health - 8; // WORKS

            } 
     }