I'm a beginner taking a crack at making pong. I've made the base game, and now I'm looking to see what else I can add on top in an effort to learn more. Currently, there can be occassions where the ball just ricochets back and forth off the top walls with no end in sight. I thought a fun way to counteract this problem would to make a black hole spawn that would appear, make the ball disappear, reappear again, and then spit the ball out in a random direction. However, I'm encountering problems at the very first stage. When I try to get my black hole scene to instantiate, it hits a hiccup when I try to call the animation player's play method, and it gives me the error I have above in the title. Any advice on what I might be doing wrong? Here's the code for the black hole:
#"Cannot call method 'play' on a null value"
1 messages · Page 1 of 1 (latest)
extends Sprite2D
class_name Blackhole
@onready var ball = preload("res://scenes/ball.tscn")
@onready var gpu_particles_2d: GPUParticles2D = $GPUParticles2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
func _init() -> void:
visible = true
position.x = 640
position.y = 320
animation_player.play("appear")
animation_player.play("loop")
Code for the ball which I have triggering the instantiation of the black hole: extends CharacterBody2D
class_name Ball
@onready var blackhole = preload("res://scenes/blackhole.tscn")
@export var initial_ball_speed = 20
#2% speed increase after each bounce
@export var speed_multiplier = 1.02
var ball_speed = initial_ball_speed
func _ready():
start_ball()
func _physics_process(delta):
var collision = move_and_collide(velocity * ball_speed * delta)
if(collision):
velocity = velocity.bounce(collision.get_normal()) * speed_multiplier
if Globals.wall_bounce_count == 5:
generate_black_hole()
func start_ball():
randomize()
velocity.x = [-1,1][randi() % 2] * initial_ball_speed
velocity.y = [-1,1][randi() % 2] * initial_ball_speed
func generate_black_hole():
blackhole.instantiate()
Also how do you get discord to display code instead of just text in a message?
When you surround some words with single backticks like `this`, it will be formatted as code.
You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:
print("hello world")
If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/
null means empty variable/object, one that hasn't been assigned yet
The _init() function runs immediately, @onready variables aren't ready yet.
If you move the _init code to _ready() it should work
I think I had it as that before, but I'll try again.
well no errors but nothing happened
now it's not doing anything, not even printing messages. with _init it at least was running down through the function's code until hitting the animation player error
_ready() will be called when the node this script is attached to is added to the SceneTree
If you create that blackhole at runtime
You need to instantiate its scene
And then add the instantiated object via add_child()
ok, so in the ball script I instantiated the scene, then I put it's onready var into the script. After that, in the generate_black_hole function, I added black_hole_child.instantiate() followed by add_child(black_hole_child)
Then it gave me the error Attempt to call function 'instantiate' in base 'null instance' on a null instance
it should look something like this
...
@onready var black_hole_scene= preload("res://blackhole.tscn")
...
func generate_black_hole():
var black_hole_child= black_hole_scene.instantiate()
get_tree().current_scene.add_child(black_hole_child)