The variable does not change

Godot Version

4.3

Question

Good evening. My variable speed is not changing. Everything

Script where speed is not changing.

Script:

if PlayerStat.mental < 100 and !one_ment:
    one_ment = true
    PlayerStat.stat(1, 0, 10)
    await get_tree().create_timer(5.00).timeout
    one_ment = false
if PlayerStat.mental <= 50 and !two_ment:
    two_ment = true
    PlayerStat.stat(0, 0, 10)
    await get_tree().create_timer(5.00).timeout
    two_ment = false
if PlayerStat.mental >= 100:
    pass

Function stat from the global plugin:

func stat(stat, plmn, inte):
    if stat == 0:
        if plmn == 0:
            HP -= inte
            if HP > 100:
                HP = 100
            if HP < 0:
                HP = 0
        else:
            HP += inte
            if HP > 100:
                HP = 100
            if HP < 0:
                HP = 0

    if stat == 1:
        if plmn == 0:
            speed -= inte
            if speed > 200:
                speed = 200
            if speed < 0:
                speed = 0
        else:
            speed += inte
            if speed > 200:
                speed = 200
            if speed < 0:
                speed = 0

    if stat == 2:
        if plmn == 0:
            mental -= inte
            if mental > 200:
                mental = 200
            if mental < 0:
                mental = 0
        else:
            mental += inte
            if mental > 200:
                mental = 200
            if mental < 0:
                mental = 0

P.S. I know it looks bulky and doesn’t seem to make much sense.

I can not help you directly but you can try checking to which line your code works as expected (to find the reason for the problem) by using a break point or printing things out on certain parts of your code.

A few additional advices:

  • you can use the match() statement instead of many if statements after another
  • maybe consider using an integer instead of variables called one_ment, two_ment, …
#
var ment_stat : int = 0


#
match(stat)
   case 0:
      # code
      break
   case 1:
      # code
      break

#
if PlayerStat.mental < 100 and ment_stat != 1:
   #...
elif PlayerStat.mental <= 50 and ment_state != 2:
  #...
elif ...
   #...
1 Like
if PlayerStat.mental < 100 and !one_ment:
    one_ment = true
    PlayerStat.stat(1, 0, 10)
    await get_tree().create_timer(5.00).timeout
    one_ment = false
if PlayerStat.mental <= 50 and !two_ment:
    two_ment = true
    PlayerStat.stat(0, 0, 10)
    await get_tree().create_timer(5.00).timeout
    two_ment = false
if PlayerStat.mental >= 100:
    pass

According to this part of the code, when you call PlayerStat.stat(), you always set “plmn” to 0. But it seems, in the following code, that you need “plmn” to be another value to change speed:

    if stat == 1:
        if plmn == 0:
            speed -= inte
            if speed > 200:
                speed = 200
            if speed < 0:
                speed = 0
        else:
            speed += inte
            if speed > 200:
                speed = 200
            if speed < 0:
                speed = 0
1 Like

I didn’t quite understand you. But I’ll add to everything I said that in other scripts (for example, when the player should stop during a dialogue), the function worked perfectly.

Example:

if Dialogic.VAR.get_variable("STOP"):
	PlayerStat.stat(1, 0, 200)
elif !Dialogic.VAR.get_variable("STOP"):
	PlayerStat.stat(1, 1, 200)
	$Training.visible = true

I think you’re right, but unfortunately, I won’t have time to rewrite everything. I decided to participate in a game jam. The deadline for submissions is in a few days. I’ve been working on the game for about 4 days now.

When you say the variable speed is not changing. Do you mean it doesn’t increase or that it doesn’t go back to zero?

1 Like

The variable should decrease by 10 every 5 seconds during an attack. Maybe I’ll just decrease it by a certain amount, but the problem is that the variable doesn’t change in any case. Perhaps I made a mistake in other scripts (there are more than 10), and I missed this error. But everything seems normal.

PlayerStat.stat(1, 0, 10) - 1 (speed), 0 (minus), 10 (amount).

Oh, I get it now. Assumed the increase was the one that was not happening. The script works properly during dialogs according to your previous post so, I would double check if the variables are correct during the attack. Not the “speed” variable, we know that one doesn’t work. I meant the variables that are checked for “speed” to change. So far those checks seem to be the variables inte, plmn and stat (not to be confused with the function of the same name). Seems that plmn especially is being used as a boolean.

1 Like

I tried to finish as soon as possible, to get to the game jam, but I didn’t have time. Thanks for the help. Most likely, I’ll just rewrite some of the scripts. Thanks again.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.