Hello everyone. I made a timer that generates a random number between 1 and 6 when it runs off. I want to make the players bow to change depending on the number the rng rolled. I already implemented basic stuff like "when rolling a 4 make the projectiles fast" but now I want to implement stuff like arrows bouncing instead of dissapearing. The rng variable (at the beggining before any function) generates a number and then another variable INSIDE the timer function limits the selection. I thought I knew how to do this; I tried to change the shooting code to add exceptions for specific generated numbers, but I quickly realized that the variable that I need (the one that limits the number selection) is inside a function so I can't call it. Does anyone know how to fix this? Here's my code (sorry about the format):

extends Node2D

var arrow = preload("res://Arrowfinal.tscn")
var rubberarrow = preload("res://RuberArrow.tscn")
export var arrow_speed = 400
export var fire_rate = 0.6
var can_fire = true
export (int) var rng = RandomNumberGenerator.new()

func _process(delta: float) -> void:
	look_at(get_global_mouse_position())

	
if Input.is_action_just_pressed("ui_attack") and is_visible_in_tree() and can_fire and rng:
		var arrow_instance = arrow.instance()
		arrow_instance.position = $Arrowpoint.get_global_position()
		arrow_instance.rotation_degrees = rotation_degrees
		arrow_instance.apply_impulse(Vector2().normalized(), Vector2(arrow_speed, 0).rotated(rotation))
		get_tree().get_root().add_child(arrow_instance)
		can_fire = false
		yield(get_tree().create_timer(fire_rate),"timeout")
		can_fire = true


func _on_Timer_timeout():
	var rngbow = rng.randi_range(1, 6)
		print(rngbow)
		if rngbow == 2:
			arrow_speed = 100
			fire_rate = 0.6
		if rngbow == 4:
			arrow_speed = 400
			fire_rate = 0.6
		if rngbow == 5:
			arrow_speed = 500
			fire_rate = 0.2
		if rngbow == 6:
			arrow_speed = 600
			fire_rate = 0.01
if Input.is_action_pressed("ui_attack") and is_visible_in_tree() and can_fire:
				var rubberarrow_instance = rubberarrow.instance()
				rubberarrow_instance.position = position 
				rubberarrow_instance.rotation_degrees = -rotation_degrees
				rubberarrow_instance.apply_impulse(Vector2(),Vector2(arrow_speed, 0).rotated(rotation))
				get_tree().get_root().add_child(rubberarrow_instance)
				can_fire = false
				yield(get_tree().create_timer(fire_rate), "timeout")
		               can_fire = true

If you need me to provide more information, just tell me. Thanks in advance.

Is rngbow the variable? If you want to access it outside of the function _on_Timer_timeout(), then you have to declare it outside of that function.

    You will have to make a reference to it.

    eg:

    var new_timer = get_tree.create_timer()
    yield(new_timer ,"timeout")

    Then you can do something like
    new_timer.my_variable = 9.9

      DaveTheCoder Thanks for answering. I know that if I declare it outside of the function I'll be able to access it. The thing is that I need this variable to be declared inside because it has to select a number only when the timer runs out. Is there any other way to do this?

        Riplea First of all, thanks for answering. Could you please specify? What do you exactly mean by this. I'm sorry, I'm completely new to this and I am kinda clueless haha.

          ItzKillerTTV first off what is the name of the variable you are trying to get and what is the name of the function that needs it?

          First thing off the bat that I notice is that your indentation is all over the place. You should keep it consistent.

          ItzKillerTTV The thing is that I need this variable to be declared inside because it has to select a number only when the timer runs out.

          If you declare the variable outside the function, the variable has file-scope. That means you can use it anywhere in that file (class).
          Outside the function:
          var rngbow
          Inside the function:
          rngbow = rng.randi_range(1, 6)

            DaveTheCoder Ok thanks that was unexpectedly easy. Do you happen to know how to access this variable in another script? I tried using singletons but I'm not surte why it doesn't work, the object is null.

            It depends on where the nodes are in the scene tree. Generally, to access a property or method in another node's script, you specify the path to the node. There are some examples here:
            https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-get-node

            Godot 3.5+ has a feature called scene unique nodes that makes this simpler.
            https://docs.godotengine.org/en/stable/tutorials/scripting/scene_unique_nodes.html

            I tried using singletons but I'm not sure why it doesn't work, the object is null.

            More details are needed to identify the problem. Ideally, post a complete minimal reproduction project with a description of the specific error.