Syntax for factory.create() (SOLVED)

So I have a few questions about factory.create()

First off, I have an error that I’m trying to resolve


I’m not sure if the syntax is just wrong or if my setup is somehow wrong. The idea is that I have my gui script that contains the logic for what happens when you click on a button. What I want to do right now is spawn an image (currently attached to a game object) when a button is pressed. Clearly, my code is not doing that (hence the error) and I’m wondering where I’m going wrong.

Secondly, can factory objects be saves and stored in a table? What i want to ultimately accomplish (once I figure out how to use factory.create) is store all the game objects I’ve created through the factory in a table and later delete them. Is it possible to do something like:

created_object = factory.create(url, [pos])
table.insert(name_of_table, created_object)

And would removing that object with table.remove() delete the object entirely?

1 Like

I am new to using Defold, so there may be a better way; but, perhaps you can try this to correct syntax:

factory.create("#LightAttackFactory", vmath.vector3(10, 10, 0))

This example shows how to create factory objects, and store them in tables. It think it does what you are looking for.

2 Likes

That fixed the error, thanks. Ended up with another problem with the fourth value (it doesn’t like you putting nil for the the property aspect) but I worked around it by making a nil table and passing that in. Needed the fifth spot for scale.

2 Likes

factory.create() returns the url path for the created object, so yes you can store that in a table exactly like you said. It’s just the url though, so removing it from the table won’t do anything to the object. You need to use go.delete(created_object) for that. (and then remove the url from the table.)

4 Likes

Quick comment just to make sure things are completely clear, factory.create returns an id right? Not a url. Or at least that’s what everything I’ve looked up has been telling me.

1 Like

You are correct, it’s an id, not an url. (factory documentation)

2 Likes

Ah, yeah sorry I said that wrong. I meant the “path”/id part of the url. Socket:path#fragment

1 Like