Ruby, RGSS & General Code Discussion
#1
Hello and good day!

I'm starting this thread as a sort of general discussion, but instead of flashing memes and making dick jokes we're going to discuss code (how fun!) Did you have a silly problem that, according to you, didn't make any damn sense? Did you figure it out afterwards and go "Duh"? Heck, do you have useful code snippets that you want to share, but don't feel like posting a Script / Support thread about it? That's what this thread is for!

Anywho, I'll share my silly code story of the day...

So I was testing Trickster's ATB when I kept coming across a silly error related to something that was in fact defined. Every time an actor levels up, I get this NoMethodError for level_up which is defined in Window_BattleStatus. "Strange," I said to the resident werewolf as he stared back at me, both of us puzzled. That's when I realized; I changed Window_BattleStatus from inheriting from Window_Base to inheriting from Window_Selectable, that way a cursor is over whomever is currently selecting actions.

"What does that have to do with anything," you ask?

Quite simply this...

Code:
# First, we make two parent classes, ParentA and ParentB.
class ParentA
end

class ParentB
end

# Now Child inherits from ParentA with the 'blah' method defined.
class Child < ParentA
  def blah
    p 'blah'
  end
end

# Now we create the Child object and call the 'blah' method.
# This should result in no error and print 'blah'.
c = Child.new
c.blah

# Changing inheritance from ParentA to ParentB cancels the 'blah' method.
class Child < ParentB
end

# Now we create the Child object and call the 'blah' method.
# This results in an error, all defined methods have been cancelled.
c = Child.new
c.blah

Lesson Learned: Changing the inheritance of a predefined class in Ruby will cancel all of its unique methods, so you'll have to copy them over or re-write them.
[Image: Button-BOTB.png]
[Image: Save-Point.gif][Image: Button-You-Tube2.png][Image: Button-Sound-Cloud2.png][Image: Button-Audio-Mack2.png]
[Image: LS-Banner.gif]
NEW ALBUM OUT NOW!

Reply }
#2
I think the all RGSS code should be open source. There are some scripts that require DLL or executable to work. The custom DLL's should have included source. The scripter reading other scripts, learns how to making scripts. Why people uses DLL in RGSS scripts? The first thing is - to hide source.

Some coding standards prefered by me :

98 % of Rpg Maker users edits RGSS Default Interpreter Scripts. This is not good idea.
The better is make new * Mods script and paste in it modified segments.

For ex.

This is Window Playtime class.
Code:
class Window_PlayTime < Window_Base
  def initialize
    super(0, 0, 160, 96)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.contents.font.name = $font.get($actual_font, 0)
    self.contents.font.size = $font.get($actual_font, 1)
    refresh
  end
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 120, 32, "Play Time")
    @total_sec = Graphics.frame_count / Graphics.frame_rate
    hour = @total_sec / 60 / 60
    min = @total_sec / 60 % 60
    sec = @total_sec % 60
    text = sprintf("d:d:d", hour, min, sec)
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 32, 120, 32, text, 2)
  end
  def update
    super
    if Graphics.frame_count / Graphics.frame_rate != @total_sec
      refresh
    end
  end
end

You want to edit a part of this class to change the text "Play Time" at the "Real Time" and you want to make visible your computer, real hour instead of PlayTime.
In * Mods :
Copy object you want to edit and edit features in the mods class.
Using this method makes code clean :)

Code:
class Window_PlayTime < Window_Base
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 120, 32, "Real Time")
    text = "#{Time.now.hour} : #{Time.now.min} : #{Time.now.sec}"
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 32, 120, 32, text, 2)
  end
end

That's all my suggestions.
Skills: Android, Ruby, PHP, Linux
Reply }
#3
Yes, this:

Content Hidden


i have no idea how to use this script, and the person who sent it to me hasn't responded. it's supposed to log all the resources used in order to help keep the filesize down.
Reply }
#4
Simple paste script like other scripts.
The script will automatically log used resources ;)
That's all :)

Edit the script, you have syntax error in first line.

First line is :
Code:
module RPG module Cache

The first lines should be :

Code:
module RPG
module Cache
Skills: Android, Ruby, PHP, Linux
Reply }
#5
I found an interesting little tidbit today! Actually I've found it before and forgot about it, but since I've been reminded...

Game_Battler#add_state

There
is a way for a method to call itself within its own definition,
but that is via the use of alias or alias_method. A child
method can call the definition of a parent's method by using a
super, a parent method can call other methods defined by it's
child class even if its not defined within its own class, but a method calling itself
just seems odd to me.

