Rocket instantiation not working properly

The rockets instantiated by pressing space do not move, they only appear on the screen. In the node tree, they are correctly sitting under the player node. Any other rocket that I just add to the game scene act naturally flowing from left to right. I checked the gitlab code, even copy pasted the gd scripts over mine to be sure it’s not a typo. Godot version is 4.2.1

strange one,

could i see a copy of the player script please.
and a screenshot of the rocket scene.

theres not two rocket scenes in the filesystem is there? just wondering if ones got a speed and the other doesnt.

if not, if you can, would be great if you are able to zip the project folder up and pop it online somewhere and i can download for a look.

if you dont have any way of getting it online somewhere, i can send you a link to upload the project.

Darren

Here goes:

extends CharacterBody2D

var speed = 400
var rocket_scene

func _ready():
	rocket_scene = load("res://scenes/rocket.tscn")

func _process(delta):
	if Input.is_action_just_pressed("shoot"):
		shoot()
		
func _physics_process(delta):
	velocity = Vector2(0, 0)
	
	if Input.is_action_pressed("move_right"):
		velocity.x = speed
	if Input.is_action_pressed("move_left"):
		velocity.x = -speed
	if Input.is_action_pressed("move_up"):
		velocity.y = -speed
	if Input.is_action_pressed("move_down"):
		velocity.y = speed
	
	move_and_slide()
	
	var screen_size = get_viewport_rect().size
	global_position = global_position.clamp(Vector2(0,0), screen_size)

func shoot():
	var rocket_instance = rocket_scene.instantiate()
	add_child(rocket_instance)
	rocket_instance.global_position.x += 50

extends Area2D

@export var speed = 300

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	global_position.x += speed*delta

Wetransfer of the zip as well: https://we.tl/t-R1uoCUL85o

sound,

cheers for sending that.

think ive found the issue.

if you look at your rocket scene itself thats saved in the filesystem. it doesnt have the rocket script attached. and thats the one that will be instantiated.

its just the one thats in the game just now that has the script.

if you go into your rocket scene, then attach the rocket.gd script to it, save and retest, it should work ok.

hope that gets you back up and running

darren

Thanks a lot. It never would have crossed my mind to check that!

1 Like

No worries, I’ve done it alot myself :slight_smile:

It’s down to changing exported variables or adding a script to an instance doesn’t update the scene it was inherited from.

Easily missed, pulled alot of my hair out in the past lol

Glad it’s working now tho

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms