How can I change my player texture with script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Xenapibe

Hello I want to create a store system to my game. But I have some issues, I want to change sprite when I pressed the buy button. But I can’t create a algorithm for this can someone help me ? So I want to change my sprite texture with global script. How can I do that?

Godot Version : 3.5.1
Game Platform : Android

You will need to share a bit more detail about how you implemented your button. Are you using a Node2D or a Sprite?

Did you see that there a TextureButton Control node in Godot?

Can you share the code that draws your button?

Juxxec | 2022-11-30 09:22

My Button code

func _on_BuyCharacter_pressed():
if GlobalLevel.GlobalCoin >= 0:
	$StorePanel/CharacterBuy/Sold.visible = true
	$StorePanel/CharacterBuy/Goblin.self_modulate = Color.crimson
	$StorePanel/CharacterBuy/Locked.visible = true
	$StorePanel/CharacterBuy/Goblin/Particles2D.visible = false
	GlobalLevel.GlobalCoin = GlobalLevel.GlobalCoin - 0
	$CollectedCoins.text = str(GlobalLevel.GlobalCoin)
	GlobalLevel.save_coin()

Xenapibe | 2022-11-30 09:23

And my Global script for saving and loading skin

  func save_skin():#save skin
	var SaveSkin = File.new()
	var data: Dictionary = {"skin": GlobalSkin}
	SaveSkin.open("user://skin.dat", File.WRITE)
	SaveSkin.store_var(data)
	SaveSkin.close()

func load_skin():#load skin
	var SaveSkin = File.new()
	if !SaveSkin.file_exists("user://skin.dat"):
		return
	SaveSkin.open("user://skin.dat", File.READ)
	var data = SaveSkin.get_var()
	GlobalSkin = data["skin"]
	SaveSkin.close()

Xenapibe | 2022-11-30 09:24

Also my GlobalSkin variable

var GlobalSkin: Dictionary ={"Goblin":Goblin,
    	"Player": DefaultPlayer}

Xenapibe | 2022-11-30 09:25

If you want to check the images of my code you can go my reddit post for this issue
Reddit post

Xenapibe | 2022-11-30 09:26

:bust_in_silhouette: Reply From: Juxxec

So your question here is: “How can I know that the player has bought a skin, so that I can remove the Buy button?”.

Anyway, that button by the looks of things is either a regular Button with a StyleBox applied or is a TextureBuffer with a set of textures applied.

For TextureButton, you can use the texture_normal property to change the texture.

For a Button, you can use set("custom_styles/normal", StyleBoxFlat.new()))

BTW, you can save StyleBox resources and then load them with load() and preload().