-
Hi Context: There is two external .h files, one with this struct and another with this function: typedef struct Vector2 { float x; float y; } Vector2;// from raylib.h
Vector2 Vector2Add(Vector2 v1, Vector2 v2); // from raymath.h In my .nelua file, I imported the struct in this way: global Vector2 <cimport, nodecl> = @record{x: float32, y: float32} then, I imported the global Raymath = @record{}
function Raymath.Vector2Add(v1: Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end but, is would be nice to have an Add method on Vector2 too, so I tried the following: function Vector2:Add(v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end
-- test:
local x: Vector2 = { 0, 10 }
local x2: Vector2 = { 10, 0 }
x:Add(x2) however, it's rendered as: // [...]
// I abbreviated the names
static Vector2 x = {0, 10};
static Vector2 x2 = {10, 0};
// [...]
// this fails because Vector2Add expects an ``Vector2``
Vector2Add(&x, x2); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
When using function Vector2:Add(v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end is really a shortcut for: function Vector2.Add(self: *Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end As you can see is wrong, because the imported C function doesn't expect a pointer in the function Vector2.Add(v1: Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end then the Plus TIP: On the very last commit I've implemented binary operator metamethods for records so now you should be able to do: function Vector2.__add(v1: Vector2, v2: Vector2): Vector2 <cimport'Vector2Add', nodecl> end then this should work: local x: Vector2 = { 0, 10 }
local y: Vector2 = { 10, 0 }
local z = x + y The same metamethods of Lua5.3 is available for overloading, that is:
|
Beta Was this translation helpful? Give feedback.
When using
:
, the self argument is always declared as a pointer to the object, so this code:is really a shortcut for:
As you can see is wrong, because the imported C function doesn't expect a pointer in the
v1
argument.In this case you need to explicitly tell the first argument, so like in lua use
.
instead of:
, do this:then the
x:Add(x2)
should work as expected.Plus TIP: On the very last commit I've implemented binary operator …