Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

SP Regen

I was going to put this in RGSS/RGSS2 Help, but I figured it's more of a request so I put it here.  I would like a script that will regenerate 5 SP every 60 frames unless the player is in battle.  I want this script to work for the first actor in the party, not the others.  I also would like to be able to turn this script on and off based on a switch (I don't care which ID).  This seems like a fairly simple request, but I'm not great at scripting yet so I froze the game trying to do this.  Please, if you could explain how each line works, that would really help me!  Thanks in advance!

By the way, if I was not clear about something, please let me know.
 
Just make a common event with parallel process and a switch.  Then use Loop, Player 1 gains 5 SP, wait 60 frames.  I just learned that parallel process only works on maps, so it won't activate during battle.

~Guardian1239
 
@Guardian - Hmm...I'll try that.

@Larynni - Yeah, as good as I am with events, I still want to learn more about scripting.  It's much more flexible, and I could do a lot more stuff with it.
 
Hi AbyssalLord,

I saw your request and i decided to give it a try!
I must admit, at first i had no idea on how to script this but i think i've figured it out.
Here is the script i've come up with, I've commented pretty much everything in it so that you can understand what i've done
and what everything does.
Code:
#==============================================================================
# ** Game_System
#------------------------------------------------------------------------------
# By: Gando
# 8/4 - 2008
#------------------------------------------------------------------------------
#
#  First, i've aliased the initialize and update method in Game_System.
#  If you don't know what aliasing does i suggest that you read Me(tm)'s
#  tutorial Aliasing with ease. You can find the tutorial here:
#  
#  http://www.rmxp.org/forums/index.php?topic=5698
#
#
#==============================================================================
class Game_System
  alias gando_timer_init initialize
  alias gando_timer_update update
  #--------------------------------------------------------------------------
  # * Initialize the "Gando_timer".
  #--------------------------------------------------------------------------
  #  gando_timer_init - This will add the old code from def initialize in
  #                     Game_System.
  #
  #  @gando_timer - this is and instance variable holding the amount of 
  #                 frames(60). You could say that this is the timer.
  #
  #
  #  So what i have done is that i have added the @gando_timer in the 
  #  initialize method in Game_System.
  #
  #--------------------------------------------------------------------------
  def initialize
    gando_timer_init
    @gando_timer = 60
  end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  #  gando_timer_update -This will add the old code from def update in
  #                      Game_System.
  #
  #  reset_timer - This line will call the reset_timer method, 
  #                and it will execute everything inside it.
  #
  #   I am not 100% sure about this, but i think the update method updates
  #   every frame. That means, that it will reduce the @gando_timer by 1
  #   every frame.  If i'm wrong, the i hope an experienced scripter can tell
  #   me if they see this.
  #--------------------------------------------------------------------------
  def update
    gando_timer_update
    # reduce timer by 1 if timer is above 0.
    if @gando_timer > 0
      @gando_timer -= 1
    end
    # Checks if swith 1 is on
   if $game_switches[1] == true
     #Checks if @gando_timer has reached 0, (if 60 frames has passed)
    if @gando_timer == 0
      reset_timer
    end
   end
 end
  #--------------------------------------------------------------------------
  # * Reset_timer
  #--------------------------------------------------------------------------
  #   Here, you can add whatever you want to happen
  #   when x number of frames has passed.
  #
  #   The line : "unless $game_temp.in_battle" means, as long as your not
  #               in battle. With other words, it checks so that your not 
  #               in any battle, and if your not, it increases the sp.
  #
  #   The line : $game_party.actors[0].sp += 5, gives the first actor in 
  #              the party 5 sp. if you want to give another member sp you
  #              can just change the 0 to either 1,2 or 3 depending on which
  #              party member you want to increase the sp of.
  #
  #              You could also change the  .sp  to  .hp   to give the
  #              party member 5 hp instead of 5 sp.
  #
  #   @gando_timer = 60 - This will reset the timer to 60 again, so that
  #                       the lead actor can keep regenerate sp every 60 
  #                       frame.
  #
  #--------------------------------------------------------------------------
  def reset_timer
  unless $game_temp.in_battle
    $game_party.actors[0].sp += 5
  end
    @gando_timer = 60
  end
end

If any experienced scripter should see this post, i would like for them to look over the script i made and point out if i'm wrong somewhere.
Cuz since om not that experienced at scripting, im not 100% sure on everything and i wouldn't want to teach you something that isn't true.. :tongue2:

Anyways, i hope you like it and that you'll understand scripting a little bit more now!
Good luck! :thumb:

Over and out - Gando
 
Thanks a lot for the help Gando!  I'll try this out as soon as I get a chance.  Btw, I think I found my main problem (I was thinking about it at school today, actually), I forgot the aliases which I think froze the game.  I think I also made the class Scene_Map instead of Game_System.  I'm not sure what that did, but forgetting the aliases was a big problem.  Anyway, thanks again!
 
No problem! :thumb:
Yeah, that might have been the problem, if you don't alias you're just overwriting the old method in the class instead of adding new things to it.
so there is a chance that not aliasing was the reason the game froze.
My guess would be that you were overwriting the def update method.^^
 
Detail? uhm let's say i'm using gando's script for my sp regen
my sp regen is 5 per 60f
but i talked to someone that gives me stronger magical power
Then my sp regen just increased to 10/60f!
 

khmp

Sponsor

Excellent work Gando! :thumb:

@AbyssalLord: he's referring to the changing of @gando_timer on the fly. A simple fix but this is Gando's baby so I won't steal his thunder.
 
