Scripting (and modding) Unreal
Engine
with Lua
Roberto De Ioris
https://github.com/rdeioris
https://github.com/unbit
@unbit
Codemotion Rome 2019
What do they have
in
common ?
∙ Game
Designers
∙ Animators
∙ Riggers
∙ Data Analysts
"Teach a man to use a game
engine
and he'll ship a game,
teach a man to build a game
engine and
he'll never ship anything"
Programming and
Scripting
in Unreal Engine 4
∙ C++
∙ Blueprints
∙ Various Plugins (Python, Javascript, Lua,
…)
Why “classic”
Scripting
Instead of Blueprints
∙ Scaling
∙ Expressivene
ss
Why Lua
?
∙ Embarrassingly easy (very few
rules)
∙ Tiny
∙ Fast
∙ Embeddable (without pain)
∙ Multiple VMs in the same process
∙ Solid
∙ Big Community
∙ Lot of third party libraries
∙ …
Caveats when
embedding
scripting languages
∙ Memory management
(!!!)
∙ Language Idiomaticity
∙ Data Representation
∙ Performance
The Unreal Engine 4
API
OO
P OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P
OO
P OO
P
OO
P
Lua and “classic”
OOP
Building a tool
for
Game Designers
∙ Low learning curve
∙ Built on top of the designers needs, not the engine
ones
∙ “Safe”
∙ Debuggable
Building a tool
for
Modders
∙ Low learning curve
∙ Built on top of the modders needs, not the engine
ones
∙ “Safe”
∙ Debuggable
LuaMachin
e
https://github.com/rdeioris/LuaMachine
https://www.unrealengine.com/marketplace/luamach
ine
Supported
Platforms
∙ Windows 64bit (Editor +
Runtime)
∙ Mac (Editor + Runtime)
∙ Linux 64bit (Editor + Runtime)
∙ Android (Runtime)
∙ iOS (Runtime)
Hello
World
SayHello
UFunction
Exposing
SayHello
as Lua’s hello()
Calling the Lua
script
Welcome Lua in
UE
Isolation:
LuaStates
Data
Representation:
LuaValues
Working with
LuaValues
Working with
LuaValues
Working with
LuaValues
Working with
LuaValues
Working with
LuaValues
(C++)UCLASS()
class LUATEST422_API UCodemotionCppLuaState : public ULuaState
{
GENERATED_BODY()
UFUNCTION()
FLuaValue DummyAdder(TArray<FLuaValue> Args);
};
FLuaValue UCodemotionCppLuaState::DummyAdder(TArray<FLuaValue> Args)
{
int32 Total = 0;
for (FLuaValue& Arg : Args)
{
Total += Arg.ToInteger();
}
return FLuaValue(Total);
}
Scripts as
Assets:
LuaCode
Running Lua
Scripts
Running Lua
Scripts
from URLs∙ Think
twice
∙ Think
twice
∙ Think
twice
Running Lua
Scripts
from URLs [take
two]∙ RSA
signing
∙ https
LuaState:
require
Widget
s
∙ Code Editor with syntax
colouring
∙ ANSI Output (WIP)
Widgets in
action
Error
Management
Monitoring and
Managing
∙ Memory
usage
∙ Stack top
∙ GC
Debuggin
g
∙ Simple GUI (WIP)
∙ Events
∙ via Lua-itself
Debugger in
action
Debug
Hooks
Debug Hooks
(C++)
void UCodemotionCppLuaState::ReceiveLuaCallHook_Implementation(const FLuaDebug& LuaDebug)
{
UE_LOG(LogLuaMachine, Warning, TEXT("Name: %s"), *LuaDebug.Name);
}
Coroutine
s
Ticking
Coroutines
Ticking
Coroutines
(improved)
Lua Tables as
Assets
Lua Interfaces
???
UObject’s
Representation:
Metatables for
UserData
Blueprint (and C++)
Functional Tests
included
Running LuaMachine
Tests
Hidden
Features
∙ Reflection API
∙ Lua States as Singletons/Managers (with
Events)
∙ Syntax Highlighter (with custom keywords)
Note
s
∙ You can call Lua functions from everywhere
∙ (animation blueprints, behaviour trees, widgets,…)
∙ C++ UFUNCTIONs can be exposed easily
∙ You can easily convert C++ UPROPERTies to Lua
tables
Tutorial
https://github.com/rdeioris/LuaMachine/blob/master/Tutorials/SimpleDialogueSystem.m
d
Communit
y
Discord channel:
https://discord.gg/eTXDfEU
Special
Thanks
∙ AIV
∙ Nicolas Martel (aka.
Nogitsu)
∙ Epic

Roberto De Ioris - Scriptiamo Unreal Engine con Lua - Codemotion Rome 2019