Search code examples
methodsluafieldselfdefold

Defold what should be part of self


What should I put as fields and methods of self in game objects? Should I put position and velocity as fields? What methods should I put in self? What should be fields of ob? Should I put velocity and position as

self.velocity = vmath.vector3()

or something like

go.velocity = vmath.vector3()


Solution

  • The self object is a Lua userdata storage for script instance values. What you put there is completely up to you. The nice thing about storing values in self is that the self object is passed to all of the lifecycle functions of the script (init, final, update, on_input, on_message, on_reload). This means that you have quick access to the variables in all of the functions.

    Storing the velocity in self is a good example of something that you may want to access and modify in different places in your script. For instance setting the velocity to some value in on_input when a button is pressed and then use the same velocity value in update to move the object.

    go.velocity = vmath.vector3()

    This is not good. What you are doing there is to assign the variable velocity to the globally accessible go.* namespace containing engine provided functions to manipulate game objects.