Skip to main content

Performance

Lets talk about performance. You must think about the performance of your level on people who will play your levels' devices. You may have the latest RX 7800 XT but little Jameson is playing on his mom's iPhone SE.

So you've got to make sure you are always considering performance on all devices when writing your ModScripts

Caching

It is very important you cache anything that is computationally expensive to perform.

Caching just means storing the result of a function (or method) call to a variable, and then using that variable instead of calling the function again. Sometimes you can't cache certain things, depending on if you need an updated value, but usually for most things, you can.

Especially the calls for getting items

local snoopDoggs = getItemsByMemeId("SnoopDogg") -- snoopDoggs stores the result of the getItemsByMemeId function call so it doesn't need to scan the level every time.

Cleanup

There is a cleanup function that you can easily place in your ModScript, and it will be called automatically when exiting the level:

-- ...rest of your code
function cleanup()
-- unsubscribe from all events here with <some event>.remove()
end

All event handlers you subscribed to must be unsubscribed to here. If you don't, you will have a memory leak and the game could lag or crash.

Lifecycle Events

All the lifecycle events that get called repetedly could start to impact performance if you're not careful.

If you subscribe to a repeating lifecycle event like OnTriggerEnterEvent or update, etc, and you do some even slightly performance heavy operation, it could start to cause lag, especially if done across multiple ModScripts.

This issue will become especially problematic in very quickly repeating lifecycle events like update, fixedUpdate, lateUpdate, OnCollisionStayEvent, OnTriggerStayEvent, and all them ones. Be very careful, and always keep a close eye on potential performance issues.

Console Logging

If you log a lot to the console and it's open, it will cause lag, but this is only an issue in the Level Builder, so it will only affect you while building your level. Still, be aware of this as a reason for lag - it's not necessarily your fault.

It's Up To You

At the end of the day, how your level performs on other people's devices is entirely up to you. You are given the freedom to create literal gold, or just pure garbage. Up to you.