No instance found when calling a factory

Hey Defold people! :wave: I’m very new to Defold, although I’ve developed reasonably successful IOS and Android apps on the Gideros platform. Now looking for a new environment I’m going through Defold docs and tutorials and this seems to be a nice one to continue to develop in.

But there seems to be a knot that needs to be untied. I can’t get if there is something fundamental that I have missed in the organisation of objects and components or if there is something else.

I wanted to create an sprite using factory.create() in a script (although now I have stripped the project from everything unnessecary). I’m calling the factory instance within a game object, but I get this:

no instance could be found in the current script environment

This is my setup:

The script:

print("hej")
factory.create("#factory")

Out of docs and tutorials I understand #factory (or /go#factory) refers to the factory called “factory” in the “go” game object, but I’m clearly missing something here.

1 Like

The output says “line 2”, which suggests you don’t do this in the life cycle functions?
Move the code into e.g. the init() function of the script.

And welcome! :slight_smile:

2 Likes

No, those two lines were the only ones in the script. So, now as I understand it - although some code obviously works outside the predefined callback functions (init(), update() etc), some other doesn’t - like callling and communicating with other parts of the project. So it is highly recommended to stay within the functions, right? If so, that might be the untied knot. Moving on! :blush:

Thanks! I really like how one can put different pieces together in Defold, and not least the extensive documentation and reference library with examples. :orange_heart:

2 Likes

You can read more about the script life cycle functions here:

1 Like

Thank you, great article. :+1:

It says nothing about code logic outside functions though, which in this particular case makes some difference, obviously. :no_mouth:

(I’m aware that writing code that way deviate from common practice :slightly_smiling_face: )