Posted December 23, 2024 by sladkokotikov
#tech #lua
My full-time-job-game-engine is Unity. Sorry for that. Löve (and Lua in the first place) feels like a sip of fresh air. A huge sip.
As a metaprogramming enjoyer I am fascinated by metatables and almost endless opportunities that Lua can bring.
I fell in Löve with Lua, hehe.
But habits are habits. Here are three things that I missed from Unity (and C#): objects (and a bit of types), tweens and asynchronous methods.
I love functional style, but objects are objects. I learned Lua in 15 minutes, thanks Tyler, but I looked up how to create a pseudo-type too often, so I took that piece of code and converted it into a function that returns a pseudo-type with given constructor:
function createType(constructor) local newType = {} newType.__index = newType function newType:new(...) local n = {} constructor(n, ...) return setmetatable(n, newType) end return newType end
Now we can describe types in separate files like this:
local myType = createType(function(n) n.position = {100, 200} end) function myType:sayHi() -- do something with fresh instance end return myType
Okay, types are ready to use. Check!
Tweens are simple. Just add deltaTime every frame, do something cool as the timer progresses, and don’t forget to stop if elapsed time span is long enough.
Creating a tweener (a singleton that will loop over a collection of tweens and remove the ones that already ended) is almost boring.
local Tween = createType(function(tween, from, to, duration, ease, valueCallback, endCallback) tween.from = from tween.to = to tween.duration = duration tween.ease = ease or DEFAULT_EASE tween.progress = 0 tween.valueCallback = valueCallback tween.endCallback = endCallback end) function Tween:tick(dt) if self.progress >= self.duration then if self.endCallback then self.endCallback() end if self.valueCallback then self.valueCallback(self.to) end return false end local p = self.progress / self.duration local v = lerp(self.ease(p), self.from, self.to) if self.valueCallback then self.valueCallback(v) end self.progress = self.progress + dt return true end
Tweens are implemented and come in all flavors: delay, tweenSet, tweenSetInt and tween01 cover all my needs at the moment. Check!
Asynchronous operations are something else. So I stepped on a long and hard path of implementing “async/await” to satisfy my primal C# instincts… I hope the next article will cover all my inventions!
<----- To Be Continued -----