0 votes

Hello,

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,

in Engine by (24 points)

1 Answer

+2 votes

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 ResourceType.new() or NodeType.new(), 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.

by (29,088 points)
edited by

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

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)

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

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

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

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:
enter image description here

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.