Accessing a variable inside a function.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ItzKillerTTV

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.

Which variable is it?

SteveSmith | 2022-10-04 06:18

Sorry, I move my comment to an answer.

Starfighter | 2022-10-05 18:53

:bust_in_silhouette: Reply From: SteveSmith

Declare the variable you need as top-level variable.

:bust_in_silhouette: Reply From: Aeris130

If I understand this correctly you want this code:

var rngbow = rng.randi_range(1, 6)

To use different ranges of values (set externally), not just 1-6?

extends Node2D

export (int) var rng = RandomNumberGenerator.new()

# Can be accessed from outside the node:
var rng_from = 1
var rng_to = 6

func _on_Timer_timeout():
  var rngbow = rng.randi_range(rng_from, rng_to)
:bust_in_silhouette: Reply From: Starfighter

You can use a singleton file to have access to any var which is declared in this file, from any scene.

Have a look here :