I’m working on a game which I plan to be an intentionally overkill version of the classic catch game with left-right inputs and falling objects you have to catch to increase your score. The balls are gonna spawn at one of 12 random points off screen, which are all in an array. When I press the button, though, it gives me a <null> instead of a point. When I pass just one of the points, ball_spawn_6 for example, instead of the current_ball_spawn variable, it gives me a position no problem, so I know my syntax is correct. How do I go about this?
PS - Regular print() debugging’s out the window because I’m on the web editor, so I just put it in a label.
extends Node2D
@onready var label: Label = $CanvasLayer/Label
@onready var ball_spawn_1 = $BallSpawns/BallSpawn.position
@onready var ball_spawn_2 = $BallSpawns/BallSpawn2.position
@onready var ball_spawn_3 = $BallSpawns/BallSpawn3.position
@onready var ball_spawn_4 = $BallSpawns/BallSpawn4.position
@onready var ball_spawn_5 = $BallSpawns/BallSpawn5.position
@onready var ball_spawn_6 = $BallSpawns/BallSpawn6.position
@onready var ball_spawn_7 = $BallSpawns/BallSpawn7.position
@onready var ball_spawn_8 = $BallSpawns/BallSpawn8.position
@onready var ball_spawn_9 = $BallSpawns/BallSpawn9.position
@onready var ball_spawn_10 = $BallSpawns/BallSpawn10.position
@onready var ball_spawn_11 = $BallSpawns/BallSpawn11.position
@onready var ball_spawn_12: Marker2D = $BallSpawns/BallSpawn12.position
var ball_spawns: Array = [
ball_spawn_1,
ball_spawn_2,
ball_spawn_3,
ball_spawn_4,
ball_spawn_5,
ball_spawn_6,
ball_spawn_7,
ball_spawn_8,
ball_spawn_9,
ball_spawn_10,
ball_spawn_11,
ball_spawn_12
]
func _on_debug_button_pressed() -> void:
var current_ball_spawn = ball_spawns.pick_random()
label.text = current_ball_spawn
@baba is correct, your other variables are only set @onready, but this array that depends on them is not @onready so the array will be assigned before them- with a bunch of null values.
No, you declared it as a class property but you declared it again inside a function. This creates a new local variable that uses the same name but overrides the class property in the scope of this function (aka shadowing - the engine will even warn you about it). Lose the var keyword when you assign in _ready()
These are two different vars, this is allowed and called “shadowing”. while you do assign _ready’s version of ball_spawns that variable is deleted once the function is complete, and your original ball_spawns is unaffected. You likely get a script warning for shadowing this variable.
Here’s how you should do this, assuming you want to get all children of BallSpawns
var ball_spawn_positions: Array[Vector2]
func _ready() -> void:
for ball_spawn in $BallSpawns.get_children():
ball_spawn_positions.append(ball_spawn.global_position)
You should not develop software without a debugger.
In any case, initialize it using a loop as suggested in my above code example. Never type such amounts of repetitive code. If your code looks that repetitive, it’s a sure sign you’re doing it - wrong.
Btw in this line you’re trying to assign a Vector2 value to a typed variable whose type is Marker2D:
@onready var ball_spawn_12: Marker2D = $BallSpawns/BallSpawn12.position
This will cause an error and probably stop the execution of the script.
Logs should show up in your web browser’s console, typically F12 will open this but Godot may also consume that input, so opening before hand may be best.
You can’t assign a String (label.text) to a Vector2 without conversion, you could use str(current_ball_spawn) or "%v" % current_ball_spawn
Yeah, I also found other errors like this, such as;
@onready var label: Label = $CanvasLayer/Label.position
and the printing the position without casting to a string.
As for your solution to my repetitive code, it works so, at least for now, i’m keeping it, mostly because it’s easier for me to read. I appreciate all your help.