Archived

This topic is now archived and is closed to further replies.

Lua (Help)

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anybody know of a good resource(or Tutorial)for using Lua with D3D stuff?I have got the Game Scripting Mastery book,but have found it as useful as a poke in the eye with a sharp stick):- because he has wrapped "Everything".And what''s supposed to be quite simple has become quite hard trying to figure it out,how to start applying lua to my D3D application. Iv''e found one short tutorial,but its basiclly for a console app,and i still don''t have a clue about parameter passing etc etc for a D3D Application. Any help would as always be great. P.S (or even a small example of using the most basic D3D app with lua) would be really useful to me. Its mostly how to pass parameters for D3D and stuff like that, wrapping it etc. Thanks.

Share this post


Link to post
Share on other sites
Basically what the AP said. Make a small test program that you use to create a re-usable interface between C and lua (sort of a wrapper). Once you have that, you can create the lua_CFunctions in your D3D application that convert the lua parameters to D3D calls. Creating a Lua enabled app with D3D shouldn''t be any different than one with anyother API (such as the Wrappuh API Game Scripting Mastery uses).

Share this post


Link to post
Share on other sites
I understand the book (lua section) up until recoding the Alien Head demo in lua.Maybe its becuase i don''t understand(or know enough)about the wrapping idea,i''m not sure.

I''ll exlpain what i mean.
I started off with something simple.Just to script changing the width and the height of a window(no Direct 3D ),although i set it up for using D3D later.

So i made a normal windows app,something like this:
-------------------------------------------------------
// Include files
#include <windows.h>
#include <stdio.h>
#include "d3d8.h"
#include "d3dx8.h"
#include "rmxfguid.h"
#include "rmxftmpl.h"

// ---- Lua -------------------------------------------
extern "C"
{
#include "lua\lua.h"
}
//-----------------------------------------------------

// Window handles, class and caption text

HWND g_hWnd;
HINSTANCE g_hInst;

//Some simple window size settings
long g_WindowWidth = 640;
long g_WindowHeight = 480;

static char g_szClass[] = "MyTestClass";
static char g_szCaption[] = "Simple Lua Test App";

// The Direct3D and Device object
IDirect3D8 *g_pD3D = NULL;
IDirect3DDevice8 *g_pD3DDevice = NULL;

lua_State * g_pLuaState;

// Lua State
#define LUA_STACK_SIZE 1024
Lua Stack Size

// Here are some macros to wrapp some lua functions:
// ---- Macros ------------------------------------------
//
// CallLuaFunc ()
//
// Calls a Lua function.
//
//------------------------------------------------------

#define CallLuaFunc( FuncName, Params, Results )
{

\
lua_getglobal ( g_pLuaState, FuncName );
lua_call ( g_pLuaState, Params, Results );
}

//-------------------------------------------------------
//
// GetIntParam ()
//
// Returns an integer parameter.
//

#define GetIntParam( Index )

( int ) lua_tonumber ( g_pLuaState, Index );

//--------------------------------------------------------
//
// ReturnNumer ()
//
// Returns a numeric value to a Lua caller.
//

#define ReturnNumer( Numer )

lua_pushnumber ( g_pLuaState, Numer );
//---------------------------------------------------------
void InitLua ()
{
// Open a new Lua state

g_pLuaState = lua_open ( LUA_STACK_SIZE );

// Register our host API with Lua
lua_register ( g_pLuaState, "GetWindowSize", API_GetWindowSize );

}
//---------------------------------------------------------
// ShutDownLua ()
//
// Shuts down Lua by closing the global state.
//---------------------------------------------------------

void ShutDownLua ()
{
// Close Lua state

lua_close ( g_pLuaState );
}

//----------------------------------------------------------
// Here are my Apps API functions(exporting to lua):
// ---- Host API--------------------------------------------
// Reads the 2 parameters from the lua stack(window width & height)

int API_GetWindowSize(lua_State * pLuaState)
{

int iWidth = GetIntParam ( 1 );
int iHeight = GetIntParam ( 2 );

void SetWindowSize(int iwidth,int iHeight);


return 0;


}

//------------------------------------------------------------
// Sets the height & width of the window with the 2 parameters
// from GetWindowSize().

int API_SetWindowSize(int WWidth,int WHeight)
{

g_WindowWidth = WWidth;
g_WindowHeight = WHeight;
return 0;

}
//-------------------------------------------------------------
// ---- WinMain ----

