#How to programmatically change a Texture?

1 messages · Page 1 of 1 (latest)

icy spindle
#

Hello. I can't find how to programmatically change a texture. I'm working on a card game in 2D and I essentially want to be able to "flip" the card from the back to the front, vice/versa.

In it's simplest form, I was planning on doing this by swapping out the textures from the CardBack resource (png) in my hirerarchy to another.

For the life of me, I can't find this in the docs. I'm seeing resourceloader/preloader examples...

Many examples are using var texture = load("my file"); And assigning it but this load method is not coming up in code completion for me.

In the area I'm working, I have a line of code...

sprite.Texture = cardBackTexture;

Where cardBackTexture is a Texture2D type, as that's what the .Texture property is. But I can't make any headway of this. I'm sure there's a simpler way to do this. Any help is appreciated. I'm using C# but I imagine that really doesn't matter...

Thx.

untold turtle
#

it is done with load/preload, ideally you load it a single time into some variable, then assign it as needed to the texture

#

for example:

const front_texture = preload("path/to/image")
const back_texture = preload("path/to/image")

func some_function():
  sprite.texture = front_texture
#

i gave my example in gdscript, but i'm sure it should work the same in C#

#

unless there's some behavior differences in loading in C# that i'm not aware of