Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | HarryCourt |
Hello everyone! I’m having a small issue with my code. I want to start a timer ($ReelTimer
) when another timer stops. However, when the other timer stops and I call a true statement to run the function, it runs, but the timer does not start, and I do not know why.
Here is the code. (Some parts may have been removed so it’s shorter):
extends KinematicBody2D
const moveSpeed = 150.0
var timesToTap = 10
var hasThrownLine = false
var caughtFish = false
var randomCatchTimer = rand_range(5, 30)
var randomFishGenerator = randi()%4+1
func _ready():
randomize()
func _input(event):
if Input.is_action_just_pressed("catch") && !hasThrownLine:
ThrowLine()
func _process(delta):
if caughtFish:
CaughtFish()
elif !caughtFish:
return null
func FishEncounterTimeout():
caughtFish = true
func PickRandomFish():
randomFishGenerator = randi()%3+1
print("From PickRandomFish(), it generated " + str(randomFishGenerator))
match randomFishGenerator:
1:
global.goldfishAmount += 1
2:
global.salmonAmount += 1
3:
global.garfishAmount += 1
func CaughtFish():
$FishEncounterTimer.stop()
$GUI/TappingText.show()
$PlayerSprite.set_animation("reeling")
$ReelTimer.set_wait_time(4)
$ReelTimer.start()
if Input.is_action_just_pressed("catch"):
timesToTap -= 1
print("Tapped the catch button upon catching fish. Taps left: " + str(timesToTap))
if timesToTap <= 0 && !$ReelTimer.is_stopped():
PickRandomFish()
timesToTap = 10
$GUI/TappingText.hide()
$Bait.hide()
caughtFish = false
hasThrownLine = false
if $ReelTimer.is_stopped():
print("Can no longer reel.")
timesToTap = 10
$GUI/TappingText.hide()
$Bait.hide()
caughtFish = false
hasThrownLine = false
Hi,
How do you checke timer did not start? Could it be that the $ReelTimer.is_stopped()
is called inside if Input.is_action_just_pressed("catch"):
, so you won’t enter the if $ReelTimer.is_stopped():
unless you just pressed “catch”? If i understand correctly your code, i would instead create a function on_ReelTimer_timeout()
and connect $ReelTimer.timeout()
to it. If this does not help, would you please explaina little more how you expect the game to work?
p7f | 2018-11-26 11:47
An observation about your code.
if Input.is_action_just_pressed(“catch”):
This line will only be true on the frame the “catch” action is pressed. One frame early or one frame late and it won’t reach the block. Is that your intention? In your tests are you calling this function every frame?
iron_weasel | 2018-11-26 23:03