simple lua qustion

Started by
8 comments, last by Kylotan 18 years, 11 months ago
hello to all the game devlopers out there i lua as a scriping langage in my game enging but i would like to have the abulity to have lua do something like the C++ GetKeyState any thing i try does not seem to work any ideaws
Peace Code & Coffee :)
Advertisement
How about your engine stores any key change in a LUA table?

LUA:
keyStates = {}

C++:
<on message WM_KEYDOWN>
lua_getglobal( luaState, "keyStates" );
lua_pushNumber( luaState, wParam );
lua_pushNumber( luaState, 1 );
lua_settable( lua_state, -3 )

<on message WM_KEYUP>
lua_getglobal( luaState, "keyStates" );
lua_pushNumber( luaState, wParam );
lua_pushNumber( luaState, 0 );
lua_settable( lua_state, -3 )


This will store keydown states as 1 and keyup states as 0 in the table (LUA) keyStates... forgive me if this is not helpful but I was extremely drunk when I posted this :) It was cheap alcohol night at a club near me...

[Edited by - ScootA on May 15, 2005 8:18:46 PM]
-Scoot
okay i guess i was not that clear in my ornigal post here is what i am trying to do i want a simple lua command lets call it GetKey so it would be called somthing like

function Input()

if GetKey('VK_UP') = 1 then
-- something happens here
end

end

now then this is my code

lua_State *g_pLuaState;

#define GetIntParam( Index )( int ) lua_tonumber ( g_pLuaState, Index );
#define ReturnNumer( Numer )lua_pushnumber ( g_pLuaState, Numer );

int HAPI_GetKeyState( lua_State * pLuaState ){

int iKey = GetIntParam (1);

if(GetKeyState(iKey)) { // If we hit the UP arrow key
ReturnNumer(1);
}
return 1;
}

well there is something wrong and i really need help any one
Peace Code & Coffee :)
I maintain the way I have shown will be totally valid: update a LUA table for each keyboard (extendable to mouse) alteration - then in your LUA code you need only query the state of a table entry:

if keyState[23] then

-- do some amazing code

end
-Scoot
the code u posted does not work even a little

keyStates = {}

C++:


lua_getglobal( luaState, "keyStates" );
lua_pushNumber( luaState, wParam );
lua_pushNumber( luaState, 1 );
lua_settable( lua_state, -3 );

<on message WM_KEYUP>
lua_getglobal( luaState, "keyStates" );
lua_pushNumber( luaState, wParam );
lua_pushNumber( luaState, 0 );
lua_settable( lua_state, -3 );


keyStates <- what is this
WM_KEYUP and KEYDOWN is MFC i am trying to stay away from that as mouch as posible in my enine would it be posible to do it with just gneric classes and functions
Peace Code & Coffee :)
Well obviously you need to fit it into your code in an appropriate way - I wasn't trying to write it for you, just offer a suggestion to a method which may suit.

keyStates is a LUA table which must be defined (and run) in a LUA script before you use it.

As for WM_KEYUP etc, these are obviously just hints, but the code can be placed in any event handling routines - and they are not MFC, they are standard windows messages that any windows program uses, either directly or wrapped.

Hope this helps
-Scoot
Yeah, basically the idea is that you handle input in the host application and just manipulate the data in Lua. Lua is really there for logic handling more than anything else. Handling keypresses is platform specific so Lua doesn't make much attempt to do that.
Quote:Original post by Kylotan
Yeah, basically the idea is that you handle input in the host application and just manipulate the data in Lua. Lua is really there for logic handling more than anything else. Handling keypresses is platform specific so Lua doesn't make much attempt to do that.


Further to this, now I come to think about it - a neater approach would be to register a bunch of script-functions to handle certain events (eg key/mouse), then have your hostApp automatically call them when the event occurs - event-driven apps are so much more elegant than state-testers...
-Scoot
okay i figured it out im sorry to truble you guys but to any one who is haveing a simmlar problem heres the answer to it first u have to create a new function that handles all of the keyboard input u want and output a newmaric code for each key like so

// well this is an anoying way to get keyboard input
int GetKey(void){

// if up or w keys
if(GetKeyState(VK_UP) & 0x80 ) {
return VK_UP;
}

// if down or s keys
if(GetKeyState(VK_DOWN) & 0x80 ) {
return VK_DOWN;
}

// if left or a keys
if(GetKeyState(VK_LEFT) & 0x80 ) {
return VK_LEFT;
}

// if right or d keys
if(GetKeyState(VK_RIGHT) & 0x80 ) {
return VK_RIGHT;
}
// if space bar
if( GetKeyState(VK_SPACE) & 0x80) {

// Use a negative Y speed to rotate around the Y axis
return VK_SPACE;
}
ETC.....
}

then u have return that val back to lua like so
int HAPI_GetKeyState( lua_State * pLuaState ){

lua_pushnumber ( g_pLuaState, GetKey() );
return 1;
}

now then the lua script will be something like the following

-- 38 is the up key
if ( GetKey() == 38 ) then
-- something happens here
end

like i said sorry abt bothering u and thanks for all the help everyone
Peace Code & Coffee :)
You might consider using GetKeyboardState(), filling a lua table in the API function, and returning that.

This topic is closed to new replies.

Advertisement