A lightweight ue4 lua framework
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Doc/Images
Resources
Source 修复使用LuaPanda调试时的路径匹配问题 Feb 19, 2019
.editorconfig
.gitignore
.gitmodules
Benchmark.xlsx
Bluelua.uplugin
README-cn.md update readme Jan 28, 2019
README.md

README.md

Bluelua for UE4

Replace the blueprint with Lua, keep it in a consistent way with the blueprint, and switch seamlessly. Accessing UObject's properties and methods with reflection and without the need to generate glue code, more simple and easy to expand.

Android, iOS, Mac, Windows, Linux are supported now.

Used Open Source Libraries

  • lua : Lua is a powerful, efficient, lightweight, embeddable scripting language
  • luasocket : Network support for the Lua language
  • Tencent LuaPanda : Pandora Lua Debugger for VS Code

How to use

Clone Bluelua to your project's Plugins folder, regenerate project solution and build.

How to inherit C++ with lua

  • ILuaImplementableInterface

    All C++ classes that can be used with LUA subclasses need to be inherited from this interface and the corresponding methods are called in the corresponding virtual functions, the main types of overrideing are as follows:

    All C++ classes (or blueprint classes) need to override two important BlueprintNativeEvent: ShouldEnableLuaBinding: should use lua subclass on this C++/Blueprint class. OnInitBindingLuaPath: return corresponding lua file path

    It's optional to override OnInitLuaState: return custom FLuaState wrapper, default use of global lua state

    • Class AActor, see LuaImplementableActor.h

      • Call OnInitLuaBinding in BeginPlay
      • Call OnReleaseLuaBinding in EndPlay
      • Call OnProcessLuaOverrideEvent in ProcessEvent
    • Class UUserWidget, see LuaImplementableWidget.h

      • Call OnInitLuaBinding in NativeConstruct
      • Call OnReleaseLuaBinding in NativeDestruct
      • Call OnProcessLuaOverrideEvent in ProcessEvent

      The UUserWidget class also has a special place where the LatentAction owned by the class is not affected by the pause, so you also need to override NativeTick and call LatentActionManager in the virtual function to handle all LatentAction objects. See TickActions in LuaImplementableWidget.h

    • Class UObject, see RPGAnimNotifyState.h in Demo LuaActionRPG

      • Call OnReleaseLuaBinding in BeginDestroy
      • Call OnProcessLuaOverrideEvent in ProcessEvent
      • Because the Uobject class does not have an initialized virtual function that can be overrided, so it can call OnInitLuaBinding the first time the ProcessEvent is called, as shown in the demo
    • Class UGameInstance, see RPGGameInstanceBase.h in Demo LuaActionRPG

      • Call OnInitLuaBinding in Init
      • Call OnReleaseLuaBinding in Shutdown
      • Call OnProcessLuaOverrideEvent in ProcessEvent
  • keyword Super

    When you use Lua to subclass c++ classes or blueprints, there is a temporary global object called Super represent the parent UObject that you can access it's properties and methods. It's only avaliable when lua file is loaded(luaL_loadbuffer), so you need to cache it's reference in your lua file like local MyParent = Super and use MyParent in your lua

  • CastToLua

    When you have a UObject in lua and want to know if the object has a lua subclass, you can call CastToLua on that object like local LuaObject = OtherObject:CastToLua(), it returns a lua table represent the lua object if OtherObject has a lua subclass. CastToLua works like cast in blueprint:

  • Modular lua subcalss

    All lua subclass should implement in modular table and methods should declare with : not ., see see GameInstance.lua in Demo LuaActionRPG

    local m = {}
    
    function m:func()
    end
    
    return m
  • inheritance in lua

    You can use lua metatable to implement inheritance like you do in C++ or blueprint, see PlayerCharacter.lua and Character.lua in Demo LuaActionRPG

  • When a UFUNCTION expose to blueprint, it can has a alias, like BeginPlay in blueprint, it actually called ReceiveBeginPlay. so when you override this function in lua you should use ReceiveBeginPlay not BeginPlay.

Samples

  • LuaActionRPG: Epic's ActionRPG demo in lua implementation, still work in progress
  • BlueluaDemo: benchmark and test

TODO

  • hot reload lua

中文版使用介绍

使用介绍