Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to create a class function with more than one parameter #157

Open
alexribeirodesa opened this issue Jul 16, 2016 · 3 comments
Open

Comments

@alexribeirodesa
Copy link

alexribeirodesa commented Jul 16, 2016

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 :)

@Vazquinhos
Copy link

Vazquinhos commented Aug 17, 2016

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.

@Vazquinhos
Copy link

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.

@andrey-desman
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants