Value of scene var from PackedScene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AshTeriyaki

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?

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.

eons | 2018-06-12 02:40

:bust_in_silhouette: Reply From: kidscancode

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

var b = Bullet.instance() 

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 :slight_smile:

AshTeriyaki | 2018-06-12 14:08

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: Chris Bradfield | creating Coding and Game Dev lessons for kids of all ages | Patreon

kidscancode | 2018-06-12 14:35