0 votes

I'm a little confused about how to get contents from a PackedScene. I have a script that fires projectile weapons:

export (PackedScene) var Bullet
export (float) var gun_cooldown

var velocity = Vector2()
var can_shoot = true

func _ready():
    $GunTimer.wait_time = gun_cooldown

Currently the rate of fire is determined by gun_cooldown, the script attached to the scene assigned to Bullet has export (float) var rate_of_firewhich I'd like to use instead.

No idea how I go about accessing this, I'm aware that PackedScene comes with a dictionary, but taking a look through it and I can't seem to get the value of rate_of_fire

Can anyone point me in the right direction?

asked Jun 11 in Engine by AshTeriyaki (16 points)

Think of a loaded packed scene resource as a class, it does not have anything and needs to be instanced to get an object with properties, like shown in kidscancode's answer.

1 Answer

0 votes

Have you instanced the PackedScene? That will create a new object on which you can access the script defined properties:

var b = Bullet.instance() 
add_child(b)
print(b.rate_of_fire)
answered Jun 11 by kidscancode (5,720 points)

Thanks for this, I was missing add_child(var) as just adding intstance() just continued to error. Much appreciated!

On a side note, love your videos, please keep up the sterling work, you've helped me an awful lot recently. If you've got a patreon, let me know and I'll get on board :)

Yes, add_child() is an important step, as the new object needs to be added to the scene tree

Thanks for the kind words! The link is: https://patreon.com/kidscancode

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.