Play animation anywhere, anytime

Play Database Animation 1.2

What does this script do?
RMXP has a great system to create animations in the database, but it forces you to use them on a sprite /event.
This script lets you play those animations anywhere you want: in the Title screen, in the menu, in custom scenes, or just on the map on a certain spot.
It can play several animations at once, and should be compatible with anything.

Instructions:
use
Animation.new(id, x, y)

* Optional values: z, freeze. Read the instructions in the code.

code
  1. #---------------------------------------------------------------------
  2. # ** Animation
  3. #---------------------------------------------------------------------
  4. #
  5. #  V.1.2
  6. #  Made by: silverwind & ForeverZer0
  7. #    
  8. #  * This class enables playing a database animation
  9. #    in every scene, including costum ones.
  10. #
  11. #  * Instructions:
  12. #    put the following code in a script / an event's script call:
  13. #    Animation.new(id,x,y)
  14. #    where  id= the number of the animation in the database
  15. #           x= screen x
  16. #           y= screen y
  17. # * Notes:
  18. #   - If the animation shows below something else on the screen, use:
  19. #     Animation.new(id,x,y,z)
  20. #      Experiment with z's value. The default value is 9000,
  21. #      so try higher numbers.
  22. #   - freeze
  23. #     Animation.new(id,x,y,z,true)
  24. #     ^ use this code to freeze the game until the animation is done.
  25. #     useful when you want to play an animation during a scene change.
  26. #     (ie, going from the menu screen to the map screen)
  27. #---------------------------------------------------------------------
  28.  
  29.  
  30. class Animation < RPG::Sprite
  31.  
  32.   attr_accessor :frames           # frame count (from frame_max down to 0)
  33.  
  34.   def initialize(id, x, y, z = 9000, freeze=false)
  35.     viewport = Viewport.new(0, 0, 640, 480)
  36.     viewport.z = z
  37.     super(viewport)
  38.     self.x, self.y = x, y
  39.     @frames = $data_animations[id].frame_max
  40.     hit = true
  41.     self.animation($data_animations[id], hit)
  42.     Graphics.play_animation(self, freeze)
  43.   end
  44.  
  45.   def dispose
  46.     self.viewport.dispose
  47.     super
  48.   end
  49.  
  50. end
  51.  
  52. module Graphics
  53.  
  54.   class << self
  55.    
  56.     def freeze_play_animation(frames)
  57.       if @animations != nil && @animations.size > 0
  58.         for i in 0..frames : update end
  59.       end
  60.     end
  61.    
  62.     def clear_animations_on_scene_change
  63.       return if @last_scene.nil?
  64.       if @last_scene != $scene.class.name
  65.         @animations.each_index {|i|
  66.               @animations[i].dispose
  67.               }
  68.         @animations = []
  69.       end
  70.     end
  71.    
  72.     def play_animation(animation, freeze)
  73.       @animations = [] if @animations == nil
  74.       @animations.push(animation)
  75.       if freeze
  76.         freeze_play_animation(animation.frames)
  77.       end
  78.     end
  79.    
  80.     alias animation_update update
  81.     def update
  82.       if @animations != nil && @animations.size > 0
  83.         @animations.compact!
  84.         @animations.each_index {|i|
  85.           @animations[i].update
  86.           @animations[i].frames -= 1
  87.           if @animations[i].frames < 0
  88.             @animations[i] = @animations[i].dispose
  89.           end
  90.         }
  91.       end
  92.       animation_update
  93.       clear_animations_on_scene_change
  94.       @last_scene = $scene.class.name
  95.     end
  96.   end
  97.  
  98.  
  99. end
Expand to see the code.


older versions
  1. #---------------------------------------------------------------------
  2. # ** Animation
  3. #---------------------------------------------------------------------
  4. #
  5. #  V.1.1
  6. #  Made by: silverwind & ForeverZer0
  7. #    
  8. #  * This class enables playing a database animation
  9. #    in every scene, including costum ones.
  10. #
  11. #  * Instructions:
  12. #    put the following code in a script / an event's script call:
  13. #    Animation.new(id,x,y)
  14. #    where  id= the number of the animation in the database
  15. #           x= screen x
  16. #           y= screen y
  17. # * Notes:
  18. #   - If the animation shows below something else on the screen, use:
  19. #     Animation.new(id,x,y,z)
  20. #     Experiment with z's value. The default value is 9000,
  21. #     so try higher numbers.
  22. #   - 'hit' is a true/false value, related to the database animation
  23. #      I'm not sure what it does, but in case you need to change it, use:
  24. #      Animation.new(id,x,y,z,hit)
  25. #---------------------------------------------------------------------
  26.  
  27.  
  28. class Animation < RPG::Sprite
  29.  
  30.   attr_accessor :frames                      # frame count (from frame_max down to 0)
  31.  
  32.   def initialize(id, x, y, z = 9000, hit = true)
  33.     viewport = Viewport.new(0, 0, 640, 480)
  34.     viewport.z = z
  35.     super(viewport)
  36.     self.x, self.y = x, y
  37.     @frames = $data_animations[id].frame_max
  38.     self.animation($data_animations[id], hit)
  39.     Graphics.play_animation(self)
  40.   end
  41.  
  42.   def dispose
  43.     self.viewport.dispose
  44.     super
  45.   end
  46.  
  47. end
  48.  
  49. module Graphics
  50.  
  51.   class << self
  52.    
  53.     def play_animation(animation)
  54.       @animations = [] if @animations == nil
  55.       @animations.push(animation)
  56.     end
  57.    
  58.     alias animation_update update
  59.     def update
  60.       if @animations != nil && @animations.size > 0
  61.         @animations.compact!
  62.         @animations.each_index {|i|
  63.           @animations[i].update
  64.           @animations[i].frames -= 1
  65.           if @animations[i].frames < 0
  66.             @animations[i] = @animations[i].dispose
  67.           end
  68.         }
  69.       end
  70.       animation_update
  71.     end
  72.   end
  73. end
