Changing Tiled Objects' Texture Event

Hello gdevelop!!
Soooooooo, it would be cool if there was an action to change the texture of objects.
"But derpdev… its called animations, u can use them on sprites… "


All jokes aside, I mean for non sprite objects, like Tiled-Sprites. You see, I want to change how a tiled sprite looks, so a feature to change the texture of an object would sure come in handy! You guys already kind of did it in the editor with the Texture thing:

Would it be easy to just add an action to do the same in game?

2 Likes

i feel like you’re referencing Acerola in the beginning :moyai:
Either way, i think this’d be pretty cool to see

I agree, but what about making the title more accurate?

Action for changing TiledSprite texture.

HOLY MOLY THEY ACTUALLY ADDED IT OMG
TYSM GDEVELOP!!!
edit: only for panel sprites :/

1 Like

I managed to use the “Set Image image_file on Object_name”.

But, I havent found a way to change the image, on a per instance of this object yet.

Tried in JS but not successful:

// Name of the Panel Sprite object in GDevelop
const panels = runtimeScene.getObjects("MY_OBJECT");

// Make sure at least one Panel exists
if (!panels || panels.length === 0) {
    console.warn("No Panel object found in the scene.");
    return;
}

// Texture you want to switch to
const newTextureName = "new_image.jpg";  
console.warn("Creating NewTexture.");

// Apply new texture to every instance
panels.forEach(panel => {
    const rendererObject = panel.getRendererObject();

    if (rendererObject) {
        rendererObject.texture = PIXI.Texture.from(newTextureName);
        console.warn("Applying Texture");
    } else {
        console.warn("Renderer object not found for Panel.");
    }
});

Edit : are you doing panel or tiled? My reply was about tiled.

Does setTexture do the same thing.

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.TiledSpriteRuntimeObject.html

If not, you could add an image that’s sort of a spritesheet and change the offsets.

I was looking through my old cheat sheet and I had this. Not sure if it still works or is the correct way. Since they’re instances, it might be all or nothing. IDK

//container - change texture
//-----------------------------
const obj = runtimeScene.getObjects("NewTiledSprite")[0];
const iContainter = obj.getInstanceContainer();
const x =obj.setTexture("car.jpg",iContainter);

1 Like

shirts

@Keith_1357 - that’s genius! - its like going to an Arsenal game at 12 o’clock with a shirt that’s 3m long tucked into your trousers and after that game you pull the next bit up out of your trousers, peg it at the shoulder and head off to the Tottenham game at 3 o’clock!

1 Like

So, here is what I was able to do:

  • Create an object (Panel Sprite) in Editor and select an image
  • Create multiple instances of this object in the editor
  • With Event, I was able to change the image of the object (which was reflected on all instances) [ Action: Set Image on Object ]
  • From JS using SetTexture I was able to change the image of selected instances

But … I could only used the original texture of the object (as defined in the editor).
When I tried to use another resource, it will not work and only display the purple Gdevelop logo …

To be continued …

1 Like

So, today’s testing:

  • If I use, “Set the image” of the object with Event [ Action: Set Image on Object ], at least once.
  • Then I can use SetTexture for this particular image/texture in JS.

So, after the object action : “PanelSpriteObject::SetImageFromResource .”, then the SetTexture will work properly.

All of these is for Panel.