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.