Beginner: How to instance a grid of 2D sprites?

eri0oeri0o Posts: 2Member

Hello,

I made a 2DNode with 4 child sprites: jewel 1, jewel 2, jewel 3, jewel 4.

I want now to instance a 6x6 grid of them (randomly). I am not using the Grid Container because I want to animate they rolling (imagine Yoshi's Cookie). How to do this in Godot ? (I would imagine something like a for i using an array to store the instances, and some way to pass the positions on construction, but I couldn't understand how to create things using script)

Answers

  • SolarLuneSolarLune Posts: 35Member
    edited December 2017 Answer ✓

    You can instance a node by using the preload() function to get a Node, which you can instance using the instance() function on the Node. Then you add it to the scene tree or a child node. Here's an example:

    func spawnDust():
    
        # Preload the dust scene (preload loads the resource at script parsing, rather than when the function is called)
        var dustScene = preload("res://Resources/Objects/Dust.tscn")
    
        # Create the dust node
        var dust = dustScene.instance()
    
        # Attach the dust node to the root node
        get_viewport().get_child(0).add_child(dust)
    
        # Move it to wherever you want
        dust.translation = get_parent().translation
    

    Here's the documentation page on it.

  • eri0oeri0o Posts: 2Member

    Thanks! I made this from your answer! :D

Sign In or Register to comment.