0 votes

So, when changing an instanced scene's property cause all previous instanced scenes instead of the latest one.
GIF Here

Here's the simplified instancing code I use

var my_scene = preload("res://myScene")
m = my_scene.instance()
m.position = position
add_child(m)
Godot version Godot 3.3
in Engine by (74 points)

Hi,
i think you have to provide the full code. Where do you preload the scene? Usualy you will do this only once on startup, then use the preloaded scene later to instanciate.

Also i'm quit sure that you have to first add it as a child, wait for it to be "ready" and then you can change the position property.

export(PackedScene) var Projectile = preload("res://scripts/projectile/Projectile.tscn")

func shoot() -> void:
    center_pivot.look_at(get_global_mouse_position())
    if Input.is_action_pressed("player_shoot") and $ShootDelay.is_stopped():

        var p = Projectile.instance()
        owner.get_node("Projectiles").add_child(p)

        physical_property.move_direction = pivot.global_rotation
        physical_property.rotation = pivot.global_rotation

        p.init(pivot.global_position, physical_property, property)
        $ShootDelay.start()

Here's the code
The init function is a custom function.

I dont see any problem here. How do you move your projectiles?
It looks like you are moving the projectile container instead of the individual
projectiles.

I don't know. I just guessing that somehow the init function is setting every instance instead of one.
The projectile is moving using the data in the init function.
Also, doesn't use the init function still getting the same result.

So, its not your code and you dont know where the projectiles are moved?

I think th problem has nothing to do with the instancing but with the movement
code instead.

func move(delta) -> void:
    var x = cos(physical_property.move_direction)
    var y = sin(physical_property.move_direction)
    var direction: Vector2 = Vector2(x, y)
    var velocity: Vector2 = direction * physical_property.move_speed

    var collider = move_and_collide(velocity * delta)

Well, this is how its move.

func init(pos: Vector2, projectile_physical_property, projectile_property) -> void:
    position = pos
    var start_pos = pos
    physical_property = projectile_physical_property
    property = projectile_property

Here's init function.

what is "physical_property" comming from?

I think you share this object in every projectile (as reference). So if you change this values every bullet changes its direction.

ahh, that's probably why. The dictionary is referencing instead of getting value.

How to fix this now?

Stop using a dictionary probably a no from me, there's a ton of variables inside that need to often pass around and use in the game. So, duplicate the dictionary. Thanks, again?

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.