How to instantiate a resource in VisualScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maer


I found how to load and preload a resource in VisualScript, but I could not figure out How can I instantiate a resource in VisualScript?

Thank you,

:bust_in_silhouette: Reply From: Zylann

Not all resources can be instanciated. When you load one, you already get that resource itself (eventually you can duplicate by calling duplicate() it but that’s probably not what you are looking for).

“Instancing” refers only to scene resources (i.e PackedScene), so when you load a scene, you can instance it by calling its instance() function. It returns the root node of the instance, which you can then add to the tree by calling add_child() on the parent node you want.

If, however, you mean creating a resource from scratch, or even nodes (i.e generate it, not loading one from disk), I have no idea if that works in VisualScript. In GDScipt that would just be or, but I’m failing to find an actual equivalent here. The closest I got was to create a new member variable in the list on the left, which might be enough in some cases but not if you intend to create multiple ones in a loop, or create nodes.

Thank you for the answer. Do you know what block should I use to call instance() function on preloaded resource?

maer | 2019-02-20 20:21

Also, just to clarify, are you looking for “instancing” a resource that you loaded, or create a new resource from scratch? (i.e generate one, not loading from disk)

Zylann | 2019-02-20 23:07

I’m looking for the way to instantiate resource. for example:

var tile_resource = preload('tile')
for i in range(5):

I want to preload resource then instantiate it couple times during game.

maer | 2019-02-21 14:02

So actually you want to instance a scene. I’m not familiar with VisualScript, so I digged a bit to learn how it can be done… and it’s quite involved, I wonder if I’m doing things badly or if VisualScript is really that complicated for things so simple^^
But I managed to translate your code, here is a working equivalent I got:

All sprites will end up at the same position though, you will have to modify it eventually.

Zylann | 2019-02-21 21:40