Save-Point
Ruby, RGSS & General Code Discussion - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Games Development (https://www.save-point.org/forum-4.html)
+--- Forum: Code Support (https://www.save-point.org/forum-20.html)
+--- Thread: Ruby, RGSS & General Code Discussion (/thread-4438.html)



Ruby, RGSS & General Code Discussion - Kain Nobel - 11-14-2012

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.


RE: Ruby, RGSS & General Code Discussion - Narzew - 11-15-2012

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.


RE: Ruby, RGSS & General Code Discussion - KasperKalamity - 11-15-2012

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.


RE: Ruby, RGSS & General Code Discussion - Narzew - 11-15-2012

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



RE: Ruby, RGSS & General Code Discussion - Kain Nobel - 12-21-2012

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?


RE: Ruby, RGSS & General Code Discussion - kyonides - 12-21-2012

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.


RE: Ruby, RGSS & General Code Discussion - MechanicalPen - 12-22-2012

(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.