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.

Scripter's corner : Snippets, Classes, Tips and more

Yeah, definately will be helpful... reminds me of the way of using arrays to decide between multiple arguments, while obviously the purpose is different (still, the array will save you the few milliseconds of processing that hashes eat up... while it's probably not the best coding style).
[rgss]def example(*args)
  if args.size == 2
    args.each{ |argument| p argument }
  else
    p 'more or less than two arguments have been passed'
  end
end
[/rgss]
Just for completeness' sake ;)
 
Actually, I've heard that hashes are faster than arrays in Ruby. It sort of makes sense when you think of all the different access styles from the array[] method, and how arrays might have to recreate the entire array at O(n) to resize whenever a value is added, while a hash only needs to compute the hash at O(1).
 
More or less completely unrelated, I put a fancy little snippet together that allows to round any Float to the nearest number on a base you can choose yourself. For example, let's say you want to round to the half numbers, choose a base of 0.5 - for example, 4.345674.round_to_nearest(0.5) will return 4.5.
The other two methods are for rounding up or down, respective to the .ceil and .floor methods.

[rgss]class Float < Numeric
 
  def round_to_nearest(base = 1.0)
    self - self % base + (self >= (self - self % base + base / 2.0) ? base : 0.0)
  end
 
  def floor_to_nearest(base = 1.0)
    self - self % base
  end
 
  def ceil_to_nearest(base = 1.0)
    self - self % base + base
  end
 
end
[/rgss]
If you don't set a parameter, they will give you a equivalent to Float.round (or .ceil or .floor), however without the option to round to certain floating point digits. It should therefore be a tad faster than the original .round, however I wouldn't suggest using it as a replacement for the default for compatibility and readability reasons.

Hope this will be useful to someone.
 
I wonder if anyone besides me ever does the little things anymore... but hey, at least someone does ;o

This time I got a custom z-level script for events, originating on a request from Peri that vanished by the time I'm writing this. Well, here you go:
[rgss]class Game_Event < Game_Character
  #--------------------------------------------------------------------------
  attr_reader :event
  #--------------------------------------------------------------------------
end
 
 
class Game_Character
  #--------------------------------------------------------------------------
  alias zleveladjust_screenz screen_z
  def screen_z(height = 0)
    if self.is_a?(Game_Event) && self.event.name[/z(\d{1,})/i] != nil && !@always_on_top
      z = self.event.name[/z(\d{1,})/i].gsub(/z/, '').to_i
      return z
    end
    zleveladjust_screenz(height = 0)
  end
  #--------------------------------------------------------------------------
end
[/rgss]

By putting for example z50 in the event name, you will set a custom z-level of 50 for the event, with the default being 200. You can actually do al kind of weird combinations for the event name, for example "lshgishgpz567holjsdn", which will set the z-level to 567. This might be good or not, but it's the easiest and leat limiting way.

Another thing I should mention is that the 'Always on Top' flag will overwrite the custom z-level setting and set the event to the default 999. If you don't flag it, however, you can set values higher than that to make events appear even over events flagged as 'Always on Top'. That might not seem logical, and is only intended for special appliances - you should stick to values from 0 to 998 normally.
 
That's way more complicated than it need to be. You don't need to create an attr_reader in Game_Event, then have the actual code in Game_Character, and then use an "if" to make sure its a Game_Event again. You should just keep the whole thing in Game_Event. After that, your doing the same RegEx, which are hard on performance, twice in a row, and it is overly complicated. And on top of all of that, the "height=0" in the alias call is not going to work. The code should look something like this:

[rgss]class Game_Event < Game_Character
 
  alias zleveladjust_screenz screen_z
  def screen_z(height = 0)
    @event.name =~ /[Zz]([0-9]+)/
    unless @always_on_top && $1 != nil
      return $1.to_i
    end
    return zleveladjust_screenz(height)
  end
end
[/rgss]
 
Yeah, that's indeed a lot simpler... the height thing I totally overlooked (and I don't think it gave me an error in the past... well, don't really have anything to test it at all, so... :/ ), and to be honest, I totally confused the order of the classes and thought Game_Event was the superclass of Game_Character... >> Well, it was late, I suppose... >>"

Thanks for those hints there.
 
It's been awhile since I posted a script. So have a quickie.

Code:
# avarisc's Tack (a.k.a. auto-alias)                             v1.0 - 7/31/14

################################################################################

class Object

    def tack_before(symbol, id) # requires code block

        alias_method(("tack_before_" + symbol.to_s + "_" + id).to_sym, symbol) unless method_defined?(("tack_before_" + symbol.to_s  + "_" + id).to_sym)

        send :define_method, symbol do

            send ("tack_before_" + symbol.to_s + "_" + id).to_sym

            yield

        end

    end

    def tack_after(symbol, id) # requires code block

        alias_method(("tack_after_" + symbol.to_s + "_" + id).to_sym, symbol) unless method_defined?(("tack_before_" + symbol.to_s  + "_" + id).to_sym)

        send :define_method, symbol do

      yield

            send ("tack_after_" + symbol.to_s + "_" + id).to_sym

        end

    end

end

I get tired of juggling/managing aliases. I have my own alternatives in larger projects, but for tinkering in a clean sandbox, I always end up reimplementing it. This reduces alias tacking to 1 line.
Usage example:

Code:
 

# automatically call Map_Awesomizer.awesomize every time after spriteset map initializes

class Spriteset_Map

  tack_after :initialize, "my_awesome_script" do Map_Awesomizer.awesomize() end

end


If you don't get what this does, the miniscript allows the example to do exactly the same thing as this:

Code:
 

# automatically call Map_Awesomizer.awesomize every time after spriteset map initializes

class Spriteset_Map

  alias_method(:my_awesome_script_initialize, :initialize)

  def initialize

    my_awesome_script_initialize

    Map_Awesomizer.awesomize

  end

end

 

Not the biggest change in the world. But if your code tends to squirm and tendril its way across all the various classes requiring a few dozen or more hooks, this will trim the code size significantly. And its faster.
 

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