Ruby/RGSSx questions that don't deserve their own thread

I still can't help without seeing your script call and eventing. 


Show me a screenshot of the event page where you do the script call for these windows.


oh.png
Hey guys I'm using a message sound script and I would like to be able to change the volume and pitch of the sound mid-game by storing them into variables.


Here's how it is right now:


MESSAGE_SOUND = RPG::SE.new("Voice", 100, 100)




here's what I would like to do:


MESSAGE_SOUND = RPG::SE.new("Voice", $game_variables[1], $game_variables[2])




Here's the script:

Spoiler
#==============================================================================
# Text Sound Effect (version 2)
# NEW FEATURE: Switch to turn off sound effect
#------------------------------------------------------------------------------
# by Zerbu
#==============================================================================
module Text_Sound_Effect
  #--------------------------------------------------------------------------
  # Options
  #--------------------------------------------------------------------------
  # The sound effect to play
  MESSAGE_SOUND = RPG::SE.new("Voice", 100, 100)
 
 
  # The number of characters to display before each time the sound plays
  # The default is 3, it's recommended you keep it as this unless you
  # know what you're doing
  MESSAGE_SOUND_FRAMES = 5
 
  # Switch to disable sound effect
  # If you need to turn off the sound effect for any part of the game,
  # turn this switch on
  # Set to nil to disable this feature
  MESSAGE_SOUND_DISABLE = nil
 
end
 
class Window_Message < Window_Base
  include Text_Sound_Effect
  #--------------------------------------------------------------------------
  # alias method: process_characer
  #--------------------------------------------------------------------------
  alias textsound_process_character_normal process_character
  def process_character(c, text, pos)
    if !MESSAGE_SOUND_DISABLE or !$game_switches[MESSAGE_SOUND_DISABLE]
      #---
      if !defined?(@sound_frames)
        @sound_frames = 0
      end
      #---
      if @sound_frames == 0
        MESSAGE_SOUND.play
      end
      #---
      @sound_frames+=1
      #---
      if @sound_frames == MESSAGE_SOUND_FRAMES
        @sound_frames = 0
      end
      #---
    end
    textsound_process_character_normal(c, text, pos)
  end
  #---
end
You could use const_set to set the sound volume and pitch on the fly :


Text_Sound_Effect.const_set("MESSAGE_SOUND", RPG::SE.new("Voice", $game_variables[1], $game_variables[2]))




I might also suggest Modern Algebra's Message Options. The script has customizable text sound. 
Silly question, I know, but aliasing confuses me enormously. I never understand the right time to alias and when to overwrite, and how the system will even know what your method is supposed to mean. 
@HexMozart88 Basically, whenever you are adding onto an existing method, you should alias that method. Let's say you want to add code onto the following method :


class Game_CharacterBase

#--------------------------------------------------------------------------
# * Initialize Public Member Variables
#--------------------------------------------------------------------------
def init_public_members
@id = 0
@x = 0
@y = 0
@real_x = 0
@real_y = 0
@tile_id = 0
@character_name = ""
@character_index = 0
@move_speed = 4
@move_frequency = 6
@walk_anime = true
@step_anime = false
@direction_fix = false
@opacity = 255
@blend_type = 0
@direction = 2
@pattern = 1
@priority_type = 1
@through = false
@bush_depth = 0
@animation_id = 0
@balloon_id = 0
@transparent = false
end

end


Overwriting the method to add in a variable or so on could bork other aliased methods down the line, or bork whatever code you are attempting to insert. So, we alias the method to ensure that not only our code functions, but code in other scripts can function as well. Aliased methods are basically added to a 'chain' ... if the method is aliased multiple times by several scripts then each alias is called in turn, running the code therein before moving on to the next alias. So if your alias is first to be run, and you bork something up ... you probably bork the other aliases of the method, too.

