Author Topic: [Solved]Array in function?  (Read 234 times)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
[Solved]Array in function?
« on: May 15, 2017, 06:07:00 am »
I get the following error:
Code: [Select]
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
  1.   function sprites[index]:on_animation_finished()
  2.     sprites[index]:set_animation(end_anime)
  3.     sol.audio.play_sound(end_sound)
  4.   end

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

Code: Lua
  1.     sprites2 = sprites[2]
  2.   function sprites2:on_animation_finished()
  3.     sprites[index]:set_animation(end_anime)
  4.     sol.audio.play_sound(end_sound)
  5.   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
  1.    
  2.  sprites2 = sprites[2]
  3.   function sprites2:on_animation_finished()
  4.     sprites[index]:set_animation(end_anime)
  5.     sol.audio.play_sound(end_sound)
  6.   end
  7.  
  8.  sprites1 = sprites[1]
  9.   function sprites1:on_animation_finished()
  10.     sprites[index]:set_animation(end_anime)
  11.     sol.audio.play_sound(end_sound)
  12.   end
  13.  
  14.  sprites3 = sprites[3]
  15.   function sprites3:on_animation_finished()
  16.     sprites[index]:set_animation(end_anime)
  17.     sol.audio.play_sound(end_sound)
  18.   end
  19.  
  20.  sprites4 = sprites[4]
  21.   function sprites4:on_animation_finished()
  22.     sprites[index]:set_animation(end_anime)
  23.     sol.audio.play_sound(end_sound)
  24.   end
  25.  
  26.  sprites5 = sprites[5]
  27.   function sprites5:on_animation_finished()
  28.     sprites[index]:set_animation(end_anime)
  29.     sol.audio.play_sound(end_sound)
  30.   end
  31.  
« Last Edit: May 15, 2017, 05:29:56 pm by Zefk »

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
[Solved]Array in function?
« Reply #1 on: May 15, 2017, 06:20:41 am »
Solved it.

Code: Lua
  1.   local sprites = sprites[index]
  2.  
  3.   function sprites:on_animation_finished()
  4.     sprites:set_animation(end_anime)
  5.     sol.audio.play_sound(end_sound)
  6.   end
« Last Edit: May 15, 2017, 05:30:12 pm by Zefk »