Advertisement

Null pointer access error when defining a class member

Started by March 17, 2024 10:22 PM
3 comments, last by -johndoe 8 months, 2 weeks ago

First of all, I'd like to thank the author of AngelScript. It's been the best and easiest scripting language I've worked with so far.

So I'm encountering a null pointer access error when I try to define the value of a class member which is a reference type script object. I have properly registered it with proper factory functions and everything. I can create new instances of it just fine when I directly define it in a variable; however, if I declare a class member with that type and then define its value in the constructor, I get the error, and then my application crashes.

Here's what my class looks like:

class Rectangle
{
public:
    math::Vector2 position;
    float width;
    float height;
    uint8_t type;
    Rectangle(): position({0.0f, 0.0f}), width(0.0f), height(0.0f), type(0) {}
    Rectangle(math::Vector2 position, float width, float height, uint8_t type = RectangleType::STATIC);
}

Here's how the copy constructor looks:

Rectangle &Rectangle::operator=(const Rectangle *rect)
{
    this->position = rect->position;
    this->width = rect->width;
    this->height = rect->height;
    this->layers = rect->layers;
    return *(this);
}

This is how the class is registered:

r = engine->RegisterObjectBehaviour("Rectangle", asBEHAVE_FACTORY, "Rectangle@ f(Vector2, float, float, uint8)", asFUNCTIONPR(RefFactory, (math::Vector2, float, float, uint8_t), Rectangle*), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("Rectangle", asBEHAVE_LIST_FACTORY, "Rectangle@ f(const int &in) {Vector2, float, float, uint8}", asFUNCTION(RefFactoryList), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectMethod("Rectangle", "Rectangle @opAssign(Rectangle@)", asMETHODPR(Rectangle, operator=, (const Rectangle*), Rectangle&), asCALL_THISCALL)

And this is what my script looks like:

#include "BaseObject.as"

class Player: BaseObject
{
    Rectangle @hitbox; // declaring the reference type member
    Player(json data)
    {
        hitbox = {{ 0.0f, 0.0f }, 10.0f, 10.0f, 2}; // defining its value
        print("Object constructed");
    }

    void Start()
    {
        print("Start method called");
    }

    void Update()
    {
        if (IsKeyPressed(KEY::SPACE)) {
            print("Space key pressed");
        }
    }
}

Like I explained, before, I get no errors if I define an insance of this type before declaring it first, but if I declare it before and then define its value like in the example above, I get a null pointer access error. I'd really appreciate it if somoene could explain how I can fix this issue. Thanks in advance.

Edit: Apologies for posting this under the artificial intelligence forum. I was sure I postetd it under Angelcode, but I guess the setting got reset when I refreshed the page.

-johndoe said:
Edit: Apologies for posting this under the artificial intelligence forum.

NP, it's moved to Angelcode.

-- Tom Sloper -- sloperama.com

Advertisement

Thanks for the compliments. I'm pleased you like my work.

Presumably the null pointer exception is when you attempt to initialize the hitbox member in the constructor.

-johndoe said:
hitbox = {{ 0.0f, 0.0f }, 10.0f, 10.0f, 2}; // defining its value 

The statement you've written is a value assign, i.e. it will attempt to copy the value defined with the initialization list to an already initialized Rectangle, which is what is giving you the null pointer error.

Add the @ symbol to show you really want a handle assignment.

@hitbox = {{ 0.0f, 0.0f }, 10.0f, 10.0f, 2};

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

WitchLord said:
Add the @ symbol to show you really want a handle assignment.

@hitbox = {{ 0.0f, 0.0f }, 10.0f, 10.0f, 2};

That actually solved the problem. I didn't tihk to look at the scripting language section in the docs thinking all I need would be the developer manual. Thank you!

This topic is closed to new replies.

Advertisement