A freshly defined method shouldn't be able to call itself within it's own definition, should it?
[Image: Button-BOTB.png]
[Image: Save-Point.gif][Image: Button-You-Tube2.png][Image: Button-Sound-Cloud2.png][Image: Button-Audio-Mack2.png]
[Image: LS-Banner.gif]
NEW ALBUM OUT NOW!

Reply }
#6
My Dynamic Leveling Scriptlet

Code:
module DynamicLeveling
  EXP_PERCENT = 0.1
  def self.dynamic_level_exp(enemy, actor_level)
    lvl_bonus_penalty = enemy.level - actor_level
    exp = (enemy.exp * EXP_PERCENT).round
    total_exp = [1, enemy.exp + (lvl_bonus_penalty * exp)].max
  end
end

class Game_Enemy
  attr_accessor :level
end

Let's say you want an actor to get some experience from a couple of poor, abused ghosts.

actor.exp = DynamicLeveling.dynamic_level_exp(enemy, actor_level)

The catch here would be the need to find a way to assign them a specific level either by using an array, a hash or something else.

Oh, oh, I found some other weird stuff found in the default scripts.

Code:
def all_dead?
    # If number of party members is 0
    if $game_party.actors.size == 0
      return false
    end
    # If an actor is in the party with 0 or more HP
    for actor in @actors
      if actor.hp > 0
        return false
      end
    end
    # All members dead
    return true
  end
  #--------------------------------------------------------------------------
  # * Slip Damage Check (for map)
  #--------------------------------------------------------------------------
  def check_map_slip_damage
    for actor in @actors
      if actor.hp > 0 and actor.slip_damage?
        actor.hp -= [actor.maxhp / 100, 1].max
        if actor.hp == 0
          $game_system.se_play($data_system.actor_collapse_se)
        end
        $game_screen.start_flash(Color.new(255,0,0,128), 4)
        $game_temp.gameover = $game_party.all_dead?
      end
    end
  end

Note that this comes from Game_Party class an even so it calls itself with an external flag, i.e. $game_party.actors.size and $game_party.all_dead?, I don't really know why they thought it would ever make any sense to achieve such a terrible and useless feat.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }
#7
(12-21-2012, 11:25 AM)Kain Nobel Wrote: I found an interesting little tidbit today! Actually I've found it before and forgot about it, but since I've been reminded...

Game_Battler#add_state

There
is a way for a method to call itself within its own definition,
but that is via the use of alias or alias_method. A child
method can call the definition of a parent's method by using a
super, a parent method can call other methods defined by it's
child class even if its not defined within its own class, but a method calling itself
just seems odd to me.

A freshly defined method shouldn't be able to call itself within it's own definition, should it?
That is actually a misconception. In RGSS, you can indeed have a method call itself recursively, just as you can in regular old Ruby. You only get the "stack level too deep" error if you actually make a stack that is too deep. Unfortunately for RGSS I remember this stack being really small (200 something if I remember correctly) and so recursive calls are not very useful when scripting. In this case, it is VERY unlikely states will be nested in a way to exceed the stack so this is an okay thing to do.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Possible means to speed up Bitmap TONE code? DerVVulfman 7 3,256 06-11-2023, 08:22 PM
Last Post: kyonides
   RGSS scripting dissections and explinations Siletrea 97 107,022 03-18-2019, 06:46 AM
Last Post: Siletrea
   Help iwth script (RGSS Player crash) Whisper 3 8,630 06-17-2017, 05:03 PM
Last Post: Whisper
  How can I use the cmd of "require" in rgss superegp 2 5,998 11-03-2015, 06:16 AM
Last Post: kyonides
   Custom Game_Event code lags too much! MechanicalPen 4 6,752 06-27-2013, 11:22 PM
Last Post: MechanicalPen
Question  RGSS stoped to work Chaos17 5 7,856 02-14-2013, 05:13 PM
Last Post: DerVVulfman
   [Request] Tut. for RGSS Eldur 9 12,078 12-07-2012, 04:27 AM
Last Post: DerVVulfman
   [RUBY] Depack Thread Problem Narzew 1 4,448 07-20-2012, 01:16 PM
Last Post: Narzew
   [ASK-RGSS] Behemoth's CBS alike Getsuga_kawaii 0 4,286 04-29-2010, 03:07 PM
Last Post: Getsuga_kawaii
   Quick RGSS question - switches Ravenith 2 5,696 08-23-2009, 11:23 AM
Last Post: Ravenith



Users browsing this thread: 1 Guest(s)