Game Update Efficiency

So,

I was wondering what you guys think the most efficient way of updating something would be.

To be more specific, I have created a class. This class is attached to a global variable. I want to update this class, via calling something like $variable_name.update.

The code looks somewhat like this....

$variable_name = Class_Name.new class Class_Name  def initialize    # // Makes Variables And Stuff  end  def update    # // Updates Variables And Stuff  endend
I figure that I could do this multiple different ways....

- I can simply call the update during each Graphics.update

- I can call the update during every scene's update

- I can call the update during selected scene's update

Anyone able to offer a more suitable alternative?

Maybe provide a reason why I shouldn't do any of these?

Any other insight?
If you only ever need to update when something specific changes there is no reason why you would need to update all the time.
It is to create an in game time system.

I figured the update would have to be fairly frequent. The most logical thing (to me) would be to update each time the graphics are updated. Or alternatively, when on map/battle.

Edit...

Would it be more efficient to simply create a module that calls methods, rather than a class attached to a global variable ? Would that not use unnecessary processing power ?
Do you want it to update while in menus or just on the map? If just on the map, I'd throw an update into the Scene_Map's update method - at whatever point it does the update to everything that hangs off it.


You could also check out the Game_Timer class since it must do at least some of what you're doing. That's updated in Scene_Map.


If it exists when the very first scene is created and you want it to update in ALL scenes, I'd be looking at putting something in Scene_Base's update method.


Since some classes (at least in previous versions) sometimes run a Graphics.update outside of the normal update cycle, you probably don't want to put it into the Graphics.update method.
I would define an update_time in Scene_Base and then for every scene that needs or doesn't need to update time I would just overwrite the method.


It doesn't really matter where you choose to put it.


Sure accessing a global variable is faster since you don't need to make method calls or anything but you're talking about microseconds of difference.


Sure, if you call it a billion times then the player will notice that a whole second of lag has accumulated.


If that kind of performance is a big deal then I guess you can go with global variables.
Thats exactly how it is currently setup.  Well... A mixture of methods...

The code looks like this...

#===============================================================================module Game_Time_Updater#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  @update_timer = 60  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def self.update    @update_timer += 1    complete_update  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def self.complete_update    return unless @update_timer >= 60    @update_timer = 0    $time.update  endend#===============================================================================class Scene_Base#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  alias :start_game_time  :start  alias :update_game_time :update  #--------------------------------------------------------------------------  # * Start Processing  #--------------------------------------------------------------------------  def start    start_game_time    start_game_tmod  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def update    update_game_time    update_game_tmod  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def start_game_tmod    # TIME::UDSC = ['Scene_Map', 'Scene_Battle']    @can_update_time_mod = TIME::UDSC.include?(self.class.name.to_s)  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def update_game_tmod    return unless @can_update_time_mod    Game_Time_Updater.update  endendthis is so that it updates  once every 60 frames on certain scenes.

I guess it would just be best to keep it as is.

Thanks for the thoughts ^_^
I'll just point out the obvious: try to avoid global variables whenever possible.

There should be something else you can attach that to without needing to give it a global scope.
What is the difference between storing it in a global variable, and storing it with an object that is a global variable?


I would store it with an object that is already using global variables (to avoid introducing yet another one), but "global variables are bad" doesn't seem like it has an obvious reason.
what does $time point to? if it's the module itself, I don't see any reason of doing it


or is it the default global for the Game_Timer?
@Mouser,

That is what I was thinking, as there is no real need for having it attached to a global variable. The only small reason being so that other devs can easily access it (if they wanted to)

Other than that it's useless, and there are ways around that...

Currently the variable $time is hooked into my own class called Game_Time. Which simply stores the Games current time/ month/ year/ season etc.

I was thinking of changing the Game_Time class into a module. I would then change it so its only using module variables and methods.

Then the player could use Game_Time.whatever_method when calling it, rather than $time.whatever_method.

Personally, I dont think the efficiency of having to type 4 less characters each time you call the data will counter the efficiency lost due to having it as a global variable. So I may as well do it this way 🙂

I could then simply do something like this...

#===============================================================================module Game_Time#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def data    [@sec,@min,@hour,@day,@week,@month,@season,@year]  endend#===============================================================================class Game_System#===============================================================================  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  attr_accessor :game_time_data  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  alias :game_time_data_init       :initialize  alias :game_time_on_before_save  😮n_before_save  alias :game_time_on_after_load   😮n_after_load  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def initialize    game_time_data_init    @game_time_data = Game_Time.data  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def on_before_save    game_time_on_before_save    @game_time_data = Game_Time.data  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def on_after_load    game_time_on_after_load load_game_time  end  #-----------------------------------------------------------------------------  #  #-----------------------------------------------------------------------------  def load_game_time    d = @game_time_data    Game_Time.set_to(d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7])  endendAnd this would ensure that when the game is loaded the time would remain as it was when you last played.
What's the difference between storing it in a global variable, and storing it in a static class/module?


They all still have global state. Doesn't really seem to make any difference to me when it comes to "avoid global variables"


But now instead it's "avoid static objects"
The only difference that I can think of, is that when calling a module method, it doesnt check all variables when called compared to if calling a class object, it would get the data for all of it.  A very small difference in performance really, but it all helps.

Yes, you are right in stating that it would still have a 'global state', it has to in order for it to properly function the way I want.

Then again, I could be mistaken in thinking that calling a module method doesnt have to load all the modules data :/
What's the difference between storing it in a global variable, and storing it in a static class/module?

They all still have global state. Doesn't really seem to make any difference to me when it comes to "avoid global variables"

But now instead it's "avoid static objects"
It's a general principle of Object Oriented design. Can the rule be broken? Of course, but it's a good general design guideline.

See also: accessor methods - never change the value of a variable directly.
I would use the system clock, something like this:

class Time_Check

def initialize

next_update_time=Time::now

end

def do_update

if (Time::now < next_update_time)

return false

end

next_update_time=Time::now

return true

end

end

Then, to use it:

checker=Time_Check.new

if (checker.do_update)

# Do all of my updates

end

However, I have an in-game time script complete with a demo available:

http://www.rpgmakervxace.net/topic/22975-flextimer-calendar-event-management

Original thread: https://forums.rpgmakerweb.com/threads/game-update-efficiency.25955/