Welcome!

You are just one step away from creating awesome games!

You need a Google account to use Defold

HOW TO SUBMIT

To submit your game, simply log in to your Defold dashboard, click “submit your entry”, and follow the instructions!

HOW TO SUBMIT

Sorry, you can't submit your game from a phone. To submit your game, you need to log in to Defold on a desktop.

- Select manual -

Script properties

Script properties provide a simple and powerful way of exposing properties that control the behavior or look of a specific game object instance and allow non coders to change them in the editor.

Script properties allow for instance specific overrides of script properties. Common use cases is to set the health or speed of a specific enemy AI, the tint color of a pickup object, or what message a button object should send when pressed—​and where to send it. There are many cases where script properties are very useful:

  • When you wan to override their values for specific instances in the editor, and thereby increase script re-usability.
  • When you want to spawn a game object with initial values.
  • When you want to animate the values of the properties.
  • When you want to access the data in one script from another.
If access the data frequently, it is better to access it from a table rather than from properties in a foreign script for performance reasons.

Most types of values that you use to control script behavior or component properties can be exposed as script properties:

Booleans

True or False

Numbers

Numerical values.

Hashes

Hashed string values.

URLs

References to objects or components.

Vector3

3 dimensional vector values.

Vector4

4 dimensional vector values.

Quaternion

Quaternion values.

Suppose that you have a script which controls the health of a game object. The script can respond to damage through a message called take_damage:

function init(self)
    self.health = 100
end

function on_message(self, message_id, message, sender)
    if message_id == hash("take_damage") then
        self.health = self.health - message.damage
        if self.health <= 0 then
            go.delete()
        end
    end
end

If the health reaches 0, the script destroys the game object. Now suppose that you want to use the script in two different game objects, but want the game objects to have different amounts of health. With the function go.property you can declare the property a script property and it will be set by the specific game object instance:

-- self.health will be automatically set to 100 by default
go.property("health", 100)

function on_message(self, message_id, message, sender)
    if message_id == hash("take_damage") then
        self.health = self.health - message.damage
        if self.health <= 0 then
            go.delete()
        end
    end
end

Any game object that includes the script can set the value specifically. Simply select the script node in the Outline view in the editor and the property appears in the Properties view allowing you to edit it:

Script Properties
In the case where you have the game object inside a sub-collection, expand the game object node in the collection to select the script.

The editor property inspector will automatically show a widget that is feasible for the type of property that you have declared. Numbers are entered in text boxes, vectors and quartenions as a set of numbers in boxes, hashes allow you to enter the string that will be hashed and URLs give you a drop down with all relative, local (that is, within the same collection) objects and components. You are still able to enter URL values manually, too.

Factory created objects

If you use a factory to create the game object, it is possible to set script properties at creation time:

function on_message(self, message_id, message, sender)
    if message_id == hash("create_my_object") then
        factory.create("#factory", go.get_position(), go.get_rotation(), { health = 50 })
    end
end

The property values provided via factory.create() override any value set in the game object file as well as the default value in the script.

If you have several script components attached to a game object, it is possible to put script properties in any of the script files. Suppose that the game object example above had another script component also containing the property health. When factory.create() is called, the health property for both scripts are set to the provided value.

I want to report anonymously.