issue changing a GLOBAL variable

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

i wrote this script in “GLOBAL.gd”
it changes the sprite texture when i click a button in the “Player.gd”

extends Node

var damage = 1

#################weapon_equipment###############
const weapon_4 = preload("res://Player/weapons/swords1_64.png")
const weapon_1 = preload("res://Player/weapons/swords2_64.png")
const weapon_2 = preload("res://Player/weapons/swords4_64.png")
const weapon_3 = preload("res://Player/weapons/swords3_64.png")
onready var equiped = dagger_sword
func change_weapon():
	if equiped == weapon_4:
		equiped = weapon_1
		damage = damage -1
		damage = damage +3
	elif equiped == weapon_1:
		equiped = weapon_2
		damage = damage -3
		damage = damage +5
	elif equiped == weapon_2:
		damage = damage -5
		equiped = weapon_3
	else:
		equiped = weapon_4
		damage = damage +1
################################################

the code in “Player.gd” regarding previous is:

export var damage = 1
##################weapon_equipment###############
onready var weapon_equiped = $weapon
func _unhandled_input(event):
	weapon_equiped.texture = Global.equiped
	if event.is_action_pressed("change_weapon"):
		Global.change_weapon()
#################################################

$weapon is the sprite where the textures is.

the “textures” is changing and “damage” too but the problem when scene changes the “damage” reset but the “texture” working fine.
I need help to make the “damage” stay in the new values and never changes when changing scenes.

By the way, in your global script, use a match statement instead of those if statements. You don’t want to turn into Yandere Dev.

https://i.redd.it/gyubzyq87xmx.png

rahsut | 2020-08-23 13:16

Thanks for your brilliant idea. How about the issue am asking for?

Sha6er | 2020-08-23 13:47

Hi,
does the player has a _ready function?

What does

    damage = damage -3
    damage = damage +5

isnt that

damage += 2

why does get incremented/decremented? Is that what you want to do? looks a bit odd to me

klaas | 2020-08-23 14:56

Yes the Player has a ready function written in it
damage = Global.damage

And yes you are right about the equation it is the same as you mentioned at your reply
But all roads leads to Rome my friend.
How about the damage reset after the scene changes any idea how to fix it.

Sha6er | 2020-08-23 15:11

well … i dont see the error. I think its in the programm flow.

I would set a breakpoint just before you switch the scene, then “step into” to analyse what is happening then.

klaas | 2020-08-23 15:17

Should making setter and getter inside the Global for the variable damage may solve this issue?

Sha6er | 2020-08-23 19:45

That wouldnt change anything. Have tried breakpoint? I strongly suggest to do so.

klaas | 2020-08-23 19:50

:bust_in_silhouette: Reply From: Sha6er

thank you my friends for your precious time to solve this issue
just to update what was the issue:

I wrote a setget function for damage in GLOBAL it solves it.

var damage = 1 setget set_damage, get_damage

func set_damage(value):
	damage = value
func get_damage():
	return damage