Planetoid Pioneers

Planetoid Pioneers

Not enough ratings
Lua Scripting Basics
By MKSTER
Learn how Lua scripting works in Crush 2D with a simple hello world example.
   
Award
Favorite
Favorited
Unfavorite
Watch this as a Video
Load a Blueprint
Each blueprint has a Lua script attached to it that controls its behaviour. So lets load a simple blueprint and add something to its script. Press F6 and double click the 1T Graphite block.
Save it with a different Name
Change the name of the blueprint and save it.
Open the Lua Script
Now scroll down in the editor and press edit blueprint script. Now Zero Brane Studio which comes with the game should open with the Lua file for this blueprint.
Set up the Lua Script
Set up your Lua Script with Build and Update function. Everything inside the Build function is done once when the assembly is created and everything inside the update functions is done a lot of times per second.


function Blueprint:Build() --Do this once when the assembly is created end function Blueprint:Update() --Do this every frame (many times a second) end

Make something Happen
So lets add a line to the build function to write "Hello World" or whatever else you want on the screen. This is added to the Build function so it happens only once. If its added to the Update then it would happen every frame.

function Blueprint:Build() --Do this once when the assembly is created print("Hello World!") end function Blueprint:Update() --Do this every frame (many times a second) end
Test Your Changes
Save your changes (Ctrl + S) and switch back into the game. Spawn the Blueprint by clicking in the game scene and pressing insert while still in edit mode (F2). Your message will appear in the bottom left of the screen. You might need to move windows out of the way to see it.
Now do something Cool
Ok that was easy here is bonus to make something cool.

function Blueprint:Build() --Do this once when the assembly is created print("Hello World!") end function Blueprint:Update() --Do this every frame (many times a second) --get the master movable object of this assembly local blockMO = self:GetMasterMO() --if a block collided then.. if blockMO:GetCollidedWithMOorTO() then --get the gravity vector and flip the direction of it and multiply it by 100 to make it hop up local gravityVec = blockMO:GetGravityVector() blockMO:AddAcceleration(-gravityVec*100) end end
Thats it!
To learn more about Lua check out some simple blueprints like the bomb or the rifles and modify them, or check out any of these:
Look up all the custom script functions for Planetoid Pioneers in the scripting documentation. You can find it in <Game>/Docs/LuaManDocs.html

Check out this guide for code snippets to copy and paste:
https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=654953736