(Answered)How to change the texture of a Sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PugMasterPug
:warning: Old Version Published before Godot 3 was released.

I have a bullet, but i want that depending on a variable it will show one texture or another (this is made so enemy bullets look different that player bullets without needing to create a new node)
How could I do it?

:bust_in_silhouette: Reply From: avencherus

This is somewhat pseudo code, but you can do something like this.

var bullet_tex1 = preload("res://bullet1.tex")
var bullet_tex2 = preload("res://bullet2.tex")
var bullet_tex3 = preload("res://bullet3.tex")

onready var bullet_sprite = get_node("bullet_sprite_node")

func switch_texture(num):

	if  (num == 1):
		bullet_sprite.set_texture(bullet_tex1)
	
	elif(num == 2):
		bullet_sprite.set_texture(bullet_tex2)
	
	else:
		bullet_sprite.set_texture(bullet_tex3)

Just for best practice should you not use a match statement in switch_texture?

sackidude | 2020-07-05 15:57

:bust_in_silhouette: Reply From: CopperyMarrow15

Easy as pie!

var bullet = Sprite.new()


func _ready():
    add_child(bullet, true)
    bullet.texture = "res://bullet_tex1"