Save-Point
General Methods XP VX & ACE - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Material Development (https://www.save-point.org/forum-8.html)
+--- Forum: Scripts Database (https://www.save-point.org/forum-39.html)
+--- Thread: General Methods XP VX & ACE (/thread-7562.html)

Pages: 1 2


General Methods XP VX & ACE - kyonides - 10-23-2019

General Methods

Methods for XP

Introduction

I am posting here a bunch of methods that I thought they have appeared a several times in my scripts or they are methods that seemed to be useful under certain circumstances, but are not good enough to become part of a specific script.

Warning!

This group of methods might not be useful for the average game developer that lacks any scripting skills.

Script Calls

rand_range(any_range)
any_range.random
Returns a random number that is included in the given range. A Range object is a number range with an initial value (the lowest) and a final value (the highest). It's something like using an entire array full of consecutive numbers.
I named the second script call random because I shouldn't overwrite rand. (The script call actually needs rand!)
Using the second option might be your best choice because it doesn't create a global method which would use more memory. Confused

$game_party.leader
Returns the current party's leader (Aluxes!?)

$game_party.subleader
Returns the current party's subleader (Basil!?)

$game_party.actors_by_hp
$game_party.actors_by_sp
$game_party.actors_by_pdef
$game_party.actors_by_mdef
$game_party.actors_by_eva
$game_party.actors_by_agi
$game_party.actors_by_dex
$game_party.actors_by_strength
$game_party.actors_by_mana
Returns the actors sorted by the chosen stat (Descending Order)

$game_player.visible?
Returns if the player's sprite is not transparent

$game_map.events[Number].visible?
Returns if the event's sprite is not transparent

$game_map.events[Number].name
Returns the chosen event's name

You can also check if a battler is hero or an enemy in a new way by calling its brand new kind method that will return either an :actor or :enemy symbol if called. This could be useful in any Game_Battler or Game_Actor or Game_Enemy or Scene_Battle script.

Script

Code:
# * General Methods XP
def rand_range(range)
 rand(4) % 2 == 0 ? -rand(range.first) : rand(range.last)
end

class Range
 def random() rand(4) % 2 == 0 ? -rand(self.first) : rand(self.last) end
end

class Game_Actor
  def kind() :actor end
end

class Game_Enemy
  def kind() :enemy end
end

class Game_Party
 def leader() @actors[0] end
 def subleader() @actors[1] end
 def actors_by_hp() @actors.sort{|a,b| b.hp <=> a.hp } end
 def actors_by_sp() @actors.sort{|a,b| b.sp <=> a.sp } end
 def actors_by_pdef() @actors.sort{|a,b| b.pdef <=> a.pdef } end
 def actors_by_mdef() @actors.sort{|a,b| b.mdef <=> a.mdef } end
 def actors_by_eva() @actors.sort{|a,b| b.eva <=> a.eva } end
 def actors_by_agi() @actors.sort{|a,b| b.agi <=> a.agi } end
 def actors_by_dex() @actors.sort{|a,b| b.dex <=> a.dex } end
 def actors_by_strength
   @actors.sort{|a,b| b.atk + b.str <=> a.atk + a.str }
 end

 def actors_by_mana
   @actors.sort{|a,b| b.atk + b.int <=> a.atk + a.int }
 end
end

class Game_Character
 def visible?() !@transparent end
end

class Game_Event
 def name() @event.name end
end

Terms & Conditions

Free for use in commercial and non commercial projects.
You are free to post any methods here you consider that can be useful for scripting power users and actual scripters.


RE: General Methods XP - kyonides - 07-06-2020

Set the Current Map's Encounter Steps

Usually you can either enable or disable map encounters. This time I'm gonna show up here to let you know how to change your current map's monster encounter steps.

This might work on any RPG Maker based on Ruby, guys! Shocked

1. Just add the scriptlet below to your script editor.

Code:
class Game_System
  alias :mes_gm_sys_init :initialize
  def initialize
    mes_gm_sys_init
    @map_encounter_steps = {}
  end
 attr_reader :map_encounter_steps
end

class Game_Map
  alias :mes_gm_map_setup :setup
  def setup(map_id)
    mes_gm_map_setup(map_id)
    steps = $game_system.map_encounter_steps[map_id]
    @map.encounter_step = steps if steps
  end

 def encounter_step=(steps)
   $game_system.map_encounter_steps[@map_id] = steps
   @map.encounter_step = steps
 end
end

2. Call it to set the new steps value. Keep in mind it should be an integer number greater than 0.

$game_map.encounter_step = NewValue

That's it! Two Thumbs Up!


RE: General Methods XP - kyonides - 08-02-2020

General Methods for XP + VX + ACE

Sometimes people show up complaining about their inability to easily setup self switches in a script call event command.
Well, here's a quick way to get its current value or set a new one.
Since we're using scripts here, you can use any letter you can think off as an self switch ID element.

Keep in mind the default implementation requires you to provide an array of 3 elements as a key to find that self switch.
This scriptlet allows you to only set those elements you really care about at any given time.

For XP

Code:
class Interpreter
 def self_switch(char, ev_id=0)
   ev_id = @event_id if ev_id == 0
   $game_self_switches[[@map_id, ev_id, char]]
 end

 def set_self_switch(char, value, ev_id=0)
   ev_id = @event_id if ev_id == 0
   $game_self_switches[[@map_id, ev_id, char]] = value
 end

 def toggle_switch(char, ev_id=0)
   ev_id = @event_id if ev_id == 0
   key = [@map_id, ev_id, char]
   $game_self_switches[key] = !$game_self_switches[key]
 end
end

For VX or ACE

Code:
class Game_Interpreter
 def self_switch(char, ev_id=0)
   ev_id = @event_id if ev_id == 0
   $game_self_switches[[@map_id, ev_id, char]]
 end

 def set_self_switch(char, value, ev_id=0)
   ev_id = @event_id if ev_id == 0
   $game_self_switches[[@map_id, ev_id, char]] = value
 end

 def toggle_switch(char, ev_id=0)
   ev_id = @event_id if ev_id == 0
   key = [@map_id, ev_id, char]
   $game_self_switches[key] = !$game_self_switches[key]
 end
end

To get its current state:

self_switch('C')

To set it to a different state:

set_self_switch('C', false)

To toggle it:

toggle_switch('C")
toggle_switch('C", 21)


Add another parameter at the end, in both cases, to select a different map event.
The advantage is that you no longer need to include thisEvent, thus saving valuable space there. Plus you get the chance to use it for any map event...


RE: General Methods XP - kyonides - 08-22-2020

Convert Your Current Temperature
to Celsius or Fahrenheit
with Ruby and RGSS!

To make that ever happen, you'd only need to copy and paste the following scriptlet.

Code:
module Termometer
  extend self
  def fahrenheit_to_celsius(f) 5 * (f - 32) / 9.0 end
  def celsius_to_fahrenheit(c) c * 9.0 / 5 + 32 end
  alias :f2c :fahrenheit_to_celsius
  alias :c2f :celsius_to_fahrenheit
end

Then you call Termometer.f2c(Degrees) or Termometer.c2f(Degrees) to get the corresponding conversion of your current temperature.



Kilometers and Miles

Yes, you can do the same with kilometers (km) and miles (m).

Code:
module Distance
  extend self
  def km_to_miles(km) km / 1.60934 end
  def miles_to_km(m) m * 1.60934 end
  alias :km2miles :km_to_miles
  alias :miles2km :miles_to_km
end

The script calls are:
Distance.km_to_miles(KM)
Distance.miles_to_km(MILES)


RE: General Methods XP - kyonides - 12-31-2021

Bitmaps and Color Inversion

On another board people were working with a stripped down version of one of LittleDrago's scripts that featured the color inversion method. I disagreed with his implementation and the minimalistic version Roninator has published so I felt the need to code it the following way:

Code:
# * Color Inversion Script
#   2021-12-31

# - Warning! - #
# Inverting a bitmap's colors is a time consuming process.

# * Script Calls * #

# - Invert a bitmap entirely!
# some_sprite.bitmap.invert_colors!
# - Invert a window's text colors!
# some_window.contents.invert_colors!
# - Invert a windowskin's colors! # Use a duplicate of the windowskin!
# windowskin.invert_colors!
# - Temporary Color Change
# some_color.invert
# - Permanent Color Change
# some_color.invert!

class Color
  def invert
    Color.new(255 - red, 255 - green, 255 - blue, alpha)
  end

  def invert!
    self.red = 255 - red
    self.green = 255 - green
    self.blue = 255 - blue
    self
  end
end

class Bitmap
  def invert_colors!
    width.times do |x|
      height.times do |y|
        clr = get_pixel(x, y)
        set_pixel(x, y, clr.invert)
      end
    end
  end
end

There's also a need to make a few fixes in his draw_item related methods.

Warning! You should NOT change a bitmap's colors directly!

It's always better to work with a duplicate or dup.

Why? Because bitmaps get cached by the engine.

Thus you might need to perform a color inversion every time you draw any face, no matter if the hero is dead or alive. Even if there's a slight possibility this might happen, it's better to avoid this color inversion madness from the very beginning.

VX ACE Window_MenuStatus Modification

Code:
class Window_MenuStatus
  def draw_item(index)
    actor = $game_party.members[index]
    enabled = $game_party.battle_members.include?(actor)
    rect = item_rect(index)
    draw_item_background(index)
    if actor.dead?
      draw_actor_face_invert(actor, rect.x + 1, rect.y + 1, enabled)
    else
      draw_actor_face(actor, rect.x + 1, rect.y + 1, enabled)
    end
    draw_actor_simple_status(actor, rect.x + 108, rect.y + line_height / 2)
  end

  def draw_actor_face_invert(actor, x, y, enabled = true)
    draw_face_invert(actor.face_name, actor.face_index, x, y, enabled)
  end

  def draw_face_invert(face_name, face_index, x, y, enabled = true)
    bitmap = Cache.face(face_name).dup
    bitmap.invert_colors!
    rect = Rect.new(face_index % 4 * 96, face_index / 4 * 96, 96, 96)
    contents.blt(x, y, bitmap, rect, enabled ? 255 : translucent_alpha)
    bitmap.dispose
  end
end

Keep in mind RMXP never sported any faces at all. Laughing
Happy with a sweat This means you'd need to include a custom script to make it possible in XP.


RE: General Methods XP VX & ACE - kyonides - 03-30-2022

Memorize Events at Will!

Winking Just make sure they will not come back to slap the hell out of your face. Laughing

XP Version

Code:
class Interpreter
  alias :kyon_int_tool_init :initialize
  alias :kyon_int_tool_setup :setup
  def initialize(depth=0, main=false)
    @memo_events = {} if main
    kyon_int_tool_init(depth, main)
  end

  def setup(list, event_id)
    kyon_int_tool_setup(list, event_id)
    @memo_events[@map_id] ||= {}
  end
  def this_map_memo_events() @memo_events[@map_id] end
  attr_reader :memo_events
end

ACE Version

Code:
class Game_Map
  alias :kyon_map_tool_init :initialize
  alias :kyon_map_tool_setup :setup
  def initialize
    kyon_map_tool_init
    @interpreter.setup_memo_events
  end

  def setup(map_id)
    kyon_map_tool_setup(map_id)
    @interpreter.setup_map_memo_events(map_id)
  end
  attr_reader :memo_events
end

class Interpreter
  def setup_memo_events() @memo_events ||= {} end
  def setup_map_memo_events(map_id)
    @map_id = map_id
    @memo_events[@map_id] ||= {}
  end
  def map_memo_events() @memo_events[@map_id] end
  def clear_memo_events() @memo_events.clear end
  def clear_map_memo_events() @memo_events[@map_id].clear end
end

If you are going to use these events from a custom script, use the following calls:

XP Calls
$game_system.map_interpreter.memo_events[MapID][EventID]
$game_system.map_interpreter.this_map_memo_events[EventID]

ACE Calls
$game_map.interpreter.memo_events[MapID][EventID]
$game_map.interpreter.this_map_memo_events[EventID]

Else if you are editing script commands, use the ones below:

Any Version
memo_events[MapID][EventID]
this_map_memo_events[EventID]
clear_memo_events
clear_map_memo_events



RE: General Methods XP VX & ACE - kyonides - 11-11-2022

This post includes two new features for Game_Variables and Game_Switches respectively.

Manually Calculate with Multiple Game_Variables!

Happy with a sweat I know that the event commands allow you to add two game variables at a time.

Even so, don't you think Thinking that it would be convenient to add or subtract or multiply or even divide by many game variables in a row?

Shocked Now you can do it!

Code:
# * Game Variables Addons

class Game_Variables
  def data(var_id) @data[var_id] || 0 end

  def add(*ids)
    ids.inject(0){|n, vid| n + data(vid) }
  end

  def subtract(*ids)
    ids.inject(0){|n, vid| n - data(vid) }
  end

  def multiply(*ids)
    ids.inject(0){|n, vid| n * data(vid) }
  end

  def divide(*ids)
    ids.inject(0){|n, vid| n / data(vid) }
  end
  private :data
end

The new methods for $game_variables are:
  • add
  • subtract
  • multiply
  • divide
You can pass them as many ID's or positions as you wish! Grinning


Toggle Your Game_Switches!

Code:
# * Game_Switches Addon

class Game_Switches
  def toggle!(*ids)
    ids.each{|sid| @data[sid] = !@data[sid] }
  end
end

Just call the method:

$game_switches.toggle!(ID1, ID2, etc.)

...and those Game_Switches will get their inverted boolean value in no time! Shocked


RE: General Methods XP VX & ACE - kyonides - 11-17-2022

Process Game.ini Data
 
Usually, you would need to set a brand new string or constant to your game's official name by hand. Then you would call it every single time your game is loaded or reloaded.
Now that's no longer necessary! Grinning
Include this scriptlet and you will only need to setup a single constant, namely INI_FILENAME and that's it! Two Thumbs Up!

Code:
# * Game.ini Data Extractor

module Game
  INI_FILENAME = "Game"
  DATA = {}
  def self.process_ini
    lines = File.readlines(INI_FILENAME + ".ini")
    lines.shift
    lines.size.times do
      key, value = lines.shift.split("=")
      next unless key
      DATA[key] = value.chomp || ""
    end
  end
  process_ini
end

From now on, you can directly access Game::DATA["Title"] or any other variable you may have set in your Game.ini file. Grinning


RE: General Methods XP VX & ACE - kyonides - 12-02-2022

Is the Player Behind You, NPC?

You only need a snippet to figure it out! Shocked

Simply call:

behind_event?(EventID)

And you'll soon know if your character is just about to get rid of some nasty tyrant...
Or some dissenter a la Belarus instead. Confused

XP Version

Code:
class Interpreter
  def behind_event?(event_id)
    event = $game_map.events[event_id]
    return nil if $game_player.direction != event.direction
    if $game_player.y == event.y
      distance = $game_player.x - event.x
      return distance == 1  if player_dir == 4
      return distance == -1 if player_dir == 6
    elsif $game_player.x == event.x
      distance = $game_player.y - event.y
      return distance == 1  if player_dir == 2
      return distance == -1 if player_dir == 8
    end
    nil
  end
end


VX & ACE Version

Code:
class Game_Interpreter
  def behind_event?(event_id)
    event = $game_map.events[event_id]
    return nil if $game_player.direction != event.direction
    if $game_player.y == event.y
      distance = $game_player.x - event.x
      return distance == 1  if player_dir == 4
      return distance == -1 if player_dir == 6
    elsif $game_player.x == event.x
      distance = $game_player.y - event.y
      return distance == 1  if player_dir == 2
      return distance == -1 if player_dir == 8
    end
    nil
  end
end



RE: General Methods XP VX & ACE - kyonides - 03-08-2023

How to Get Rid of Those Weird Invisible Characters

Let Me Guess Let me guess... your username or a directory has a multibyte character or space inbetween that shouldn't be displayed on screen and now you're having this issue right?

Let us think this has been caused by a nasty character found in the environmental variable HOME.

Since we are working with Ruby 1.9 or below if talking about the RGSS based engines, this might only be solved by applying one of the following solutions:

Method #1 - Ruby 1.9
Code:
env_home = "#ENV['HOME']}/AppData/Local/game"
APPDATA = env_home.force_encoding("ascii-8bit").gsub(/\W+/,"")

Method #2 - Ruby 1.8 or 1.9
Code:
env_home = "#ENV['HOME']}/AppData/Local/game"
APPDATA = env_home.unpack("A*").to_s.gsub(/\\x\w{2}|\W+/,"")

Replace "" (nothing) with " " (a single space) if it's supposed to be a simple space there.
In this case, the order of the replacements of the matched regular expressions do matter here.

Other solutions might tell you that you need to use iconv, but it'd be way better for the average game developer to keep it as simple as possible as shown above.