+7 votes

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?

in Engine by (131 points)
edited by

2 Answers

+18 votes
Best answer

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)
by (5,274 points)
selected by

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

–4 votes

Easy as pie!

var bullet = Sprite.new()


func _ready():
    add_child(bullet, true)
    bullet.texture = "res://bullet_tex1"
by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.