mvrens / delphilua Goto Github PK
View Code? Open in Web Editor NEWAn API conversion and wrapper classes for integrating Lua 5.2 into Delphi projects
An API conversion and wrapper classes for integrating Lua 5.2 into Delphi projects
Hi, with the new feature of being able to register a LuaCFunction to a table, I was thinking of how I can now take a class and auto register all of its class procedure LuaCFuntions. I have this so far:
TLuaFuncs = class
public
class procedure test2(aContext: ILuaContext);
end;
procedure RegisterClassFunctions(aClass: TClass; aTableName: string);
var
FRttiContext: TRttiContext;
rttiType: TRttiType;
rttiMethod: TRttiMethod;
rttiParameters: TArray<System.Rtti.TRttiParameter>;
tbl: TLuaTable;
begin
tbl := TLuaTable.Create;
rttiType := FRttiContext.GetType(aClass);
for rttiMethod in rttiType.GetMethods do
begin
if (rttiMethod.MethodKind <> mkClassProcedure) then continue;
if (rttiMethod.Visibility <> mvPublic) then continue;
rttiParameters := rttiMethod.GetParameters;
{ Check if one parameter of type ILuaContext is present }
if (Length(rttiParameters) = 1) and
(Assigned(rttiParameters[0].ParamType)) and
(rttiParameters[0].ParamType.TypeKind = tkInterface) and
(TRttiInterfaceType(rttiParameters[0].ParamType).GUID = ILuaContext) then
begin
WriteLn('class method: %s', [rttiMethod.Name]);
tbl.SetValue(rttiMethod.Name, WHAT_DO_I_PUT_HERE); <<------- how to handle this
end;
end;
gEngine.Table.SetValue(aTableName, tbl);
gEngine.Lua.SetGlobalVariable(cVivaceTable, gEngine.Table);
end;
I am trying to figure out how to handle "WHAT_DO_I_PUT_HERE" section of the above code. I discovered I can do this:
tbl.SetValue('test2', TLuaImplicitVariable(TLuaFuncs.test2));
With the rtti info I have a this point, how could then register each class procedure I come across?
I tried this:
tbl.SetValue(rttiMethod.Name, TLuaImplicitVariable(TLuaCFunction(rttiMethod.CodeAddress)));
But it will crash at: TLuaCFunction(rttiMethod.CodeAddress)
I'm so close, any ideas?
Possible? I would like to scan the lua source and look for all my onXXX event functions (onStartup, onShutdown etc) once at startup then just call them if they have been assigned. I thought if I could get a global scope reference it would work. It does in that it will let me call the onXXX routines, but if I call any registered routine inside it will crash with a stack overflow:
function lunagine.onStartup()
print("this is a test") -- this works fine
lunagine.display.open(...) -- this will crash
end
What I discovered is that when lunagine.display.open(...) is called, it just calls onStartup() again and again until it crashes.
if I do this:
procedure TEngine.OnStartup;
begin
// FOnStartup is a global reference. When it's used, it will call the onStartup routine, but any
// other call inside it will crash
//if Assigned(FOnStartup) then FOnStartup.Call;
// this will work
var f: ILuaFunction := FTable.GetValue('onStartup').AsFunction;
f.Call;
end;
This works. Like I said I would like to lookup all the event handlers at start then call them if they are not nil rather than do the lookup each frame and since this is a game, it's running 60 times a second and there are about 15 event handlers used in the engine. Or maybe that lookup is fast and not an issue? From a gamedev perspective I auto assume that its not and I need to cache the values.
Any suggestions?
Hi, didn't know how to contact you otherwise so apologies if this is the wrong place. I was wondering if you are interested in the LuaJIT version of DelphiLua? I got it working with current version of LuaJIT and everything seems to work well. All about a few unit tests work. I'm using it in my current project and updating things on an as-needed basis.
Thanks
Jarrod
I'm trying to figure how I can do this:
table0.table1.myfunction()
I need to be able to register myfunction on table1 and add table1 to table0. How can TLuaTable.SetValue be modified to be able to do something like this:
procedure myfunction(aContext: ILuaContext);
begin
...
end;
table1.SetValue("myfunction", myfunction)
Then I should be able to do:
table0.SetValue("table1", table1)
BTW, I got the code compiling and using luajit/moonjit (seems be working fine).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.