I’m unsure if this is a bug or not, but I have dug through the gdevelop javascript manual, and the github repository to try to find how the “setTexture” method is supposed to be used for a Tiled Sprite object but haven’t found anything that’s helpful. Also dug through forum threads here without a definite answer.
This is the method I’m talking about: https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.TiledSpriteRuntimeObject.html#setTexture
It looks like to me, that this method is supposed to, by itself, set the texture used by a Tiled Sprite object. However, in my tests, this is not working as the Gdevelop default logo texture is used instead.
Here’s the javascript code I’m currently trying:
const tiled_sprite = runtimeScene.getObjects("NewTiledSprite")[0];
const inst_cont = tiled_sprite.getInstanceContainer();
tiled_sprite.setTexture("new_texture.png", inst_cont);
There’s no errors from this when using a correct image name. Using an incorrect name will throw an error however, which tells me that the method works to some extent as it can find the correct image. It just seems like it doesn’t set it properly or something.
Now I’m wondering if I should file a bug report or if there’s just something missing to this?
I’ve attached a very simple example project to illustrate the issue:
https://www.dropbox.com/scl/fo/jwhnfsvbx76osh8qs450d/AIsPwid8vX6JH58Y084Cukc?rlkey=zf3zno09o2jfz2dha5di6td4n&st=j4ijdqzo&dl=0
IDK if this a bug either. I just tried it and it works sometimes. The events look nearly identical to the ones I have in a document. Either I wrote them years ago or I copied them.
I don’t know if it’s a file format or naming or path but it works for some resources but not others. When it doesn’t work, the debugger says something like resource not found.
My events.
I don’t know if there’s a more preferred Javascript method. You could look through the extensions and see how they do it even if they don’t use tiled sprite there might be a clue.
Hey Keith!
Yeah I was thinking, since there’s an action for changing an image for the tiled sprite, there must be underlying code for it that I should be able to replicate. So I’ve been relentlessly going through the github files for the tiled sprite extension itself and so on, but couldn’t find what the difference is between how it’s in the files vs this code you have (which is where I got the code from, you posted it in another thread some time ago
).
I decided to dissect the “Load image from url” extension on github and found something that works, but isn’t using the setTexture function but slightly more complex.
This is what I got to work, in case helpful for anyone:
var tiled_sprite = runtimeScene.getObjects("NewTiledSprite")[0];
const renderer = tiled_sprite.getRendererObject();
renderer.texture.baseTexture = PIXI.BaseTexture.from("new_texture.png");
This is where I learned it from: https://github.com/GDevelopApp/GDevelop-extensions/blob/main/extensions/community/LoadImageFromURL.json#L45
The pixi class isn’t documented in gdevelop but has its own documentation (pixi.js): https://pixijs.download/release/docs/rendering.Texture.html.
1 Like
At this point, when youre programming JavaScript in GDevelop, you have to stop thinking how GDevelop works and starting thinking how it works. Honestly its API sucks anyway.
You have full access to pixi.js and electron in GDevelop, no setup required.
One problem with my earlier solution is that I’m changing the base texture with renderer.texture.baseTexture
This causes all object instances to also change texture, which isn’t what I want. So the correct way is:
renderer.texture = PIXI.Texture.from(color_picked + ".png")
Here it only changes the texture of the picked instance instead. Thought I’d add this for those coming to this post looking for answers to the same issue.
1 Like