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.

Help editing existing classes (using alias)

Akin

Member

I'm still pretty new to scripting and I'm working on a script that creates an options screen. I'm about halfway done but I found that I need to edit some of the Scene_Title and Scene_Map classes to get things to work properly. Rather than have to go in and insert the code directly into the game's existing script, I'm pretty sure I can just put the code with my own code block. I'm not really sure how to do it though. I think I might need to use an alias to open the class without changing whats already in it.

My example is that I need to add the line Audo.adjust_bgm_volume to the play_title_music in the Scene_Title class
Code:
   def play_title_music
    $data_system.title_bgm.play
    Audio.adjust_bgm_volume
    RPG::BGS.stop
    RPG::ME.stop
  end

Whats the best way to go about addeding this without touching the main game code as much as possible?
 
There's a CMS with a volume control module that I found at, I believe, CreationAsylum. If you put this into the Game_System class, it *should* work, but thats strictly an assumption. Try to avoid writting duplicate code for multiple classes if you don't have to. If you define it in Game_System (which was what the origional scripter did), then you won't have to write it in Scene_Map or Scene_Title.

I'm still learning this stuff myself, but this should get you off to a good start. Put all of it below the 'end' following 'def initialize' somewhere where it would make sense. Also, be sure to comment it just like the rest of the script, and put some tag (like your initials or something) next to any added code, so you can CTRL+F to find it again later if you need to delete it.

Code:
  def bgm_play(bgm)
    @playing_bgm = bgm
    if bgm != nil and bgm.name != ""
      Audio.bgm_play("Audio/BGM/" + bgm.name , bgm.volume * bgm_volume / 100, bgm.pitch)
    else
      Audio.bgm_stop
    end
    Graphics.frame_reset
  end
  
  def bgs_play(bgs)
    @playing_bgs = bgs
    if bgs != nil and bgs.name != ""
      Audio.bgs_play("Audio/BGS/" + bgs.name, bgs.volume * se_volume / 100, bgs.pitch)
    else
      Audio.bgs_stop
    end
    Graphics.frame_reset
  end
  
  def me_play(me)
    if me != nil and me.name != ""
      Audio.me_play("Audio/ME/" + me.name, me.volume * se_volume / 100, me.pitch)
    else
      Audio.me_stop
    end
    Graphics.frame_reset
  end

  def se_play(se)
    if se != nil and se.name != ""
      Audio.se_play("Audio/SE/" + se.name, se.volume * se_volume / 100, se.pitch)
    end
  end

Also, yes, you can modify a class from an outside script, you just have to call the class again like the origional script...

Say you're doing it in a CMS (thats seperate from the rest of the game scripts), just re-define it like so...


Code:
class Game_System
  def custom_initialize initialize #alias method
    @bgm_volume = 100
    @bgs_volume = 100
    @se_volume = 100
    @me_volume = 100
  end
  #----------------------------------------------------------------------
  # * Added functions
  #----------------------------------------------------------------------

Now, you should be able to write that stuff from this point. Everything posted above should go here. (Again, no matter how silly it may seem, be sure to comment your code.)

In this case, this method is aliased (so it should inherit all the information previously called in Game_System.) My knowledge on alias-ing methods is still new, I'm going off of 100% theory. If I'm wrong somebody will surely come around and correct me ;)

I hope I have been of some help to you, good luck! :D
 

Akin

Member

thanks alot I'm rewriting my script as I speak, it was functioning the first way I had it but definately not the best. I'm trying to figure out how using alias works to insert code into existing classes. I'm using this now to add variables for the various game volumes, but I'm not sure how it works 100%. The code that you posted, Kain, I belive only works for XP, and I'm trying to make one that works for VX. I belive that I need to insert code into the Game_Interpreter class but I'm not 100% sure how to use "alias" to acomplish this. That's more what I need help with I have a pretty good handle on how to adjust the volumes in the game. I really do appreciate the help and thanks for the tips Kain Nobel.


Code:
# This adds the option screen volume values to the game system
class Game_System
  attr_accessor :bgm_volume
  attr_accessor :se_volume
  attr_accessor :bgs_volume
  attr_accessor :me_volume
  alias akin_sound_ini_game_system initialize
  
  def initialize
    akin_sound_ini_game_system
    # BGM, BGS, SE and ME Volume Values for storage
    @bgm_volume = 70; @se_volume = 100; @bgs_volume = 70; @me_volume = 100;
  end
end
 
Did you get the alias stuff figured out? I honestly haven't really tried aliasing too much, but I believe it should work like an existing class.

Code:
class Scene_Map
  alias kn_scene_map_main main #Name your alias, then the method it modifies
  def main #Call the origional method like so
    # Your additional code here
    kn_scene_map_update #Will jump to next aliasing method
  end
  alias kn_scene_map_update update #Again, name your alias, then the method it modifies
  def update #Call the origional method like so
    # Your additional code here
  end
end

Again, I just vaugley remember this from studying other scripts, I haven't wrote an alias method before but I think its just as simple as that really. (If you want to get down to the nitty gritty, it kinda reminds me of the Jump To Label event command, but yeah... different subject.) And best advice when aliasing, always try to name your alias method with your trademark, such as your initials, so you can always CTRL+SHIFT+F all of your custom methods ;)

kn_scene_map_update #<--Tells me its my method, it belongs to Scene_Map and its the update method.

I believe, correct me if I'm wrong, an alias has the same strength as a global or constant variable, as they are read from anywhere, so be sure not to alias something with the same name for different methods or even similiar methods in two+ different classes. Matter of fact, you can read more about aliasing in the instructions of the SDK, I think some of the explanations are better than the RPGXP/VX help files when describing scripting methods.
 

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