Nullptr when passing one userdata to another
notlion opened this issue · comments
Ryan Alexander commented
Userdata is becoming null for some reason when passed to a method of another userdata class. When used by the same class there's no problem. Simple test case:
struct Vec2 {
float x = 0;
float y = 0;
Vec2(float x_, float y_) : x{x_}, y{y_} {}
};
struct Incr {
Vec2 incr(const Vec2& v) {
return Vec2(v.x + 1, v.y + 1); // Eek! v is nullptr
}
};
lua.new_userdata<Vec2, float, float>("Vec2");
lua.new_userdata<Incr>("Incr", "incr", &Incr::incr);
lua.script("a = Vec2.new(10, 20)\n"
"i = Incr.new()\n"
"b = i:incr(v)\n"
"print(b.x, b.y)");
Danny commented
In your test case, v
doesn't exist. Use a
instead.
Ryan Alexander commented
Well I feel like an idiot. You're right. That test case actually works when the proper variable name is used.
I did track down the actual bug, though. I had typed b = i.incr(a)
instead of b = i:incr(a)
. With the dot notation incr()
is actually called, but is passed a reference to a null value.
I'm not sure if you'd consider this a bug or not, but it was confusing. Sorry for the false alarm.
Danny commented
No problem. Things happen.