it has to be defined prior the "unit xyz" part.Originally Posted by Robert Kosek
it has to be defined prior the "unit xyz" part.Originally Posted by Robert Kosek
This is my game project - Top Down City:
http://www.pascalgamedevelopment.com...y-Topic-Reboot
My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
http://www.pascalgamedevelopment.com...source+manager
Oh, I'd have to go back and double check that, thanks for the tip. Though that's the least of my problems currently; I'm having trouble passing a function as an argument (Pointer style) that is making the program choke.
Working with functions as parameters isn't near as easy as it sounds. You have to keep a reference to the function in the Lua Registry, and then you have to use that entry to actually call the function.
The following is a "simple" sample:[pascal]function lua_CallFunc(L : PLua_State) : Integer; cdecl;
var
n, callTimes, funcRef, c, pidx : Integer;
pArray : array of variant;
begin
n := lua_gettop(L); // Get the number of items on the stack
callTimes := lua_tointeger(L, 1); // Get the number of times to call the function
if lua_type(L, 2)=LUA_TFUNCTION then // Make sure we have a function
begin
lua_pushvalue(L, 2); // Make a copy to push to the registry
funcRef := luaL_ref(L, LUA_REGISTRYINDEX); // push to the registry and get the reference
try
SetLength(pArray, n-3); // Get the params that we are passing
for c := 3 to n do
pArray[c-3] := luatovariant(L, c);
for c := 1 to callTimes do
begin
lua_rawgeti(L, LUA_REGISTRYINDEX, funcRef); // Place the reference on the stack
for pidx := 0 to length(pArray)-1 do // place each param on the stack
luapushvariant(L, pArray[pidx]);
lua_call(L, length(pArray), LUA_MULRET); // Call the function
end;
finally
luaL_unref(L, LUA_REGISTRYINDEX, funcRef); // Release the reference to the function
end;
end;
lua_CallFunc := 0;
end;[/pascal]
The Lua "Prototype" for this would be:
function CallFunc(NumTimes, FuncPointer, ...)
Of course, for speed, when using a known function you should cache the function registry reference for re-use. Then release it when you no longer need it.
Hope that helps some, just typed it in looking at some working code. But, there may be problems
- Jeremy
http://www.eonclash.com/
Uh, no, check the previous page. I cannot even register the functions... :?
Also, I noticed that registering the CRT library functions caused that access violation; I don't know how, why, or how to fix it though.Originally Posted by Robert Kosek
Okay, I tried this, and it raises the error that it has insufficient arguments.
[pascal]procedure RegisterCRT(L: PLua_State);
var
idx : Integer;
begin
lua_pushliteral(L, 'crt');
lua_newtable(L);
idx := lua_gettop(L);
LuaRegisterCustom(L, 'KeyPressed', lua_KeyPressed, idx);
LuaRegisterCustom(L, 'ReadKey', lua_ReadKey, idx);
LuaRegisterCustom(L, 'GetXY', lua_GetXY, idx);
LuaRegisterCustom(L, 'TextColor', lua_TextColor, idx);
LuaRegisterCustom(L, 'BackgroundColor', lua_BackgroundColor, idx);
LuaRegisterCustom(L, 'ClrScr', lua_ClrScr, idx);
LuaRegisterCustom(L, 'ClrEol', lua_ClrEol, idx);
LuaRegisterCustom(L, 'DelLine', lua_DelLine, idx);
LuaRegisterCustom(L, 'GotoXY', lua_GotoXY, idx);
LuaRegisterCustom(L, 'InsLine', lua_InsLine, idx);
LuaRegisterCustom(L, 'Window', lua_Window, idx);
// lua_settable(L,idx);
lua_settable(L, LUA_GLOBALSINDEX);
end;[/pascal]
But when I try this, it completely freaks out with the following error messages:[pascal]procedure RegisterCRT(L: PLua_State);
var
idx : Integer;
begin
lua_pushliteral(L, 'crt');
lua_newtable(L);
idx := lua_gettop(L);
LuaRegisterCustom(L, 'KeyPressed', @lua_KeyPressed, idx);
LuaRegisterCustom(L, 'ReadKey', @lua_ReadKey, idx);
LuaRegisterCustom(L, 'GetXY', @lua_GetXY, idx);
LuaRegisterCustom(L, 'TextColor', @lua_TextColor, idx);
LuaRegisterCustom(L, 'BackgroundColor', @lua_BackgroundColor, idx);
LuaRegisterCustom(L, 'ClrScr', @lua_ClrScr, idx);
LuaRegisterCustom(L, 'ClrEol', @lua_ClrEol, idx);
LuaRegisterCustom(L, 'DelLine', @lua_DelLine, idx);
LuaRegisterCustom(L, 'GotoXY', @lua_GotoXY, idx);
LuaRegisterCustom(L, 'InsLine', @lua_InsLine, idx);
LuaRegisterCustom(L, 'Window', @lua_Window, idx);
// lua_settable(L,idx);
lua_settable(L, LUA_GLOBALSINDEX);
end;[/pascal]Should I try "function as lua_CFunction" or "lua_CFunction(function)"? This is a bit crazy. I hope this doesn't occur very frequently when messing with Lua initially. :?Code:LuaCRT.pas(76,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(77,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(78,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(79,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(80,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(81,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(82,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(83,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(84,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(85,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(86,68) Error: Incompatible type for arg no. 4: Got "SmallInt", expected "<procedure>" LuaCRT.pas(233) Fatal: There were 11 errors compiling module, stopping LuaCRT.pas(5,50) Fatal: Compilation aborted
What is the declaration of LuaRegisterCustom()?
Lua.pasLuaRegisterCustom, LuaUtils.pasCode:type lua_CFunction = function(L : Plua_State) : Integer; cdecl;
All my functions are adhering to both call/definition correctly too. :? That's the strange part. I call the register function for the CRT functions I made, commenting out the search routine (I still cannot register it), and the instant they are registered (output isn't even started) that exception occurs and the application dies.Code:procedure LuaRegisterCustom(L: PLua_State; TableIndex: Integer; const Name: PChar; F: lua_CFunction); var Count: Integer; S: string; begin LuaProcessTableName(L, Name, S, TableIndex, Count); LuaRawSetTableFunction(L, TableIndex, S, F); lua_pop(L, Count); end;
Robert, you don't want to use the register methods from LuaUtils. Instead look at the register methods in my LuaWrapper and LuaObjects units.
Post up a zip of your source files and I'll adjust it to work. Then you can compare the two and see what I'm talking about.
- Jeremy
http://www.eonclash.com/
Is there something wrong with those register methods? They were the only really simple way to register the functions to a table that I could find. The way you gave me before FPC couldn't find the needed function. :?Originally Posted by Robert Kosek
EDIT: You mean to try the RegisterMethod from LuaObject.pas? So, I should just use that instead I guess. Though, I honestly don't understand the duplicity and frequency of so many functions to try and do that same thing.
EDIT2: As in something like this to register my functions?[pascal]procedure RegisterCRT(L: PLua_State);
var
idx : Integer;
begin
lua_pushliteral(L, 'crt');
lua_newtable(L);
idx := lua_gettop(L);
RegisterMethod(L, 'KeyPressed', @lua_KeyPressed, idx);
RegisterMethod(L, 'ReadKey', @lua_ReadKey, idx);
RegisterMethod(L, 'GetXY', @lua_GetXY, idx);
RegisterMethod(L, 'TextColor', @lua_TextColor, idx);
RegisterMethod(L, 'BackgroundColor', @lua_BackgroundColor, idx);
RegisterMethod(L, 'ClrScr', @lua_ClrScr, idx);
RegisterMethod(L, 'ClrEol', @lua_ClrEol, idx);
RegisterMethod(L, 'DelLine', @lua_DelLine, idx);
RegisterMethod(L, 'GotoXY', @lua_GotoXY, idx);
RegisterMethod(L, 'InsLine', @lua_InsLine, idx);
RegisterMethod(L, 'Window', @lua_Window, idx);
// lua_settable(L,idx);
lua_settable(L, LUA_GLOBALSINDEX);
end;[/pascal]Attempting that immediately gave me this when attempting to compile:Ick; at the moment I'm reconsidering trying to learn Lua using FPC. FPC just isn't very friendly to me no matter what I do, whenever I try.Code:LuaObject.pas(462,49) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(463,55) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(464,46) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(465,43) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(466,46) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(467,43) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(484,49) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(485,55) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(487,43) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(488,46) Error: Incompatible type for arg no. 3: Got "<address>", expected "<procedure>" LuaObject.pas(516) Fatal: There were 10 errors compiling module, stopping LuaObject.pas(4,50) Fatal: Compilation aborted
I just noticed something, but Jeremy, why are you pushing a procedure like a pointer, when you aren't using the right type? If you adjust your types to include a lua_PCFunction (in Lua.pas) like so:[pascal]type
lua_CFunction = function(L : Plua_State) : Integer; cdecl;
lua_PCFunction = ^lua_CFunction;[/pascal]And then change your RegisterMethod and lua_pushcfunction to use the lua_PCFunction type it seems to work. You're treating the function like a pointer and FPC is choking all over itself on this. Either it says "I don't want an address!" or when you pass a function "where are the variables for the function?" and so it doesn't work either way in its present implementation.
Bookmarks