Creating Your First 3D Game: Riding the Rails - Digging Deeper

Riding the Rails: Digging Deeper

The Digging Deeper resources are additional pages that go into more detail about the code that is developed in a FreshBrain activity. Code from the activity is highlighted and printed with a fixed-width font to make it stand out in the page. Following the code is a more detailed explanation of what the code does, and often there are references off to additional documentation where you may learn more details about the syntax and semantics of the Lua programming language as well as the supporting classes provided by the Wild Pockets system.



function onSceneStart()



As described in the tutorial, the onSceneStart() function is like a "main" function in many other program languages. It is the function that will get called by the system when the game is first started.


Camera.setMode("manual")



Camera is a class that is provided by the Wild Pockets system. setMode() is a function on the Camera class; in Lua, class functions are called with a '.', eg., Class.function(). The setMode() function takes one argument, a string.

You can read more about Lua classes, instances, and functions at:

You can read more about Wild Pocket's Camera class in the Wild Pockets API Reference, which can be accessed by clicking the question mark icon in the Wild Pockets development environment.



myPath = Path.new()



This call creates a new instances of the Path class, and assigns it to the myPath variable. Creating new objects (new instances of a class) is a common paradigm in object oriented programming languages.

The variable myPath is global; variables in Lua are global unless they are specifically described as local.

You can read about local variables in Lua at:
http://www.lua.org/pil/4.2.html

You can read more about the Path class in the Wild Pockets API Reference.


myPath:setPosition(2, SceneManager.getObject("Track 2"):getPosition()+vec(0, 0, 3))
myPath:setRotation(2, SceneManager.getObject("Track 2"):getRotation())



As described in the Wild Pockets tutorial, these functions are used to set waypoints on the path myPath. Note that now we're using ':' for the function call rather than '.'. In Lua, '.' is used for class function calls, while ':' is used for instance methods. In the setPosition() call, just work your way out from the inside to figure out what is going on:

  1. SceneManager.getObject("Track 2") makes a call to the SceneManager class, to get the object named "Track 2"

  2. the :getPosition() call gets the position of the object in 3D space

  3. + vec(0, 0, 3) increases the z coordinate of the position by 3

  4. the setPosition() call is now passed 2 arguments: 2, meaning this is the second waypoint on myPath, and a position in 3D space.



myPath:setTarget(SceneManager.getCamera())
myPath:go()



are obviously instance method calls on the myPath object. See the documentation of the Path class in the Wild Pockets API Reference for details about the setTarget and go functions.