Godot Version
4.8.dev5
Question
I wrote my code to where dice are rolled that the resulting value adds that number of shots that the orb can shoot at incoming mobs. However, that count keeps going negative for some reason. What I am trying to achieve is for the orb to constantly “scan” for mobs within its range and fire shots only if there are shots available. The dice rolls to add to the shot count automatically.
The screenshot below shows how my nodes are arraigned:
Below is the code of the Line2D node:
extends Line2D
var firing_status: bool = false
var shot_duration: float = 0.05
var shot_delay: float = 0.3
@onready var shot_sound: AudioStreamPlayer = $"../../../Shot_sound"
@onready var balance: Label = $"../../../Main_CanvasLayer/VBoxContainer/HBoxContainer/Balance"
func _process(_delta: float):
#Updates the balance in the label
balance.text = str(Globals.balance)
shooting_mob()
func _on_orb_area_shape_entered(_area_rid: RID, area: Area2D, _area_shape_index: int, _local_shape_index: int):
var mob = area.get_parent()
if mob.has_method("when_attacked"):
Globals.mobs_in_range.append(mob)
func shooting_mob():
for mob in Globals.mobs_in_range:
if Globals.shoot_counter > 0:
var targeted_mob = Globals.mobs_in_range[0]
add_point(Vector2(0,0))
add_point(to_local(targeted_mob.position))
await get_tree().create_timer(shot_duration).timeout
clear_points()
await get_tree().create_timer(shot_delay).timeout
Globals.shoot_counter -= 1
print(str(Globals.shoot_counter) + " shots left")
targeted_mob.when_attacked()
Globals.mobs_in_range.erase(targeted_mob)
shot_sound.play()
The dice are made in a separate scene. Below is the screenshot of how I have my dice scene’s nodes arraigned:
Below is the code for the hovering dice scene:
extends Control
@onready var die_face: TextureRect = $Die
@onready var roll_timer: Timer = $Roll_Timer
@export var die_texture_array: Array[AtlasTexture]
var index_chooser: int
var shoot_auth: bool = false
var index_max_face: int
var material_multiplier: int = 1
var num_of_shots: Array = [
1, 2,
1, 2, 3,
1, 2, 3, 4,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5, 6
]
var timer_started: bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
die_face.set_texture(die_texture_array[Globals.index_min_face])
match Globals.index_min_face:
#Index of max faces of bone dice
0: index_max_face = 1
2: index_max_face = 4
5: index_max_face = 8
9: index_max_face = 13
14: index_max_face = 19
#Index of max faces of gold dice
20: index_max_face = 21
22: index_max_face = 24
25: index_max_face = 28
29: index_max_face = 33
34: index_max_face = 39
#Index of max faces of diamond dice
40: index_max_face = 41
42: index_max_face = 44
45: index_max_face = 48
49: index_max_face = 53
54: index_max_face = 59
func _process(_delta: float):
if timer_started == false and Globals.shoot_counter < 1:
roll_timer.start()
timer_started = true
if roll_timer.time_left > 0:
index_chooser = randi_range(Globals.index_min_face, index_max_face)
die_face.set_texture(die_texture_array[index_chooser])
func _on_roll_timer_timeout():
if index_chooser >= 0 and index_chooser <= 19:
material_multiplier = 1
if index_chooser >= 20 and index_chooser <= 39:
material_multiplier = 5
if index_chooser >= 40 and index_chooser <= 59:
material_multiplier = 10
Globals.shoot_counter += (num_of_shots[index_chooser] * material_multiplier)
print(str((num_of_shots[index_chooser] * material_multiplier)) + " shots added")
timer_started = false
Below shows the output. It would explain why the orb isn’t able to shoot at the mobs, but I have no idea why the number of shots go into the negative.







