controlling an instanced scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ddarkmoto
:warning: Old Version Published before Godot 3 was released.

Hi I have a world scene and an enemy scene. The enemy scene is being instanced inside the world scene through codes. My code goes this way:

	var enemygen = enemy.instance()
    if (summon == false):
	
	 summon = true
	 summon_count = summon_count + 1

	
	 var epos = Vector2(500, 50) + get_node("spawnpoint/pipe").get_pos()
	 enemygen.set_pos(epos)
	 get_parent().add_child(enemygen)
	 en_timer.start()

What it does is basically spawning the enemy scene from the right side because that is what my enemy scene’s default direction is. Now, what I want for it to happen is for every other spawn, the enemy will move left. Is there anyway to tweak the instanced scenes movement? tried tweaking with singleton but no luck so far. Thank you in advance

How many enemies do you need to spawn and how often (time elapsed between 2 enemy spawns)?

DodoIta | 2017-03-07 15:11

:bust_in_silhouette: Reply From: avencherus

Yes. Attach a script to the enemy scene’s root node, and write your own function or functions for controlling movement. Perhaps move_left() or move_right() or something multipurpose like move(direction).

A basic example for alternating calls. Using a helper function for odd and even numbers:

func is_even(num): return num % 2 == 0 // A function for alternating.

Then call the enemy functions.

if(is_even(summon_count)): enemy.move_left()
else: enemy.move_right()

Hello aven. Thanks for the reply :). I have this code right now, and i did a print check of whether the image is flipped or not. Apparently it says that current instanced node is flipped but it doesn’t when i run it. Did i miss something? everything seems to be in order.

	if (summon == false):
	
	 summon = true
	 summon_count = summon_count + 1
	 var mod = summon_count%2
	 var enemygen = enemy.instance()
	 if (mod > 0):

	  enemygen.get_node("Sprite").set_flip_h(true)
	  print (enemygen.get_node("Sprite").is_flipped_h())

	var epos = Vector2(500, 50) + get_node("spawnpoint/pipe").get_pos()
	enemygen.set_pos(epos)
	get_parent().add_child(enemygen)

ddarkmoto | 2017-03-07 16:44

I can’t reproduce it, so you might want to test other assumptions. Like what node is it selecting?

var node = enemygen.get_node("Sprite")
printt(node.get_name(), node.get_type())

avencherus | 2017-03-08 08:25