Lua + Delphi (续)


 

这几天试图在delphi中整合Lua,发现相关的资料相当少。

最早下载的Lua4Delphi(http://sourceforge.net/projects/lua4delphi/files/lua4delphi/)包已经废弃很久了。连自带的示例都无法正常运行。源码中也有显而易见的bug。所以继续寻找可用的组件,找到了LuaDelphi 2010(http://blog.spreendigital.de/wp-content/uploads/2009/10/LuaDelphi2010-v1.3.zip) 这个组件可以正常运行,但有一个缺点:只能注册类中的方法而无法注册类本身。而且注册的类方法必须遵循统一的函数原型。所以它也不是好的选择。

后来找到了一个叫pLua的组件,发现它还是不错的,支持注册类,也支持动态注册类实例。不过它是用Lazarus写的,所以在函数指针上与delphi编译器有差异,在pLuaObject.pas中,plua_pushFunction这个过程要改一下才能正常运行。具体就是把  addr := integer(p^); 改成  addr := integer(p);即可.

 

具体用法如下:

uses LuaWrapper,pLuaObject;

  Lua := TLua.Create(self);
  lua.Value['x'] := 100;//注册变量
  Lua.RegisterLuaMethod('ShowMessage', lua_ShowMessage);//注册函数
  RegisterLuaButton(Lua.LuaState);//注册类

   RegisterExistingButton(Lua.LuaState, 'btn', btn1);//注册类实例
  if FileExists('script.lua') then
    begin
      Lua.LoadFile('script.lua');
      Lua.Execute;
      showmessage(inttostr(lua.Value['x']));//读取变量 
    end;

var
  ButtonInfo : TLuaClassInfo;


function setButtonInfo : TLuaClassInfo;
begin
  plua_initClassInfo(result);
  result.ClassName := 'TButton';
  result.New := @newButton;
  plua_AddClassProperty(result, 'Caption', @GetCaption, @SetCaption);
  plua_AddClassProperty(result, 'Left', @GetLeft, @SetLeft);
  plua_AddClassProperty(result, 'Top', @GetTop, @SetTop);
  plua_AddClassProperty(result, 'Width', @GetWidth, @SetWidth);
  plua_AddClassProperty(result, 'Height', @GetHeight, @SetHeight);
  plua_AddClassProperty(result, 'Visible', @GetVisible, @SetVisible);
  plua_AddClassProperty(result, 'Enabled', @GetEnabled, @SetEnabled);
  plua_AddClassMethod(result, 'Click', @Click);
end;

procedure RegisterLuaButton(L: Plua_State);
begin
  plua_registerclass(L, ButtonInfo);
end;

procedure RegisterExistingButton(L: Plua_State; InstanceName : AnsiString; Instance: TButton);
begin
  TButtonDelegate.Create(plua_registerExisting(L, InstanceName, Instance, @ButtonInfo), Instance);
end;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM