Gelya
Gelya

Reputation: 23

Why isn't my game script loading as expected?

I am trying to learn gdscript and started making this fishing mechanic I am not sure what is wrong with it.

It's supposed to generate a random letter and if the player presses it correctly it knocks some "health points" out of the fish if the wrong key is pressed it heals the fish a it and then stops when the fish's health reaches 0.

When I try running it it just doesn't load it doesn't crash or anything. I tried removing the while loop and it seems to run but not as I wanted it to. Without the if nothing appears on the screen at first but if I press a key it shows if it was the right one or not and adds/removes health from the fish.

CODE:

extends Label


var letters: Array[String] = ["Q", "W", "E", "R"]
var cletter: String
var fish = 100 
var code

func _input(event):
    while fish > 0 && fish < 101:
        if(event is InputEventKey):
            cletter = letters.pick_random()
            text = cletter
            code = OS.find_keycode_from_string(cletter)
            if(event.keycode == code ):
                fish = fish-10
                text = "YAY " + str(fish)
                await get_tree().create_timer(2).timeout
            else:
                text = "NO " + str(fish)
                fish = fish + 10
                await get_tree().create_timer(2).timeout
    text = "fish dead"

Upvotes: 2

Views: 125

Answers (1)

Theraot
Theraot

Reputation: 40295

Your code looks like this if we ignore the inner details:

func _input(event):
    while fish > 0 && fish < 101:
        if(event is InputEventKey):
            # OTHER STUFF HERE
            pass

    text = "fish dead"

Which means that when it gets an event that is not an InputEventKey (that could be a joypad event, a mouse event, a touch event, even a MIDI input event), it will loop forever.


Furthermore, Godot will call _input for each one of them, and each one of them will result in this running this loop. Which means that even if you handle every possible kind of input, you are likely to end with multiple concurrent (but not parallel) executions of this method.


I must advice to remove the while loop and then fix the behavior. Which probably means using a conditional.

Convert the while into an if, and put the code that is outside it inside the else:

func _input(event):
    if fish > 0 && fish < 101:
        if(event is InputEventKey):
            cletter = letters.pick_random()
            text = cletter
            code = OS.find_keycode_from_string(cletter)
            if(event.keycode == code ):
                fish = fish-10
                text = "YAY " + str(fish)
            else:
                text = "NO " + str(fish)
                fish = fish + 10
    else:
        text = "fish dead"

I would write it as follows:

func _input(event:InputEvent) -> void:
    var key_event := event as InputEventKey
    if key_event == null:
        return

    if fish > 0 && fish < 101:
        cletter = letters.pick_random()
        text = cletter
        code = OS.find_keycode_from_string(cletter)
        if (key_event.keycode == code):
            fish = fish-10
            text = "YAY " + str(fish)
        else:
            text = "NO " + str(fish)
            fish = fish + 10
    else:
        text = "fish dead"

Upvotes: 3

Related Questions