[SOLVED] Why are my gameobject properties not initialized on object creation?

Hello. I am currently making a somewhat basic breakout game to get a better understanding of Defold’s ways.

For now, I am working on the paddle’s logic and I recently changed some of it’s controller script’s inner variables (speed, width, etc.) into gameobject properties.

The thing is, these properties doesn’t seem to be registered correctly and I am unable to find why.
I think it have to do with the fact that the paddle isn’t a gameobject but a collection, and is spawned at runtime using a collectionfactory.

This is the error the engine gives me :

ERROR:GAMEOBJECT: Properties can not be of type 'nil'.
ERROR:SCRIPT: scripts/paddle_controller.script:10: the property 'scale.x' of 'section_middle' must be a number  
--the self.width is nil because the go.property("width", value) doesn't exists in the gameobject

stack traceback:
  [C]:-1: in function set
  scripts/paddle_controller.script:10: in function resize
  scripts/paddle_controller.script:42: in function <scripts/paddle_controller.script:28>
  [C]:-1: in function create
  levels/1/level_controller.script:14: in function <levels/1/level_controller.script:3>

this is the /level#controller script (init function) :

	local width, height = window.get_size()
	local position = vmath.vector3(width / 2, height / 2, 0)
	local properties = {
		[hash("/root#controller")] = {
			min_width = constants.PADDLE.MIN_WIDTH,
			max_width = constants.PADDLE.MAX_WIDTH,
			width = constants.PADDLE.DEFAULT_WIDTH,
			speed = constants.PADDLE.DEFAULT_SPEED,
		}
	}
	self.paddle = collectionfactory.create("/paddle#spawner", position, nil, properties)

And the /root#controller script (for the paddle)

local constants = require "modules.constants"

go.property("min_width", constants.PADDLE.MIN_WIDTH)
go.property("max_width", constants.PADDLE.MAX_WIDTH)
go.property("width", constants.PADDLE.DEFAULT_WIDTH)
go.property("speed", constants.PADDLE.DEFAULT_SPEED)

-- Changes the paddle width
local function resize(self, factor)
	go.set(self.sections.middle, "scale.x", factor)
	self.width = factor

	local side_width = go.get(self.sections.left.."#sprite", "size.x")
	local half_width = factor + side_width / 2
	go.set(self.sections.left, "position.x", -half_width)
	go.set(self.sections.right, "position.x", half_width)
end

function init(self)
	local DIRECTIONS = constants.DIRECTIONS

	--ids for each game object in the paddle's collection
	self.sections = {
		left = "section_left",
		middle = "section_middle",
		right = "section_right",
	}

	--paddle movement
	self.is_moving = false
	self.direction = DIRECTIONS.NONE

	resize(self, self.width)

	msg.post(".", "acquire_input_focus")
end

What could be my mistake in this case ?

When we parse the values for the gameobject properties, we don’t actually evaluate and run Lua, but we parse the text. So you cannot use the “constants.PADDLE.MIN_WIDTH”.

Instead, you have to do something like:

go.property("min_width", 64)
1 Like

Thank you.

I coudn’t have guessed something like that.

1 Like