Code:
class Game_CharacterBase

  #--------------------------------------------------------------------------
  # * ALIAS - Initialize Public Member Variables
  #--------------------------------------------------------------------------

  # F12 Crash Protection - Check to see if the method has already been aliased
  # by our script.

  # The name of the aliased method should be as unique as possible. No 
  # 'myaliasedfunction' or anything generic like that, as it is possible
  # for another script to use similar name and bork everything.
  unless method_defined?(:yourprefix_El3fajtA_gcb_init_public_members)
    alias_method(:yourprefix_El3fajtA_gcb_init_public_members, :init_public_members)
  end
  
  # Adding in *args, &block is also important. Unless you are using a 
  # specific argument which is being passed to the method, toss in 
  # *args, &block.
  # *args is all arguments being passed through the method, including any
  # new arguments which are added in via new scripts and so on ... this will
  # ensure maximum compatability.
  # &block is rarely used and a little complicated to explain ... basically
  # adding it in will help raise compatability.
  def init_public_members(*args, &block)
    yourprefix_El3fajtA_gcb_init_public_members(*args, &block)
    
    # Our variable we wanted to add in. 
    @random_test_variable = 0;
  end

end
Thanks, that makes a lot more sense now  🙂
If you plan to share your scripts, you should alias the default methods you want to change whenever you can for compatibility.


If you make your scripts for yourself only, you can overwrite them and add whatever you need to add at one place only (without scattering the separate things in multiple scripts).


The system knows what to do with them, because when you create an alias method it will save the old/original method, and you define the name for that saved method, and you use that same name in the method you want after the alias line (not necessarily in the same method, but in the same class/module).


So, by using the name of the aliased method, you simply call an existing method, they are just like any other methods you have created, it's just that in the case of alias methods, you don't really type the whole method in yourself, the system does it for you, and it will "type in" whatever the aliased method got at the point of the alias call.


If the thing you want to edit is in the middle of a method, and if that thing got no other reference to get to from any other methods, you will most probably have to overwrite the method. But in most cases, there is a way to successfully alias a method.


And finally, there are many different alias types you can use (although, their correct usage is often debatable). 


I believe there was a topic about these alias types around here somewhere, you should definitely check that one out. I'm too lazy to search for it now, thou. 😛


When I say "alias types" I don't mean different keywords for calling different alias types, I mean that depending on how, when and where you use the aliased method, it can greatly change the function and the result of the final method in the end.


Ohh, and you should also learn when to use alias and when to use a super call. That is an important information too.


This is related to aliasing in classes which inherit from other classes. If the super-class of another class got a method defined, and this another class is a super-class for yet another class but did not defined that very same method again in it's class, you should use a super call in the lowest level class instead of an alias if you want to get the original method in your code.


Okay, I am sure that this one sounds especially confusing, but there is another topic around here which explains this too (it tells about the issues which could rise if you do it wrong).


No idea if this was helpful or not, so sorry if it wasn't. 😛


I remember having the same issue as you, and the only thing I could do to learn all this was to just do the old fashioned trial and error myself, because I simply couldn't understand what people wanted to explain about this topic. 


In any case, you will learn it naturally if you keep coding for a longer time, and if you experiment a bit with it.
No idea if this was helpful or not, so sorry if it wasn't. 😛

It was, don't worry.  🙂

you will learn it naturally if you keep coding for a longer time, and if you experiment a bit with it.

OK, guess I'll have to get scripting 🙂
So, trying to create a state that increases the enemies MHP, and scales their health up to match the new max. The general thought is that I'll convert it to a percentage, scale it, and then.. Well, haven't figured that bit out yet. Anyway! The first bit causes a crash, as an array can't be coerced into a fixnum.


$game_variables[104].to_f = ($game_troop.members[0].hp.to_f / $game_troop.members[0].mhp.to_f)


Could anyone point out what rule I've broken, here?
So, trying to create a state that increases the enemies MHP, and scales their health up to match the new max. The general thought is that I'll convert it to a percentage, scale it, and then.. Well, haven't figured that bit out yet. Anyway! The first bit causes a crash, as an array can't be coerced into a fixnum.



$game_variables[104].to_f = ($game_troop.members[0].hp.to_f / $game_troop.members[0].mhp.to_f)


Could anyone point out what rule I've broken, here?





