#Solved | Newly added scenes (with on_timer_timeout) interaction

29 messages · Page 1 of 1 (latest)

celest dune
#

Hello folks,
I am trying to learn the Gadot 4+ also new dev here.
i was making a small space game where player just has to skip or shoot the meteors.
so i created a timer auto-started it and on_timer_timeout i add new meteor scene to the parent node of the scene
but the meteors do not move. Meteor is a Area2D node hence i have been updating position in _process func Please help. Here is the code


extends Area2D

@onready var player = $"../../Player"
@export var meteor_speed :=400

func _ready():
    var width = get_viewport().get_visible_rect().size[0];
    var rng = RandomNumberGenerator.new();
    var random_x = rng.randi_range(0,width);
    var random_y = rng.randi_range(-150,-50);
    position = Vector2(random_x,random_y)
    
func _process(delta):
    position += Vector2(0,1) * meteor_speed * delta


func _on_body_entered(body):
    if body.name == 'Player':
        print(body)

and here is code where i add new meteor


extends Node2D

@onready var meteors = $Meteors

var meteorScene:PackedScene = load('res://scenes/meteor.tscn')
func _ready():
    pass # Replace with function body.

func _process(_delta):
    pass

func _on_timer_timeout():
    var new_meteor = meteorScene.instantiate();
    meteors.add_child(new_meteor)

Please help me on what i am doing wrong, thank you

#

Both Player and Meteor are on same level

ripe yacht
#

Can you put a print in the process. Lets see if its running. Print out the position as well

celest dune
ripe yacht
#

Kool and its not moving?

#

Oh I think i see the problem.

#

Your meteors parent node is a regular node. Plain nodes have no transform. Make it a Node2D so that its children know how to handle positions

celest dune
sage orbit
#

that script is on the Meteor scene correct? not just on the Meteor instance in main?

#

ie: you aren't just adding nodes that don't have scripts

#

because we're only seeing 1 print - you add more nodes, but they don't indicate a position - much less the same position

#

it's just the original meteor printing that it's moving offscreen

#

ie: if this were on every meteor

#

your other meteor at the top left of the screen would not be printing 165

#

and this wouldn't be only printing a single meteor's consistent progression

celest dune
#

oh yeah i get it. scrpit is not attached to new scenes this is what you are saying right?

sage orbit
#

correct - it seems like you don't have this script on the saved Scene, it's just on the instance of the scene in main

#

so you instance the scene - it has no script, it goes nowhere

celest dune
#

okkk, do you know where do i look for this fix?

sage orbit
#

open that scene

celest dune
#

yeah

sage orbit
#

attach that script to the root node of that scene

#

and save the scene

celest dune
#

ok i got it now i'll try it thank you very much

celest dune
# sage orbit attach that script to the root node of that scene

Hey man thanks for advice. I changed the code just set_script to the scene at time of instanciation

extends Node2D

var meteor_scene:PackedScene = load("res://scenes/meteor.tscn")
var meteor_script:Script = load("res://scenes/meteor.gd");

@onready var meteors = $Meteors

func _on_timer_timeout():
    var meteor_instance = meteor_scene.instantiate();
    meteor_instance.set_script(meteor_script);
    meteors.add_child(meteor_instance);

As you see on second to last line timer func

#

Solved | Newly added scenes (with on_timer_timeout) interaction

sage orbit
#

that was not the itention of the suggestion - there's no need to set_script() if you make the scene with the script already attached

if the scene as saved in the .tscn already has the script - when you instance it, it will have the script

celest dune
#

yeah i thought so, i'll definately keep in mind next time...