Avorion Wiki
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Admin noticeboard
Avorion Community
Official forum
Discord
Twitter
Reddit
Facebook
Steam
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Avorion Wiki
356
pages
Explore
Main Page
All Pages
Interactive Maps
navigation
Main page
Community portal
Recent changes
Random page
Admin noticeboard
Avorion Community
Official forum
Discord
Twitter
Reddit
Facebook
Steam
Gamepedia
Gamepedia support
Report a bad ad
Help Wiki
Contact us
Editing
Performance Optimization
(section)
Back to page
Edit
VisualEditor
View history
Talk (0)
Edit Page
Performance Optimization
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Avoid too many new API class instantiations == Every time you create an instance of a Scripting API class, for example with <code>Entity(id)</code>, Avorion does a few things in the background before it finally creates the instance of the class. Among those is can be memory allocations (internally done with '''new()''', but Avorion tries to do those as little as possible using memory pools), lookups of the '''Entity''' in the '''Sector''', potential locking due to its multithreaded nature, and many more. So one, very easy way to improve performance of your scripts would be to avoid new instantiations of classes as much as (reasonably) possible. For example, take this code, where we want to add cargo to a ship: <source lang="lua"> -- bad, creates a new reference to Entity in each loop function addGoods(id, goodsToAdd, amount) for _, good in pairs(goodsToAdd) do Entity(id):addCargo(good, amount) end end </source> <source lang="lua"> -- better, creates a single reference to entity and reuses it in each loop function addGoods(id, goodsToAdd, amount) local entity = Entity(id) for _, good in pairs(goodsToAdd) do entity:addCargo(good, amount) end end </source>
Summary:
Please note that all contributions to the Avorion Wiki are considered to be released under the CC BY-SA 3.0
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab