Wish I knew this sooner!
Ironically don't really think about this stuff too much even though I primarily mod on the PS2 where that should be a concern lol.
I originally planned it for another thread of Lua tutorial, but since Rangga made it first, why should i keep holding my breath lol.
I heard that PS2 is fragile about memory stuff, but SE and AE are same in my opinion. Optimization stuff will always be a concern to every platform you know. You don't know how much i got crash complaint for my Selector Mod -REMAKE-, that's because i had no idea about how things work and declared 260 global variables at once lmao.
Two days ago, i reinstalled my Selector Mod -REMAKE- to check its memory growth.
It grew up so fast lol. No wonder some people told me it crashes every 10 minutes.
Any way to reduce the growth as much as possible?
I noticed any time I create tables, text... (because of repeatedly running functions when selecting them from a menu) it grows slightly. Not really good for an extended play time, I guess.
Some optimization tips based on my experience:
1. Use local instead of global variable unless you need it the most.
2. Do not set local repeatedly in a loop.
Example:
local X, Y, Z
while true do
X, Y, Z = PlayerGetPosXYZ()
Wait(0)
end
3. Use
collectgarbage() routinely to collect unused and dead objects (except global variables).
4. Avoid to execute function repeatedly unless you don't have any choice.
Example:
-- Pause game clock (most used method)
while true do
PauseGameClock()
Wait(0)
end
-- Pause game clock (better method)
while true do
if not ClockIsPaused() then
PauseGameClock()
end
Wait(0)
end
* CMIIW. I'd love to learn too.
Even though Lua 5.0.2 is actually run garbage collection automatically from time to time and Bully itself calls
collectgarbage() whenever you exit from an area, it does not help much if you make a huge script. You need to clean your own mess with
collectgarbage() because it's effective to reduce the memory growth.
I imagine it's due to the new stuff getting created on top of the old ones, or something?
I can't prove that actually.
gcinfo() (or
collectgarbage('count') in Lua 5.1) itself is also raising the memory growth.
But, i agree with you in common sense. It's like:
you can't achieve anything without any sacrifice.