different object instances from one scene

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

hello
Im pretty new in Godot, making my first 2D game.
I have created a scene with a bad guy that can be left or right oriented, standing or laying down.
So it is 4 options of his initial position and animation.
Now how can I put 4 instances of this bad guy in my main scene, each instance with different initial position and animation?
Do I need to change bad gyu scene into 4 separated scenes? Or can I pass somehow parameter from main scene into bad guy scene to set initial parameters?

best regards
Jan

:bust_in_silhouette: Reply From: DodoIta

I don’t think it would be necessary to make 4 scenes with so little differences.
You could just make one, then assign different positions and animations with little code from the parent scene.
You could also use the inspector if you don’t wanna write code.

:bust_in_silhouette: Reply From: Squatnet

I would have bad guy scene have a little script, give the script a function, I’ll call it setup() and then just pass info to script… so you set everything up before you even add the bad guy to the scene. then as for your bad guy, he should just be a sprite with 4 panels. one state in each. with animation H frames and V frames set accordingly.

#parent scene
extends Node2D
var intialState = 0 # if there are 4 we use 0,1,2,3
var badGuy = preload(“res:// Scenes/BadGuy.tscn”) #get our badguy scene
func _ready():
var b = badGuy.instance() # instance the badguy
b.setup(initialState) # setup badguy with var initialState
get_node(“YourNodeName”).add_child(s) # add to the scene

#bad guy
extends Node2D
func _ready():
pass # just pass here,
func setup(val): # val is initialState from parent
set_frame(val) # sets the animation frame of a sprite node

with a little more code you could make only a 2-panel sprite. and use the set_flip_h(bool) method on it

Squatnet | 2018-04-05 13:55