---------------------------------------------------------------
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,
LPSTR szCmdLine, int nCmdShow)
{

WNDCLASSEX wcex;
MSG Msg;

g_hInst = hInst;

// Create the window class here and register it
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wcex))
return FALSE;
//-------------------------------------------------------
// Here is where out API function WindowSize(),is called.

// Load our lua script

// lua_dofile ( g_pLuaState, "test.lua" );

// CallLuaFunc ( "GetWindowSize", 0, 0 );
//-------------------------------------------------------
// Create the Main Window
g_hWnd = CreateWindow(g_szClass, g_szCaption,
WS_CAPTION | WS_SYSMENU,
0, 0, g_WindowWidth,g_WindowHeight,
NULL, NULL,
hInst, NULL );


if(!g_hWnd)
return FALSE;
ShowWindow(g_hWnd, SW_NORMAL);
UpdateWindow(g_hWnd);

// Run init function and return on error
if(DoInit() == FALSE)
return FALSE;

// Start message pump, waiting for signal to quit
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT) {
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if(DoFrame() == FALSE)
break;
}


// Run shutdown function
DoShutdown();

UnregisterClass(g_szClass, hInst);

return Msg.wParam;
}
//------------------------------------------------------
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;

}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//-------------------------------------------------------



And my Lua script,looks like this:


//-----------------------------------------------------------
-- set the width & height

WindowWidth = 800;
WindowHeight = 600;

function GetWindowSize(WindowWidth,WindowHeight);

end


//----------------------------------------------------------


Where have i gone wrong? what am i doing wrong?

Thanks,
Paul.

Share this post


Link to post
Share on other sites
Go to here for the Lua website...
www.lua.org

A nice introduction to integrating with with C++
http://www.gamedev.net/reference/articles/article1932.asp

I use Luna to expose my C++ classes to Lua...
http://lua-users.org/files/wiki_insecure/users/lpalozzi/luna.tar.gz

Hope these help

Cheers,
Paul Cunningham

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The code looks a bit confusing. API_GetWindowSize and the Lua function GetWindowSize seem to be actually setting the window size. Also is the InitLua function ever called to register the function in Lua, and is the Lua code ever run or is it commeted out as in the code you posted? The main program doesn''t seem to actually use the Lua functions and macros, it just has a few functions that would use them that are commented out. The functions to Init Lua, run Lua scripts, and shut down Lua actually need to be called somewhere in the program, they are just ordinary functions. And there is no need for something like CallLuaFunc ( "GetWindowSize", 0, 0 ); since API_GetWindowSize is a C function you could call directly anyway.

Share this post


Link to post
Share on other sites
In your lua script file, you have this:

WindowWidth = 800;
WindowHeight = 600;

function GetWindowSize(WindowWidth,WindowHeight);

end


What this does is it makes a lua function call GetWindowSize that takes two parameters does nothing. Its parameters have the same name as the two global variables you defined right above the function.

Here's what you could do to change it:

Instead of

lua_register ( g_pLuaState, "GetWindowSize", API_GetWindowSize );

do

lua_register ( g_pLuaState, "SetWindowSize", API_SetWindowSize );

and make sure you change the name of the C function to match. The reason to do this is because API_GetWindowSize as you've written it takes two parameters and SETS the window size.

Now in your lua script, change

function GetWindowSize(WindowWidth,WindowHeight);

end


to

function SetDefaultWindowSize();
SetWindowSize(WindowWidth,WindowHeight);
end

[/source]

So now you have a lua function SetDefaultWindowSize that calls the C function SetWindowSize with the two global variables you declared above.

Now for the last change:
Make

CallLuaFunc ( "GetWindowSize", 0, 0 );

into

CallLuaFunc ( "SetDefaultWindowSize", 0, 0 );

This will call the lua function SetDefaultWindowSize with zero parameters (as it was defined) and it expects zero return values.

If all goes well, and I didn't mess up, your lua script should now set g_WindowWidth to 800 and g_WindowHeight to 600

[edited by - zaniwhoop2 on May 29, 2003 11:34:04 PM]

Share this post


Link to post
Share on other sites
[OT] Anyone interested in a way of making it possible to let VS.NET syntax-check lua script files when compiling a solution and also understand error messages in a way which allows it to jump directly to the line like you can when compiling common c files ?




LightBrain website relaunched, BomberFUN only USD 9.99
visit http://www.lightbrain.de

Share this post


Link to post
Share on other sites