Try...


$game_variables[104] = $game_troop.members[0].hp / $game_troop.members[0].mhp.to_f




Edit: I recall writing some code that does something similar to what you're trying to accomplish. It was meant to function with any changes to MHP, MMP, or MTP caused by states or equipment, so it might not be exactly what you're looking for, but I'll post it here just in case.


Plug & Play

Spoiler
Code:
#==============================================================================
# ** Game_BattlerBase
#------------------------------------------------------------------------------
#  This base class handles battlers. It mainly contains methods for calculating
# parameters. It is used as a super class of the Game_Battler class.
#==============================================================================
class Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Overwrite Method: Get Percentage of TP
  #--------------------------------------------------------------------------
  def tp_rate
    @tp / max_tp.to_f
  end # tp_rate
end # Game_BattlerBase
#==============================================================================
# ** Game_Battler
#------------------------------------------------------------------------------
#  A battler class with methods for sprites and actions added. This class 
# is used as a super class of the Game_Actor class and Game_Enemy class.
#==============================================================================
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Alias Method: Apply Effect of Skill/Item
  #--------------------------------------------------------------------------
  alias :relative_effect_apply :item_effect_apply
  def item_effect_apply(user, item, effect)
    relative_array = [21, 22, 31, 32, 33, 34]
    ohp_rate, omp_rate, otp_rate = user.hp_rate, user.mp_rate, user.tp_rate
    relative_effect_apply(user, item, effect)
    return unless relative_array.include?(effect.code)
    user.hp = [user.mhp * ohp_rate, 0].max.round.to_i
    user.mp = [user.mmp * omp_rate, 0].max.round.to_i
    user.tp = [user.max_tp * otp_rate, 0].max.round.to_i
  end # add_new_state
end # Game_Battler
#==============================================================================
# ** Game_Actor
#------------------------------------------------------------------------------
#  This class handles actors. It is used within the Game_Actors class
# ($game_actors) and is also referenced from the Game_Party class ($game_party).
#==============================================================================
class Game_Actor < Game_Battler
  #--------------------------------------------------------------------------
  # * Alias Method: Change Equipment
  #     slot_id:  Equipment slot ID
  #     item:    Weapon/armor (remove equipment if nil)
  #--------------------------------------------------------------------------
  alias :relative_change_equip :change_equip
  def change_equip(slot_id, item)
    ohp_rate, omp_rate, otp_rate = hp_rate, mp_rate, tp_rate
    relative_change_equip(slot_id, item)
    @hp = [mhp * ohp_rate, 0].max.round.to_i
    @mp = [mmp * omp_rate, 0].max.round.to_i
    @tp = [max_tp * otp_rate, 0].max.round.to_i
  end # change_equip
  #--------------------------------------------------------------------------
  # * Alias Method: Forcibly Change Equipment
  #     slot_id:  Equipment slot ID
  #     item:     Weapon/armor (remove equipment if nil)
  #--------------------------------------------------------------------------
  alias :relative_force_equip :force_change_equip
  def force_change_equip(slot_id, item)
    ohp_rate, omp_rate, otp_rate = hp_rate, mp_rate, tp_rate
    relative_force_equip(slot_id, item)
    @hp = [mhp * ohp_rate, 0].max.round.to_i
    @mp = [mmp * omp_rate, 0].max.round.to_i
    @tp = [max_tp * otp_rate, 0].max.round.to_i
  end # force_change_equip
end # Game_Actor
Thank you very much @Rinobi! You've been quite helpful 🙂
I've got a minor issue. In my battles when an enemy hit my actor the damage is not show on the actors health bar until after all the attacks are complete. So damage is only show at the end of the round, not as the battle plays out.


Victor sant animated battle script.


https://drive.google.com/file/d/0B0cSp2lASM9wdjMwNEMtMFlkR1k/view


I have yanfly Battle engine https://dl.dropboxusercontent.com/u/17078211/Scripts/YEA/Ace_Battle_Engine.rb


as well. 


testing shows that removing Yanfly battle engine fixes the issue or removing victor animated battle fixes the issue.


