Topic: MScript::getInteger

I've been running through my code, trying to expose some helpful things to lua (like messages in my message system) and I was trying to sort one thing, and something felt a bit wrong, so I checked it up. MScript::getInteger (and other get functions) take unsigned int.

Checking a previous project, indices are usually negative... I just checked the actual code and you're doing arg+1, which I assume was meant to be, as a C programmer, people would be used to asking for 0 .. n-1 rather than 1 .. n. I think it's possible to hack it by casting the sign off... but I think the "nicer" way to do it might be to do something like lua_tointeger(m_state, arg - getArgsNumber());

Wondering if I am still early enough to submit this "fix" in time for the builds... or if it was done like that for a reason I can't see?

Last edited by Nistur (2012-08-05 20:24:20)

Re: MScript::getInteger

I've used that to create custom function from c++, it was a long time ago so it's not fresh in my mind,
but I think it was something like :

int myCustomFunction(void)
{
    if(script->getArgsNumber() == 2)
    {
        int firstArgument = script->getInteger(0);
        int secondArgument = script->getInteger(1);

        // do a simple addition
        script->pushInteger(firstArgument+secondArgument);
        return 1; 
    }

    return 0;
}

script->addFunction("myCustomFunction", myCustomFunction);

I just finished the builds, so lets wait the first debug pass...

Re: MScript::getInteger

Ok then, I have no idea how lua works internally, but I am seeing negative indices in things inside MScript.cpp.

Doesn't matter if it doesn't make the build, I guess, I can always hack it for now wink

script->getInteger((unsigned int)-1);

Re: MScript::getInteger

I've just done a quick test

    const char* params[10];
    for(int i = 0; i < 10; ++i)
        params\[i\] = script->getString((unsigned int)(i - 5));

Wow. lua is weird. Both positive and negative work :S Weird. I wonder why all lua docs I've read would take negative numbers

Re: MScript::getInteger

Normally, the -1 is used to parse arrays and special messages,
so it depends what you want to do ?

Are you making a custom function or something different ?