Adding GO + Sprite into an embedded GO layer in editor_script

Hi there,

Newby there. I want to create my first editor_script, it named sprite palette, I want to achieve something similar with sprites what Defold already do with tilesets: showing all the images+animations in .atlas file to choose from, then it should create the go+sprite (or the sprite only if applicable) into the curently selected go layer.
I’m stick with filling out sprite properties like image + default animation (ot texture and default_animation)

I’m using this piece of code, which correctly creates the GO and an empty sprite, you can check my failed attempts commented out:

-- use a transaction to create an embedded GO with sprite inside the layer
editor.transact({
    editor.tx.add(layer, "children", {
        type = "go",
        id = base_name,
        position = {pos_x, pos_y, pos_z},
        components = {
            {
                type = "sprite",
                id = "sprite99"

                --image = atlas_path
                --textures = {
                --    texture = atlas_path 
                --}
                -- default_animation = sprite_id
                -- data = '{\n'
                --    .. 'material: "/builtins/materials/sprite.material"\n'
                --    .. 'textures {\n'
                --    .. '  sampler: "texture_sampler"\n'
                --    .. '  texture: "' .. atlas_path .. '"\n'
                --    .. '}\n'
            }
        }
    })
})

So I’m stuck here cannot fill out image and default animations properties, Defold says that not ‘data’ or ‘image’, ‘textures’ property to set

ERROR:EXT: Sprite Palette’s “run” in /editor/sprite_palette.editor_script failed: /editor/sprite_palette.editor_script:281 Can’t set property “data” of EmbeddedComponent
ERROR:EXT: Sprite Palette’s “run” in /editor/sprite_palette.editor_script failed: /editor/sprite_palette.editor_script:281 “Cloud_1” is not

Let’s see if @vlaaad can provide you with an answer on Monday!

Hi @vlaaad by anaylzing the sprite.clj file (used AI tho help) found out that this is actually works as far as I don’t try to set default_animation:

    -- use a transaction to create an embedded GO with sprite inside the layer
    editor.transact({
        editor.tx.add(layer, "children", {
            type = "go",
            id = base_name,
            position = {pos_x, pos_y, pos_z},
            components = {
                {
                    type = "sprite",
                    id = "sprite99",
                    __sampler__texture_sampler__0 = atlas_path --,
--                    default_animation = sprite_id
                }
            }
        })
    })

Hey, yes, as you noticed, a property with an image label uses __sampler__texture_sampler__0 name. This is because this property is actually dynamic and depends on a material that may define 0, 1, or more sampler images. Default animation currently cannot be set in the same transaction that sets the atlas path, because its allowed values depend on the atlas path, but since the component value is an unordered lua table, it’s problematic to do it reliably… So you can do it in a separate transaction for now. I created a PR to allow setting any default animations, so the situation should improve in the new versions.

1 Like

Thank you @vlaaad , just what I needed! Noticed that the correct name is in fact showed in the tooltip of the image, I was just too ignorant to notice.
Now all my new sprites created with my tool placed to the correct layer but in the 0,0,0 position (actually I made a dialog to set a position, but I will never set the position by hand so will be removed). Is there a chance that I can get the editor’s current positions to put the sprite to the current view’s centre instead? I can imagine that it wouldn’t be that easy as we actually see a 3d view, not a simple 2d editor screen