Saveth
Saveth

Reputation: 13

Is there a way to play an animation that one has exported from another scene?

I am currently working on an enemy spawner for a 2D arena wave spawner game in Godot.

I have an export(resource) variable that allows me to insert a spawning animation into the scene.

My question is: How do I play said animation?

I have created comments in my code that explains how it is set up. The two possible ways I think it could be solved is either by

  1. Inserting a function where I commented it, that takes the previously determined position value and playing that animation at those coordinates. However I do not know how to play the animation as an export(resource) var.
  2. Play the animation not as a function using the position value... but its still the same question.

Thanks!

extends Node2D

export(NodePath) var node_path

const WIDTH = 254
const HEIGHT = 126
export(Resource) var ENEMY
export(Resource) var SPAWNANIMATION

var spawnArea = Rect2()
var delta = 3
var offset = 0.5

#Creates the spawnArea and randomizes positions for the enemy to spawn
func _ready():
    randomize()
    spawnArea = Rect2(0, 0, WIDTH, HEIGHT)
    setnextspawn()

#Spawns an Enemy at said Random position
func spawnEnemy():
    var position = Vector2(randi()%WIDTH, randi()%HEIGHT)
    var enemy = ENEMY.instance()
    enemy.position = position #Determined position
    #EnemySpawningAnim(position)
    get_node(node_path).add_child(enemy)
    return position

#Spawn Timer for in between each enemy spawn
func setnextspawn():
    var nextTime = delta + (randf()-0.5) * 2 * offset
    $Timer.wait_time = nextTime
    $Timer.start()

func _on_Timer_timeout():
    spawnEnemy()
    setnextspawn()

#Function that takes a position to play the animation at
func EnemySpawningAnim(position):
    pass

Upvotes: 1

Views: 366

Answers (1)

hola
hola

Reputation: 3500

You'll need an AnimationPlayer node that you add_animation() resource when _ready(). Here's a code example:

func _ready():
    $AnimationPlayer.add_animation('spawn', SPAWNANIMATION)

func EnemySpawningAnim(position):
    # ... logic to handle position
    $AnimationPlayer.play('spawn')

Upvotes: 0

Related Questions