|
|
|
|
Reply From: |
Omar13 |
you are adding the instance and then changing his position
you have to create the instance, change position, and then add it
var wallLoader = preload("res://scenes/wall.tscn")
# I'll make a list of positions, this will be hepfull for less code
var positions = [Vector2(24, 120), Vector2(104, 120), Vector2(24, 360), Vector2(104, 360)]
....
func wallSpawner() -> void:
for i in Range(positions.size()):
# positions.size() = 4 -> 'i' is going to iterate from 0 to 3
var wallInstance = wallLoader.instance()
wallInstance.position = positions[i]
# positions[0] = Vector2(24, 120)
# positions[1] = Vector2(104, 120) and so
add_child(wallInstance)
If you get a bit lost with the array and the “for” I recommend you to check those concepts (there are universal on programming)
It worked nicely! I am familiar with arrays and loops, I studied a little bit of JavaScript a while back. Tho, I never got far enough to be capable to apply it in code. But I know their concepts, hehe. Thanks dude!
andreluizgollo | 2020-05-04 01:03
Messing around with the code you provided, actually trying to understand everything in it, I’m left with 2 doubts… why did you declare the “var wallInstance = wallLoader.instance()” again inside de function, if it was already declared in the beginning of the code? I tried removing it and the function stopped working hahahaha. Plus, what the “-> void” does?
andreluizgollo | 2020-05-04 01:09
“void” is the expected return for the func (in this case… nothing). I like to work with static typing, is a bit slower at start but saves a lot of bug fixing time.
I declared the var only inside wallspawner (didnt declare at top of all) 'cause I don’t want to use memory until that very moment inside wallspawner (a function that may or may not be called).
If you want to call only at the start than do it, just be sure is an onready var. And inside wallspawner only call whatever you want to do with that var
Omar13 | 2020-05-04 01:27