Solarus-Games English Forum

Solarus => Development => Topic started by: Zefk on May 15, 2017, 06:07:00 AM

Title: [Solved]Array in function?
Post by: Zefk on May 15, 2017, 06:07:00 AM
I get the following error:
0] Error: Failed to load script 'scripts/lib/zefk_entity_lib.lua': [string "scripts/lib/zefk_entity_lib.lua"]:2093: '(' expected near '['

when trying this:

Code ( lua) Select
  function sprites[index]:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end


The following function will only work if I convert my array to a normal variable.

Code ( lua) Select
    sprites2 = sprites[2]
  function sprites2:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end


Is there a way around this because I'd like to on_animation_finish() without having a limit set. I do not want to do something like this:

Code ( lua) Select
   
sprites2 = sprites[2]
  function sprites2:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end

sprites1 = sprites[1]
  function sprites1:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end

sprites3 = sprites[3]
  function sprites3:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end

sprites4 = sprites[4]
  function sprites4:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end

sprites5 = sprites[5]
  function sprites5:on_animation_finished()
    sprites[index]:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end
Title: [Solved]Array in function?
Post by: Zefk on May 15, 2017, 06:20:41 AM
Solved it.

Code ( lua) Select
  local sprites = sprites[index]

  function sprites:on_animation_finished()
    sprites:set_animation(end_anime)
    sol.audio.play_sound(end_sound)
  end