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!
== Callbacks over Polling == Whenever you want to react to something that happens in the game (say a ship got some new cargo), you have two options for detecting that something happened: # Polling: Check every frame (or second) whether that something has changed (ie. check for new cargo) # Callbacks: Have the game call a very specific function when that something has changed The second option is vastly superior because you can avoid unnecessary execution of script code. So whenever you want to check for, say, new cargo, use the <code>onCargoChanged(...)</code> callback. Check out the '''Callbacks Section''' of the [[Scripting API]] for more information on callbacks and a complete listing of all callbacks. <source lang="lua"> -- bad function update(timeStep) -- it is possible to make this a lot more sophisticated, but this is mainly for demonstration purposes if CargoBay():findCargos("Energy Cell") then myCargoChangedFunction() end end function myCargoChangedFunction() print ("cargo changed!") end </source> <source lang="lua"> -- better function initialize() Entity():registerCallback("onCargoChanged", "myCargoChangedFunction") end -- Avorion also tells us the index of the object that the callback happened to, the delta change and the good that changed function myCargoChangedFunction(objectIndex, delta, good) print ("cargo changed!") end </source> Not only is the second variant easier to read, but callbacks usually also provide a lot of relevant information by themselves.
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