Hi, i started to make a little project rpglike to learn to programming but when i tried to do a preliminar damage system by pressing a button like dungeons and dragons, but it just rest damage one time, after i press the button again it doesn't do anything or if i change the things from damage function to on_pressed it just changes the damage value instead of substracting more damage from the health. what am i doing wrong?
My Code:
extends Button
var maxhp = 20
var hp = 20
var atq = 2
var dice = randi_range(1, 20)
var total = ""
var dmgtotal = ""
var dmghalf = atq / 2
var luck = atq / 2
var dmgluck = atq + luck
var critic = atq * 2
var hpres = maxhp
func _ready() -> void:
$hp.set_text("HP: " + "20")
func _on_pressed():
calc_damage(hpres)
$hp.set_text(dmgtotal)
$tdmg.set_text(total)
$result.set_text(str(dice))
func calc_damage(hpres):
match dice:
1,2,3,4,5:
total = "Fallaste el ataque..."
dmgtotal = "HP: " + str(hp)
return hpres
6,7,8,9,10:
total = "Hiciste " + str(dmghalf) + " de daño!"
dmgtotal = "HP: " + str(hp - dmghalf)
hpres -= dmghalf
return hpres
11,12,13,14,15,16,17:
total = "Hiciste " + str(atq) + " de daño!"
dmgtotal = "HP: " + str(hp - atq)
hpres -= atq
return hpres
18,19:
total = "Hiciste " + str(dmgluck) + " de daño! (Suerte)"
dmgtotal = "HP: " + str(hp - dmgluck)
hpres -= dmgluck
return hpres
20:
total = "<¡Critico!> Hiciste " + str(critic) + " de daño"
dmgtotal = "HP: " + str(hp - critic)
hpres -= critic
return hpres
You have two variables, hp and hpres, and I am not sure what the difference is supposed to be? You use hp to calculate what the dmgtotal string should contain, but the hp variable itself never gets updated, instead you subtract from hpres.
Also, the dice variable gets set at the beginning, but never recalculated. May I suggest that you do:
dice = randi_range(1, 20)
at the beginning of the _on_pressed function? That way, you’ll get a new dice roll every time.
Well i was testing that maybe it could work if i did a variable for the hp when it was substracted the damage could work but it didn’t either, so the code normally looks like this
extends Button
var maxhp = 20
var hp = 20
var atq = 2
var dice = randi_range(1, 20)
var total = ""
var dmgtotal = ""
var dmghalf = atq / 2
var luck = atq / 2
var dmgluck = atq + luck
var critic = atq * 2
func _ready() -> void:
$hp.set_text("HP: " + "20")
func _on_pressed():
calc_damage()
$hp.set_text(dmgtotal)
$tdmg.set_text(total)
$result.set_text(str(dice))
func calc_damage():
match dice:
1,2,3,4,5:
total = "Fallaste el ataque..."
6,7,8,9,10:
total = "Hiciste " + str(dmghalf) + " de daño!"
dmgtotal = "HP: " + str(hp - dmghalf)
11,12,13,14,15,16,17:
total = "Hiciste " + str(atq) + " de daño!"
dmgtotal = "HP: " + str(hp - atq)
18,19:
total = "Hiciste " + str(dmgluck) + " de daño! (Suerte)"
dmgtotal = "HP: " + str(hp - dmgluck)
20:
total = "<¡Critico!> Hiciste " + str(critic) + " de daño"
dmgtotal = "HP: " + str(hp - critic)
Well the dice kinda works, i didn’t know how to make a variable that i could put on a function and other at the same time, but now it works like how i programmed it almost at the start when i had everything on the _on_pressed, the values change but the hp doesn’t continue going down, it just changes depending on the conditions i put on the damage function.
My point is that the hp variable never gets set to anything other than 20. Try this:
func calc_damage():
match dice:
1,2,3,4,5:
total = "Fallaste el ataque..."
dmgtotal = "HP: " + str(hp)
6,7,8,9,10:
total = "Hiciste " + str(dmghalf) + " de daño!"
hp = hp - dmghalf # actually change value of hp variable
dmgtotal = "HP: " + str(hp)
# ... and so on for the other cases
I just figured out that i could be that five minutes ago and i was about to post that, but thanks a lot for the help, i even optimized it a bit i think.
match dice:
1,2,3,4,5:
total = "Fallaste el ataque..."
dmgtotal = "HP: " + str(hp)
hp -= 0
6,7,8,9,10:
total = "Hiciste " + str(dmghalf) + " de daño!"
dmgtotal = "HP: " + str(hp)
hp -= dmghalf
11,12,13,14,15,16,17:
total = "Hiciste " + str(atq) + " de daño!"
dmgtotal = "HP: " + str(hp)
hp -= atq
18,19:
total = "Hiciste " + str(dmgluck) + " de daño! (Suerte)"
dmgtotal = "HP: " + str(hp)
hp -= dmgluck
20:
total = "<¡Critico!> Hiciste " + str(critic) + " de daño"
dmgtotal = "HP: " + str(hp)
hp -= critic