@khmp:  hehe, thanks!^^

@omegazion:  Well, you could put a switch condition in the reset_timer method. like, if switch 2 is on, you regen 10 sp/60f    else you regen  5 sp/60f..  Like this, replace the "reset_timer" method in the script with this:
Code:
  def reset_timer
  unless $game_temp.in_battle
   if $game_switches[2] == true
    $game_party.actors[0].sp += 10
   else
    $game_party.actors[0].sp += 5
  end
  end
   @gando_timer = 60
  end

If you want to add more options, just add "else if $game_switches[id] == true" and then "$game_party.actors[0].sp += value" under it.
But remember that you have to add the "else if" stuff before the "else".  And another thing you should remember is that for every "else if" you write, you have to put an extra "end" after "else".  I'll show you and example, this is the reset_timer method with 3 options:
Code:
  def reset_timer
  unless $game_temp.in_battle
  if $game_switches[2] == true
    $game_party.actors[0].sp += 10
  else if $game_switches[3] == true
    $game_party.actors[0].sp += 1    
  else
    $game_party.actors[0].sp += 5
  end
  end
  end
   @gando_timer = 60
  end
If switch 2 is on, the leader will regen 10 sp/60f,  if switch 3 is on, the leader will regen 1 sp/60f,  and if neither switch 2 or 3 is on, the leader will regen 5 sp/60f (default regen).


So all you have to do is replace the reset_timer in the script with this,
and then make it so when the player talks to this guy, switch 2 (for example) is turned on.
I hope that helped. Good luck! :thumb:

Over and out - Gando
 
Gando, I don't want to take this request away from you, but now that you've showed me how to this without freezing the game, I can make a much more organized and customizable version of it.  If you don't mind, I'd like to give it a shot.
 
No problem, i don't mind at all!
I'm eager to see what you come up with, that is.. if you decide to post it. 
If so, i might learn something from you too! :wink:
Anyways, good luck with it! :thumb:

Over and out - Gando
 
I made it.  I just need to test it before I post it.



EDIT: I tested it and it just ended up in a load of errors.  They are probably simple and stupid (most likely the wrong number of 'end' statements), but I'm way too tired right now to tell what I did wrong, so I'll post it here for you guys to look at.  I'll be busy this weekend, so if you find what's wrong, please tell me.  Thanks!

Here's the code:

Code:
#===============================================================================
#**Game_System
#-------------------------------------------------------------------------------
#By AbyssalLord and Gando
#-------------------------------------------------------------------------------
#Credits go to AbyssalLord and Gando
#===============================================================================

class Game_System
  
#===============================================================================
#Constant Variables
#-------------------------------------------------------------------------------
#SET - Set this to 'true' if you want to keep the sp regen and wait frames at a 
#      set amount.
#ACTOR_EFF - Set this to the ID of the actor affected by the sp regen. 
#            0 == first actor in the party, 1 == second, etc.
#
#If SET == true
#
#SP_REGEN_SET - Set this to the amount of SP you want the player to regain.
#WAIT_FRAMES - Set this to the amount of frames you want to wait between each 
#              regen.
#
#If SET == false
#
#SP_REGEN_VAR - Set this to the ID of your sp regen variable.  Set this variable
#           in-game to the value you wish to add to the player's SP.
#WAIT_FRAMES_VAR - Set this to the ID of your wait frames variable.  Set this 
#                  variable in-game to the interval of frames between regaining sp.
#===============================================================================

SET = false
ACTOR_EFF = 0 

SP_REGEN_SET = 5
WAIT_FRAMES_SET = 60

SP_REGEN_VAR = 1
WAIT_FRAMES_VAR = 2 


#-------------------------------------------------------------------------------
#Alias Methods
#-------------------------------------------------------------------------------

alias abyss_init initialize
alias abyss_update update
  
#-------------------------------------------------------------------------------
#Object Initialize
#-------------------------------------------------------------------------------

  def initialize
    abyss_init
    @set = SET
    @wait_frames = WAIT_FRAMES_SET
    @sp_regen = SP_REGEN_SET
    set_var
      if @check <= 1
        @original_wait = $game_variables[WAIT_FRAMES_VAR]
      end
    end
  end
  
#-------------------------------------------------------------------------------
#If set...
#-------------------------------------------------------------------------------

  def set_var
    unless @set == true
      @check += 1
      @sp_regen = $game_variables[SP_REGEN_VAR]
      @wait_frames = $game_variables[WAIT_FRAMES_VAR]
    end
  end
  
#-------------------------------------------------------------------------------
#Frame Update
#-------------------------------------------------------------------------------

def update
  abyss_update
  set_var
    if @set == true
       if @wait_frames > 0
         @wait_frames -= 1
       elsif @wait_frames == 0
         reset_sp_regen
       end
    else
      if @wait_frames > 0
        $game_variables[WAIT_FRAMES_VAR] -= 1
        set_var
      elsif @wait_frames == 0
      reset_sp_regen
    end
  end
  
#-------------------------------------------------------------------------------
#Reset SP Regen
#-------------------------------------------------------------------------------

  def reset_sp_regen
    unless $game_temp.in_battle
      set_var
        if @set == true
          $game_party.actors[ACTOR_EFF].sp += @sp_regen
          @wait_frames = WAIT_FRAMES
        else
          $game_party.actors[ACTOR_EFF].sp += @sp_regen
          @wait_frames = @original_wait
        end
      end
    end
  end
end

Yeah...probably too many 'ends'.  I lost count.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top