Extreme Battle Lag

Neo_Kum0rius_6000

Not Your Ordinary Guy!
Veteran
Joined
Nov 8, 2017
Messages
196
Reaction score
349
First Language
english
Primarily Uses
RMVXA
So I'am using a script in my game that adds animated face sprites
it works great but in battles the game lags so much when there's more
than 1 character with an animated face. Also when you use an item the game
lags big time and it lasts through the entire battle. The item thing even
effects when there's only one party member. The game drops from 60 FPS down to 9 FPS in seconds

Can someone please fix this script?
I really need some help.
Thanks!
Code:
#===============================================================================
# )----------------------------------------------------------------------------(
# )--     AUTHOR:     Mr Trivel                                              --(
# )--     NAME:       Animated Faces                                         --(
# )--     CREATED:    2014-09-12                                             --(
# )--     VERSION:    1.2                                                    --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                         VERSION HISTORY                                --(
# )--  1.0 - Initial script.                                                 --(
# )--  1.1 - Fixed bugs. Uses Frames per second instead of pure frames.      --(
# )--  1.2 - Fixed more bugs. Loops through drawn faces instead of party.    --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          DESCRIPTION                                   --(
# )--  Allows for actor faces to be animated.                                --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          INSTRUCTIONS                                  --(
# )--  Define animation files and frame sequence in ANIMATED_FACES module.   --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          LICENSE INFO                                  --(
# )--  Free for non-commercial & commercial games if credit was given to     --(
# )--  Mr Trivel.                                                            --(
# )----------------------------------------------------------------------------(
#===============================================================================

# )=======---------------------------------------------------------------------(
# )-- Module: ANIMATED_FACES                                                 --(
# )---------------------------------------------------------------------=======(
module ANIMATED_FACES
  # )--------------------------------------------------------------------------(
  # )-- Place your animated faces data here.                                 --(
  # )--------------------------------------------------------------------------(
  DATA = {
  # )-- ACTOR_ID    Filename      Frame Sequence                 
          1 => [ "Casey_Face.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]
 
  }
 
  # )--------------------------------------------------------------------------(
  # )-- How fast should frames change. Speed in milliseconds.                --(
  # )--------------------------------------------------------------------------(
  FRAME_SPEED = 20
end

# )=======---------------------------------------------------------------------(
# )-- class: Window_Base                                                     --(
# )---------------------------------------------------------------------=======(
class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_update :update
  alias :mrts_face_anims_initialize :initialize
 
  # )--------------------------------------------------------------------------(
  # )-- Alias: initialize                                                    --(
  # )--------------------------------------------------------------------------(
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}
    
  end
  # )--------------------------------------------------------------------------(
  # )-- Overwrite: draw_actor_face                                           --(
  # )--------------------------------------------------------------------------(
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end
 
  # )--------------------------------------------------------------------------(
  # )-- Alias: update                                                        --(
  # )--------------------------------------------------------------------------(
  def update   
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    mrts_face_anims_update
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v| 
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
 
  # )--------------------------------------------------------------------------(
  # )-- New Method: ms                                                       --(
  # )--------------------------------------------------------------------------(
  def ms(start, finish)
   (finish - start) * 1000.0
  end
end
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,248
Reaction score
1,250
First Language
Spanish
Primarily Uses
RMVXA
you're hooking up the validation loop from the Window_Base update, which effectively runs for every single window.
 

Neo_Kum0rius_6000

Not Your Ordinary Guy!
Veteran
Joined
Nov 8, 2017
Messages
196
Reaction score
349
First Language
english
Primarily Uses
RMVXA
you're hooking up the validation loop from the Window_Base update, which effectively runs for every single window.
I honestly don't understand is there a way I can fix this?
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,248
Reaction score
1,250
First Language
Spanish
Primarily Uses
RMVXA
I honestly don't understand is there a way I can fix this?
yes, it's just a misplaced procedure.
I can show you how, but right now I don't have a clean Ace project at hand.... give me a minute.

EDIT:
here.
Code:
module ANIMATED_FACES
  DATA = {
          1 => [ "Actor1.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]

  }
  FRAME_SPEED = 20
end

class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_initialize :initialize 
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}    
  end
 
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end

  def ms(start, finish)
   (finish - start) * 1000.0
  end

  def update_anim_frame
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v| 
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
end


class Window_MenuStatus < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

class Window_SkillStatus < Window_Base
  def update
    super
    update_anim_frame
  end
end

class Window_Status < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

basically what I did was split the new code away from the update procedure, and make it into a separate procedure called update_anim_frame.
then, have all windows relying on draw_actor_face() call update_anim_frame after calling their parent's update.
that way, no procedure that doesn't require draw_actor_face() will run that extra chunk of code.

I'm sure it can be made more efficiently, but I don't want to think right now :frown:
 
Last edited:

Neo_Kum0rius_6000

Not Your Ordinary Guy!
Veteran
Joined
Nov 8, 2017
Messages
196
Reaction score
349
First Language
english
Primarily Uses
RMVXA
yes, it's just a misplaced procedure.
I can show you how, but right now I don't have a clean Ace project at hand.... give me a minute.

EDIT:
here.
Code:
module ANIMATED_FACES
  DATA = {
          1 => [ "Actor1.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]

  }
  FRAME_SPEED = 20
end

class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_initialize :initialize
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}   
  end
 
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end

  def ms(start, finish)
   (finish - start) * 1000.0
  end

  def update_anim_frame
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v|
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
end


class Window_MenuStatus < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

class Window_SkillStatus < Window_Base
  def update
    super
    update_anim_frame
  end
end

class Window_Status < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

basically what I did was split the new code away from the update procedure, and make it into a separate procedure called update_anim_frame.
then, have all windows relying on draw_actor_face() call update_anim_frame after calling their parent's update.
that way, no procedure that doesn't require draw_actor_face() will run that extra chunk of code.

I'm sure it can be made more efficiently, but I don't want to think right now :frown:
Thanks for taking the time to help me!!!
It means alot!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c

Forum statistics

Threads
105,857
Messages
1,017,018
Members
137,563
Latest member
MinyakaAeon
Top