Expand to see the code.


  1.  
  2. #==============================================================================
  3. # ** Animation
  4. #------------------------------------------------------------------------------
  5. #  This class shows a database animation on the screen.
  6. #   should work outside scene_map /battle.
  7. #
  8. #  Instructions:
  9. #    put the following code in a script / an event's script call:
  10. #    Animation.new(id,x,y)
  11. #    where  id= the number of the animation in the database
  12. #           x= screen x
  13. #           y= screen y
  14. #  Notes:
  15. #   if the animation shows below something else on the screen, use:
  16. #   Animation.new(id,x,y,z)
  17. #   experiment with z's value. it's 9000 by default, so try higher numbers.
  18. #  
  19. #==============================================================================
  20. class Animation
  21.  
  22.   def initialize(anim_id, x, y, z=9000, hit=true)
  23.     view = Viewport.new(0, 0, 640, 480)
  24.     view.z = z
  25.     @sprite =  RPG::Sprite.new(view)
  26.     @sprite.x = x
  27.     @sprite.y = y
  28.     @sprite.z = z
  29.     @anim_file = $data_animations[anim_id]
  30.     run_it(x, y, hit)
  31.     @sprite.viewport.dispose
  32.     dispose
  33.   end
  34.  
  35.   def dispose()
  36.     @sprite.dispose()
  37.   end
  38.  
  39.   def run_it(x, y, hit)
  40.     @sprite.animation(@anim_file, hit)
  41.     size = @anim_file.frame_max
  42.     for i in 0...size
  43.       @sprite.update()
  44.       Graphics.update()
  45.       $game_screen.update
  46.     end
  47.   end
  48.  
  49. end
Expand to see the code.
I'm curious why you would purposefully have the rest of the game freeze during the animation's playback. Personally I think it would be better to have the game proceed normally during the animation, or at the very least allow the player to choose.
I did this script cause I needed to play an animation during Scene_Title. so I've searched scripts which let you play an animation out of map/battle, but those usually come as a part of a CMS. there was one using threads +some fancy weather system (by MCgamer), which was an overkill, imo (it also freezes the game, btw).

not freezing would be better, but it complicates things immensely.
if, for example, I update the animation in each scene's update method, or in Graphics.update, the animation will play in slow-motion or freeze completely in some scenes. I'll have to add my code into every scene- several times in longer update methods, making it incompatible with custom scenes.
another option is to re-invent the wheel, and write the RPG::Sprite.animation script from scratch, and make it replicate the animation in the database accurately (did I mention overkill?)

in short, ease of use + 100% compatibility > avoiding freeze for 1-2 seconds.

do you have a simple way to avoid freezing? I'd love to hear it.
  1. $scene.update
Expand to see the code.
ForeverZer0 wrote:
  1. $scene.update
Expand to see the code.

epic one liner
I wish it was this easy.
a quick testing made me drown in errors.
like I mentioned, some scenes (like scene_map /scene_battle) have huge update methods that freeze the animation. if the animation is done by a script call, RMXP quits (cause it takes too long). it also creates bugs if you're in scene_title or in the middle of changing scenes.

Listen, the script may not be perfect, but it WORKS. it does play the animation, and the freeze is hardly noticeable, except in scene map/ costum battle systems (where you can use RMXP's 'show battle animation' command). if the freezing bugs you, there are other scripts out there.
Hmmm. You are correct in the regard that in it takes more than simple $scene.update. Another solution is to alias the Graphics module's update method. Then it could be compatible with absolutely everything, even if there is no scene. Something like this:

  1. class Animation < RPG::Sprite
  2.  
  3.   attr_accessor :frames
  4.  
  5.   def initialize(id, x, y, z = 9000, hit = true)
  6.     viewport = Viewport.new(0, 0, 640, 480)
  7.     viewport.z = z
  8.     super(viewport)
  9.     self.x, self.y = x, y
  10.     @frames = $data_animations[id].frame_max
  11.     self.animation($data_animations[id], hit)
  12.     Graphics.play_animation(self)
  13.   end
  14.  
  15.   def dispose
  16.     self.viewport.dispose
  17.     super
  18.   end
  19.  
  20. end
  21.  
  22. module Graphics
  23.  
  24.   class << self
  25.    
  26.     def play_animation(animation)
  27.       @animations = [] if @animations == nil
  28.       @animations.push(animation)
  29.     end
  30.    
  31.     alias animation_update update
  32.     def update
  33.       if @animations != nil && @animations.size > 0
  34.         @animations.compact!
  35.         @animations.each_index {|i|
  36.           @animations[i].update
  37.           @animations[i].frames -= 1
  38.           if @animations[i].frames < 0
  39.             @animations[i] = @animations[i].dispose
  40.           end
  41.         }
  42.       end
  43.       animation_update
  44.     end
  45.   end
  46. end
Expand to see the code.


I didn't mean any insult, but in my opinion the freeze WAS noticeable, and it would be even worse for longer animations. 10 frames might not be noticed too bad, but if someone has a 3 or 4 second animation, its going to be hard to ignore.
thank you ^^ that's an elegant solution.. I was going to try using threads, but this works too.
I'll add you to the script credits. (do you want to forbid commercial use? if so tell me, and I'll add that to the header)
I'm sure this script will be useful to many, especially people working on a custom animated scene.