There must be something that needs to be fixed with either one to correct this. I'm working on it and will post my results if I'm successful.


*update - FIXED. Commented out line 3154   def refresh_status; return; end
I've been having a hangup in something I'm writing for VX Ace, in that I need a handful of instance variables to be created when the Input module is. Unfortunately, any and all documentation of the Input Module's source has been scattered to the sands of time.
 


Because I've been learning from the bottom up, I can't quite put my finger on how an instanced variable in a Module is created. My only findings on google have been like this:
4ff60432e4.png


This leads me to believe that the best practice is... this?


module Input
@variable_a = false
@variable_b = true

def self.example
@variable_a = !@variable_a
end
end


Which is bizarre, since any other code seems to throw exceptions when it's found outside of a module's methods. Why would variable declarations be any exception? Furthermore, is it possible to define variables in this way when re-opening the module in another script, or do you have to find and alias whatever method initializes the module?

Any advice welcome.
Most probably because it is variable declaration with direct setting of value. I've yet to see a language that doesn't allow doing that outside of a method/function. 


but you can always do this if you're so bothered by it


module Input
def self.set_variable_a(value)
@variable_a = value
end

#this will probably error though unless you make sure you've set variable_a to a value at least once before this
#method is called
def self.get_variable_a
return @variable_a
end
end

or this way so that there's only one method for both get and set

module Input
def self.variable_a(value=nil)
@variable_a = value if value != nil
return @variable_a
end
end

For "get" you just do Input.variable_a for "set" you do Input.variable_a(true/false)

for a safer return code you can use

return @variable_a == nil ? false : @variable_a

this way if @variable_a is not yet defined it will just return false




What do you mean by re-opening the module in another script?
Using Yanfly's Battle Engine for VX Ace.  Is there a way to scale down the actor graphic picture?  


I'm guessing probably not, so is there a way to have it draw the character sprite instead of the face sprite?


I found this in the script code:


  def draw_face(face_name, face_index, dx, dy, enabled = true)
    bitmap = Cache.face(face_name)
    fx = [(96 - item_rect(0).width + 1) / 2, 0].max
    fy = face_index / 4 * 96 + 2
    fw = [item_rect(0).width - 4, 92].min
    rect = Rect.new(fx, fy, fw, 92)
    rect = Rect.new(face_index % 4 * 96 + fx, fy, fw, 92)
    contents.blt(dx, dy, bitmap, rect, enabled ? 255 : translucent_alpha)
    bitmap.dispose
  end


And I'm guessing the "bitmap = Cache.face(face_name)" is what I need to change.  


I just can't figure out what to change it to. ^_^;;
To do it without affecting anything else, you'd need to duplicate that method and give it a different name, then make the battle engine script call your new method instead.  Then I'd create scaled down versions of the images manually (don't do it through code - they don't look good and it's heavier processing), add a '2' to the end of the file name so it doesn't alter the originals, and change the new method to use the new name and smaller dimensions.
To do it without affecting anything else, you'd need to duplicate that method and give it a different name, then make the battle engine script call your new method instead.  Then I'd create scaled down versions of the images manually (don't do it through code - they don't look good and it's heavier processing), add a '2' to the end of the file name so it doesn't alter the originals, and change the new method to use the new name and smaller dimensions.



I figured out how to put up the character sprite - and it had nothing to do with the line I'd thought it did. ^_^;;  I had to change line 1804 from "draw_actor_face" to "draw_actor_graphic."


I'll see if I ever have the motivation to make an entirely new coding method for the mini-portraits. X_x
Hey everyone,

Is there a way to export the item IDs into an array with a script?
@J_C Well, database items are already in an array of objects. The IDs are actually their position in that array. It would a little redundant but it can be done in a line or two.

Code:
item_array = Array.new
$data_items.each {|item| next unless item ; item_array.push(item.id)}
Or just do this for single line

item_array = $data_items.collect {|item| item.id}

Original thread: https://forums.rpgmakerweb.com/threads/ruby-rgssx-questions-that-dont-deserve-their-own-thread.150/