Changing parameters of an instance created by code.

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

OK, people, I’m starting to learn Godot and trying to create a basic Asteroids game… I have a scene for the Ship and another scene for the basic shots, so whenever player shoots, the Ship script does this, to create a shot that uses the value of the ship pointing angle:

var PlayerBasicShotInstance = preload("res://PlayerBasicShot.tscn").instance()
if randi() % 10 == 0:
	PlayerBasicShotInstance.ShotAngle = shipangle

And inside the PlayerBasicShot scene:

extends Node2D

var ShotDirection = Vector2()
export (int) var ShotAngle = 0

func _ready():
	ShotDirection.x = cos(deg2rad(ShotAngle))
	ShotDirection.y = -sin(deg2rad(ShotAngle))

Problem is that although I try to pass the value of ShotAngle to the PlayerBasicShot, the ShotAngle variable is always zero. How can I inside a script alter the value of an instance of another script created by code?

Thanks in advance.

:bust_in_silhouette: Reply From: kidscancode

The problem is you’re only creating one instance, at load time. Instead, create a separate instance whenever you need:

var PlayerBasicShot = preload("res://PlayerBasicShot.tscn")

Then in your shooting code:

if randi() % 10 == 0:
    var shot_instance = PlayerBasicShot.instance()
    shot_instance.ShotAngle = shipangle

See here in the docs for an example:

No success. Same problem.

I added your first line in func _ready() and second code at _process().

Just in case here’s the PlayerBasicShot code…

extends Node2D

var ShotDirection = Vector2()
var ShotLifeSpan = 0
var ShotAngle = 45

func _ready():
	ShotDirection.x = cos(deg2rad(ShotAngle))
	ShotDirection.y = -sin(deg2rad(ShotAngle))

func _process(delta):
	position.x += ShotDirection.x*delta*4500
	position.y += ShotDirection.y*delta*4500
	ShotLifeSpan += 1 * delta
	if ShotLifeSpan > 2:

Leandro | 2018-11-23 20:51

This is a whole separate problem. First, you’re not setting the instance’s position when you create it, so where is it supposed to start?

Second, that code in _ready() is not only not necessary, but it’s not going to do anything because you will wind up setting the angle after the _ready() code runs.

I say “not necessary” because when you’re working with vectors, you don’t need to muck around with trig functions or separate x/y variables. Vectors contain directional information already. So lose sin and cos code. Instead, you just need a velocity vector which describes the direction and magnitude of your desired movement. This way your _process() function just becomes:

position += velocity * delta

Second, did you look at the example I linked? The code there shows exactly what I’m talking about. The usual practice in situations like this (spawning instances via code) is to give the instance an initializer function (start() in the linked example), and pass to it all the parameters it needs to know, such as position and direction.

kidscancode | 2018-11-23 21:04

Sorry about the delay and not reading the code example you linked before (I was about to leave the office and temporarily without Internet at home, so I wanted to do it quickly). I finally got it to work, many thanks!

Godot is so full of already made stuff it’ll take sometime for me to learn to do things in the easy way. Really loving it. :slight_smile:

Leandro | 2018-11-24 18:55