jeremyong/Selene

unable to create a class function with more than one parameter

Opened this issue · 3 comments

hello everyone,

I don't know if it is a issue, a limitation or just me doing it wrong.
I made a class with a function with two parameters like this:
class MySprite {
public:
void setPosition( float x, float y );
}

and I did this to register it on lua:
state["MySprite"].SetClass<MySprite, float>("setPosition", &MySprite ::setPosition);

it compiles with no error, but, when I call this function on lua it returns this error:
/mnt/data/app/main.lua:5: bad argument # 1 to 'SetPosition' (unregistered type expected, got number),

thanks :)

I have the same problem but with only single parameter and using float:

I do the following:

class Bar
    {
    public:
        Bar(float num)
        {
            x = num;
        }

        void SetX(float x2) {
            x = x2;
        }

        float GetX() const {
            return x;
        }
    private:
        float x;
    };

// Bind the Bar class in the state
`aState["Bar"].SetClass<Bar, float>("SetX", &Bar::SetX, "GetX", &Bar::GetX);``

and then in the .lua file:

bar = Bar.new(8)
barx = bar:GetX()

I got the same error:

[INFO]Error Script: ../data/scripts/camera_test.lua:7: bad argument #1 to 'new' (unregistered type expected, got number).

I do the same example with int and double and it works correctly. If you try your example but set X and Y separatelly? Does it run ok?

I use MS2015.

I have added the define LUA_32BITS when I compile lua, and I works correct now, I have also try your case and it works fine.

Faced this issue recently.

Depending on LUA configuration (e.g. LUA_32BITS) lua_Number will be float, double or long double. The issue happens when calling a class method which accepts an argument of one of above types, but that type doesn't match lua_Number. E.g. lua_Number is float, but method is void Bar::set(double x).

You should register classes with methods accepting lua_Number. You can always make a wrapper class for this purpose.