+1 vote

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))
can_fire = false
can_fire = true

func _on_Timer_timeout():
var rngbow = rng.randi_range(1, 6)
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))
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.

Godot version v3.5.1
in Engine by (35 points)

Which variable is it?

Sorry, I move my comment to an answer.

3 Answers

0 votes

Declare the variable you need as top-level variable.

by (897 points)
0 votes

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)
by (90 points)
0 votes

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 :

by (95 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.