Help Passing Properties with collectionfactory.create (SOLVED)

Hello all,

I am trying to use my first collection factory and I cannot get it to properly pass properties to the collections it creates. I have created a spawn script which just attempts to distribute random game objects around the world and hopefully give them different sprites and values.

It works fine if I leave out the optional properties argument from the create function (however that means I have clones of the same collections spawning).

Here is the issue it raises, with the collectionfacotyr.create function:

     ERROR:SCRIPT: scripts/polypeptide_factory.script:17: bad argument #7 to 'create' (hash expected, got string)
        stack traceback:
          [C]:-1: in function create
          scripts/polypeptide_factory.script:17: in function <scripts/polypeptide_factory.script:3>

Here is my spawn script for the collections:

go.property("spawn_count", 10)
local imgs = {hash("gear"),hash("polypeptide sheet"),hash("spring")}
function init(self)
	math.randomseed(os.time())
	self.spawned_molecules = {}
	for i = 1, self.spawn_count do
		local p = go.get_position()
		p.x = p.x + math.random(-1000,1000)
		p.z = p.z + math.random(-1000,1000)
		
		local img = imgs[math.random(1,3)]
		local type = hash("polypeptide")
		
		pprint(img, type)
		local component = "#polypeptide_collection_factory"
		local props = {sprite_image = img, type = hash("polypeptide")}
		local id = collectionfactory.create(component, p, nil, props )
		table.insert(self.spawned_molecules, id)
	end

In the collection script I have these lines

go.property("type", hash("unspecified"))
go.property("sprite_image", hash("spring"))

and in the initialization I have set the sprite image with the following line:

msg.post("#sprite", "play_animation",{id = self.sprite_image})

This same code worked fine for a regular factory, so I am lost.
Thanks for any assistance you can provide.

also-- I am new to Defold and game development in general and just want to give a big thanks to all the awesome people on these forums because I’ve already learned so much.

You have to specify which object the properties apply to when using collection factories:

2 Likes

I swear I read that passage but it didn’t sink in until you re-shared it.
I have altered my properties and now it works like a charm (and it makes a lot of sense now too.)

props[hash("/game_resource#object")] = {sprite_image = img, resource_type = type}

Thank you!

2 Likes