Edit:
tiny problem with scene-changes: if done close to a scene change, the animation will not appear in the right scene, but in the new one. ie: if you have an animation play on the menu when canceling, the animation will play on the map screen, not on the menu screen.
so I'll take your advice & let the user choose whether to freeze until it ends or not.
I'll also clear animations when a scene changes.
  1. #---------------------------------------------------------------------
  2. # ** Animation
  3. #---------------------------------------------------------------------
  4. #
  5. #  V.1.2
  6. #  Made by: silverwind & ForeverZer0
  7. #    
  8. #  * This class enables playing a database animation
  9. #    in every scene, including costum ones.
  10. #
  11. #  * Instructions:
  12. #    put the following code in a script / an event's script call:
  13. #    Animation.new(id,x,y)
  14. #    where  id= the number of the animation in the database
  15. #           x= screen x
  16. #           y= screen y
  17. # * Notes:
  18. #   - If the animation shows below something else on the screen, use:
  19. #     Animation.new(id,x,y,z)
  20. #      Experiment with z's value. The default value is 9000,
  21. #      so try higher numbers.
  22. #   - freeze
  23. #     Animation.new(id,x,y,z,true)
  24. #     ^ use this code to freeze the game until the animation is done.
  25. #     useful when you want to play an animation during a scene change.
  26. #     (ie, going from the menu screen to the map screen)
  27. #---------------------------------------------------------------------
  28.  
  29.  
  30. class Animation < RPG::Sprite
  31.  
  32.   attr_accessor :frames           # total frames in the animation
  33.  
  34.   def initialize(id, x, y, z = 9000, freeze=false)
  35.     viewport = Viewport.new(0, 0, 640, 480)
  36.     viewport.z = z
  37.     super(viewport)
  38.     self.x, self.y = x, y
  39.     @frames = $data_animations[id].frame_max
  40.     hit = true
  41.     self.animation($data_animations[id], hit)
  42.     Graphics.play_animation(self, freeze)
  43.   end
  44.  
  45.   def dispose
  46.     self.viewport.dispose
  47.     super
  48.   end
  49.  
  50. end
  51.  
  52. module Graphics
  53.  
  54.   class << self
  55.    
  56.     def freeze_play_animation(frames)
  57.       if @animations != nil && @animations.size > 0
  58.         for i in 0..frames : update end
  59.       end
  60.     end
  61.    
  62.     def clear_animations_on_scene_change
  63.       return if @last_scene.nil?
  64.       if @last_scene != $scene.class.name
  65.         @animations.each_index {|i|
  66.               @animations[i].dispose
  67.               }
  68.         @animations = []
  69.       end
  70.     end
  71.    
  72.     def play_animation(animation, freeze)
  73.       @animations = [] if @animations == nil
  74.       @animations.push(animation)
  75.       if freeze
  76.         freeze_play_animation(animation.frames)
  77.       end
  78.     end
  79.    
  80.     alias animation_update update
  81.     def update
  82.       if @animations != nil && @animations.size > 0
  83.         @animations.compact!
  84.         @animations.each_index {|i|
  85.           @animations[i].update
  86.           @animations[i].frames -= 1
  87.           if @animations[i].frames < 0
  88.             @animations[i] = @animations[i].dispose
  89.           end
  90.         }
  91.       end
  92.       animation_update
  93.       clear_animations_on_scene_change
  94.       @last_scene = $scene.class.name
  95.     end
  96.   end
  97.  
  98.  
  99. end
Expand to see the code.
You don't need to credit me, nor do I care about commercial use, but I appreciate the thought.

The scene problem could be solved something like this:

  • In the Animation class, add an attr_reader called "scene" or whatever
  • In Animation's initialize, add a @scene = $scene.class
  • In the Graphic's update where each animation is updated, add an additional check:
    1. if $scene.is_a?(animation.scene)
    Expand to see the code.
  • If false, dispose the animation and remove it from the array
well, your name is in the credits, if you want it or not ;)
I already fixed the scene problem, and updated the first post with the new code. should have been more clear about it in my last post ^^;

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76315