![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Ph_Pheo |
Edit:
Completly changing my question because i discovered something.
In the code below, the way i’m trying to call my function several times with a for loop in the _ready function seems to be the problem.
When i do it like this, the 4 instances are taking the same position, they all have SlimePosition = 3
Printed this in the _process, the 4 first lines are the instances ID.
It seem that my SlimePosition
is jumping directly to 3 after spawning the 4 instances.
But when i’m calling the function with this
if Input.is_action_just_pressed("ui_accept"):
slime_spawning()
I can press my key 4 time and everything go as intended.
What is the for loop or the _ready function doing ?
And how can i call the function several time when launching the game ?
Here is the code of the Main scene
extends Node2D
onready var SlimeScene = load("res://Scènes/Slime.tscn")
signal SlimeNbr
var SlimeNbr = 0
func _ready():
for _number in range(4):
slime_spawning()
func slime_spawning():
var SlimeInstance = SlimeScene.instance()
add_child(SlimeInstance)
emit_signal("SlimeNbr", SlimeNbr)
SlimeNbr += 1
And here the code of the Slime scene
extends Sprite
onready var ListenMain = get_parent()
var SlimePosition = 0
var PositionSetted = false
var InstanceSpawned = 0
var PositionVectors = [Vector2(104, 64), Vector2(136, 96), Vector2(168, 64), Vector2(136, 32)]
func _ready():
ListenMain.connect("SlimeNbr", self, "position_var_setter")
PositionSetted = false
func _process(delta):
if PositionSetted == false and SlimePosition == InstanceSpawned:
position_setter()
PositionSetted = true
func position_var_setter(SlimeNbr):#Cette fonction fonctionne
SlimePosition = SlimeNbr
InstanceSpawned = SlimeNbr
func position_setter():
self.position = PositionVectors[SlimePosition]
Thanks