Sometimes it's useful to see a value printed on screen to see how it changes. We can use the 'print()' function to print messages to the console.
Here's an example of printing an object's 3D position to the screen whenever it moves:
function MeshEntity:onMove() print(tostring(self:getWorldPosition()), 1) end
Note the use of the 'tostring()' function to convert the value returned by 'getWorldPosition()' (a vector3) into a string. The second parameter of 'print()' (in this case '1'), represents a unique ID (which can be any number) that lets us replace the printed message with a new message the next time 'onMove()' is called (instead of appending a new message each time).
Here's another example of printing a multi-part message:
function MeshEntity:onMove() print("Name: " .. self:getName(), 1) print("Linear Velocity: " .. tostring(self:getRigidBody():getLinearVelocity()), 2) print("Speed: " .. tostring(self:getRigidBody():getLinearVelocity():length()), 3) print("Angular Velocity: " .. tostring(self:getRigidBody():getAngularVelocity()), 4) print("Position: " .. tostring(self:getWorldPosition()), 5) end
Notice the use of the concatenation operator '..' to combine two strings together. Also, notice the IDs (1,2,3,4,5) are unique for each line which ensures we keep updating our respective printed line each time 'onMove()' is called.
Comments
0 comments
Please sign in to leave a comment.