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

If I alias a method in a 'child class' where the method is only defined in the 'parent class', would this have any negative consequences ?

Would I be better to simply call 'super' rather than call the aliased method?

Actually... Now that I think about it, having an extra aliased method floating around cant be a good thing... I will just stick with super on this occasion.

While I am asking though, are there any other 'more deadly' negative impacts of aliasing a method as previously described?
Hey there!

It's not that big of a deal. Either would be fine, although, it would be a "good programming practice" to use super instead of alias as much you can. Don't let those good programing practices take all over though, heh. But, yes, it would be more standard and a more predictable solution.

However, if you may need to alias it several times in your code, you can either make sure the one that uses super is executed first, always, or just use alias for all if you think your code order may change in the future (either within the file or moving the file order).

Lastly, but not least, if a script uses alias on that method and is inserted before your code, you will overwrite it with your non aliased definition of that method. Super, in that case, will call the parent method, not the aliased method by the script, and it will cause unwanted behavior or even crashes.

So... It comes down to the situation 😛 . I personally use super as much as I can, only then use alias. But that's just me!

Hope that clears the panorama 😀
Just curious, if this code:

def create_plane(plane_source, x, y, z) @plane= Sprite.new @plane.bitmap = Cache.picture(plane_source) @plane.x = x @plane.y = y @plane.z = zendcan normally call something like:

create_plane("Hwa", 0, 0, 0)This only applies to a certain time where you dispose the plane and create another plane, as to they are being held with the same instance variable. I am just curious, is there a way to ignore the parameters or add the parameters so even calling such thing as

create_plane("Hwa", 0, 0, 0)create_plane("Jeh", 0, 0, 0)create_plane("Hwajeh", 0, 0, 0)at the same time would call Hwa, Jeh and Hwajeh image, without getting Hwa and Jeh overwritten originally if you run the code? I was thinking of adding more plane names, but of course, that would be an error on the arguments. So say for example you want to call another create_plane at some point and time, how can you create just one universal class to hold them all?
@Soulpour - Dont fully understand what your meaning.

Like, if you wanted multiple instances of @plane, why not just make it some kind of array ?

Maybe something like...

def create_plane(plane_source, x, y, z)  @plane = [] unless @plane && @plane.kind_of?(Array)  newplane = Sprite.new(@viewport)  newplane.bitmap = Cache.picture(plane_source)  newplane.x = x  newplane.y = y  newplane.z = z  @plane << newplaneendLike I said though, not sure I fully understand your query 🙂

Edit:

Seen you mention about a universal class to hold all these images...

Wouldn't that be the exact same as scripts like Spriteset_Map ?
My query is that, say for example I wanted to make multiple backgrounds on my Scene_Menu. Normally what I always do is to create different instances of a plane however, separately, which makes the code longer and gets confusing to maintain. For example:

def create_plane @plane= Sprite.new @plane.bitmap = Cache.picture("Hwa") @plane.x = 0 @plane.y = 0 @plane.z = 0enddef create_plane2 @plane= Sprite.new @plane.bitmap = Cache.picture("Jeh") @plane.x = 0 @plane.y = 0 @plane.z = 0endand of course, different dispose functions for all of those. My first set of code on my question, using the arguments, allows me to call the images via passing the arguments...right? However, when I call each of my Hwa, Jeh and Hwajeh, what happens is that Jeh replaces Hwa, and Hwajeh replaces Hwajeh, since they all receive the same instance, @plane_source. So I was wondering how to call them universally without repeating a method separately just to call something like for example, 45 images.
Maybe having a look at how i wrote my unlimited scene backgrounds script will give some insight on one way you could handle all your images.

http://dekitarpg.wordpress.com/2013/03/27/d13x-scene-backgrounds/

I essentially do as mentioned previously and have one array defining all the background settings, image name and the like and then create an array of images which use a new BG class (derived from Plane class).

Maybe that script will help some?
I'll try looking into it. I just want to reduce using different methods, as to its hard to maintain especially when you're like me who wants to use almost 100 images on an animated menu :\
maybe if you put the planes in viewports of different z values?

wait, no, the problem is that you are using the same variable to store your three planes...
Indeed, you are re-defining the Plane, and then setting a different graphic to it when you execute both methods. The last you execute will prevail.

One way to handle this would be to store them in an Array or, if you want to keep accessing them during execution, in a Hash.

def create_layer(codename, graphic)

@layers[codename] = Sprite.new

@layers[codename] = graphic

# rest of code

end

I'm on my phone so I can't write a lot xD but you get an idea.

This way, you can acces it like @layer[:my_layer].ox += 1 to, say, scroll it horizontally on every update.

You may use

@layer.each do |codename, sprite|

# Code to apply to all layers.

end

to apply something to all layers.

You can use this to define a method that disposes all layers ever created. Just make sure once you complete the loop, you clear the Hash with

@layers = Hash.new

In order to free memory.

Also don't forget to define @layers beforehand!

Remember, if you want a layer to be in front of the other one, you must use a .z greater than the other one. You can make that happen automatically in your method if you want! As well as many other things.

Again, sorry for typing like this, as I'm on my phone xD. But hope it enlightens you :as
How do I access Ruby's core functions in RGSS- say, for something like yaml.

Normally in Ruby, I'd do something like:
 

require 'yaml'but in RGSS, require seems to be looking for a file named yaml in my project folder. I know that Eb! stripped out most (all) of stdlib, but YAML should be in core rather than stdlib.
No, YAML is actually part of the Ruby standard library -- it is not a part of the core language at all, and never has been.
In RGSS3, I just have some questions that I wanted to be cleared:

1. Is there another way to rewrite the Audio play in RGSS3? Say for example, to play the audio, we mostly go to the Audio class, but is there a way to open a new or replicate an Audio player so you can set up other things like left and right section of the BGM, tempo and volume?

2. Is there a way to track or keep a record of all the items gained? Say for example, in your database, you have at least 4 items namely Potion, Antidote, Ether and Hookshot. Now, along the way, you got Potion and Antidote. The system I am making needs to track which items are gained and differentiate them from all the items from the database...I was thinking of going pedestrian and just do a tracker by doing a global array and play along the events, but that would be just tiresome and loads of global variables. Does anyone know a solution?
If im holding some bitmaps in a cache that are used at varying frequencies when should I clear that cache?

Do I even have to clear it?

Will the GC clear them up for me when needed?

Bit of a cache newb over here ;_;
@Loli: You could probably just copy YAML over to ruby and try loading it from the game's root folder. If that doesn't work, I might have a few tricks to help; let me know.

@SoulPour777:

1) Yes, there is a way; you get an audio library dll and interface with it via Win32API or DL::Importer. Both are a bit complex; Win32API is more user friendly but DL is cleaner / more efficient IF you know how to use it (there isn't much documentation; you have to figure it out by yourself, basically). There are a few FMOD scripts floating around; however, fmod is not available free for commercial anything, so you'd have to buy a license. I'd look up gorilla audio library.

2) What exactly do you mean? Like, a count of ALL of a specific type of item gained, even if it's lost again? If so, just hook into Game_Party#gain_item, and add to a variable. If you elaborate on what you mean more, we could come to a conclusion 🙂

@Dekita:  One thing I dislike about RPG Maker is the uncertainty that resources are properly disposed of, specifically on a game crash or force close. I don't think you need to clear the Cache though; that's really only for extreme cases. Think about it. A bitmap's memory size is technically width * height * sizeof(unsigned int) (which is 4 bytes I do believe; try executing "puts DL::SIZEOF_INT" in an irb command prompt, or in game).

A bitmap of 96 x 128 is approximately:

96 x 128 x 4, == 49,152.0 bytes, or

49,152.0 bytes / (1024.0 * 2) == 0.0469 Megabytes

The cache only gets to around approx. 500 times that, which amounts to approximately 23-25 MB. That isn't really something significantly hampering the RAM, as most computers have at least 1GB.

However, When things go wrong and said bitmaps aren't properly disposed (ie a game crash or force close), things can get clogged. I've been thinking of writing a batch file and c++ dll wrapper around Game.exe to manage the Bitmaps created upon crash...not sure if it'd work. Thoughts?

EDIT: Shaz, you are getting ninja'd a lot these days! Haha
2. Is there a way to track or keep a record of all the items gained? Say for example, in your database, you have at least 4 items namely Potion, Antidote, Ether and Hookshot. Now, along the way, you got Potion and Antidote. The system I am making needs to track which items are gained and differentiate them from all the items from the database...I was thinking of going pedestrian and just do a tracker by doing a global array and play along the events, but that would be just tiresome and loads of global variables. Does anyone know a solution?
What do you mean "they are gained"? If they are set up as items in the database, why would you want to track them by any other way than the standard inventory? You might need to explain a bit more about WHAT you're trying to do (maybe in a new thread)
Hmm, to explain, its something like this, I want to make a scene shop where the items only sold are the ones you've already gained or was able to find. Say for example, you have seen a potion, antidote, herbs and a magic drink...so when I enter that shop, the only ones sold are those items I just found.
@Dekita:  One thing I dislike about RPG Maker is the uncertainty that resources are properly disposed of, specifically on a game crash or force close. I don't think you need to clear the Cache though; that's really only for extreme cases. Think about it. A bitmap's memory size is technically width * height * sizeof(unsigned int) (which is 4 bytes I do believe; try executing "puts DL::SIZEOF_INT" in an irb command prompt, or in game).

A bitmap of 96 x 128 is approximately:

96 x 128 x 4, == 49,152.0 bytes, or

49,152.0 bytes / (1024.0 * 2) == 0.0469 Megabytes

The cache only gets to around approx. 500 times that, which amounts to approximately 23-25 MB. That isn't really something significantly hampering the RAM, as most computers have at least 1GB.

However, When things go wrong and said bitmaps aren't properly disposed (ie a game crash or force close), things can get clogged. I've been thinking of writing a batch file and c++ dll wrapper around Game.exe to manage the Bitmaps created upon crash...not sure if it'd work. Thoughts?
Yea I checked to be sure it is 4 bytes.

I checked and after a fairly vigorous test I have 0.94 MB of data floating around in my cache.

Do you think it would be best if I where to clear the cache manually when I am sure it can be safe?

I mean, theres no point of having almost 1MB of data floating around that doesnt need to be.

Or do you think that the performance gained from that would not be equal/greater than the performance lost from re applying the required images to my cache?

Also, pretty sure a script like the F12 reset fix would solve any issues relating to the cache holding excess undisposed bitmaps.

Your wrapper suggestion could have alternative benefits though 🙂
@SoulPour777 - Ohhh, okay. That makes loads more sense 😛

A simple $game_party variable would suffice; hook into Game_Party#gain_item, and for >= 1 of any item gained, add the item to your variable. To maximize on storage, you could throw a bitwise system in for items / armors / weapons, kinda like so:

Spoiler
class Game_Party ItemGainFlags = [0x01, 0x02, 0x04] alias init_item_gain_check_ initialize def initialize(*argv, &argb) @item_gains = [nil] init_item_gain_check_(*argv, &argb) end def add_gain(rpg_item) return if rpg_item.nil? index = [RPG::Item, RPG::Weapon, RPG::Armor].index(rpg_item.class) n = @item_gains[rpg_item.id].to_i r = @item_gains[rpg_item.id] = n | ItemGainFlags[index] p r return r end alias gain_item_add_gain_ gain_item def gain_item(item, count, *argv, &argb) add_gain(item) if count > 0 gain_item_add_gain_(item, count, *argv, &argb) end def had_item?(item) return false if item.nil? return false if @item_gains[item.id].nil? flag = ItemGainFlags[[RPG::Item, RPG::Weapon, RPG::Armor].index(item.class)] return @item_gains[item.id] & flag == flag endendclass Game_Interpreter def had_item?(type_id, item_id) container = [$data_items, $data_weapons, $data_armors][type_id] return $game_party.had_item?(container[item_id]) endend
@Dekita - Go ahead and clear it, by all means! I don't mean to tell you not to manage memory; actually that is a good thing! However, the lag Ruby GC can induce by being called too many times might take effect if too many large objects are released at a more enhanced rate; I would be wary of this. This is why memory management is difficult with Ruby 😛

EDIT: And my estimate on ~500 MB was grossly over-estimated; that's an -extreme- case lol.
Thanks FenixFyre, that made the problem I had been thinking so far solved. 
Well I mean, I dont need to call the GC to clear my cache.

Its essentially just a module containing a hash of image data. Easy enough to add a clear method and call it when the scene changes or whatever.

Probably something a little less frequent than every scene change. Maybe every time you save / load the game would be a wiser option...

Is there any way for me to test whether one method is quicker than another?

Some kind of class that I could run a  method like

Code:
SpeedTest.start# -- runs codeSpeedTest.print_time_taken_since_start
@SoulPour777 - No problem 🙂

@Dekita - Try Ruby's benchmark...just copy benchmark.rb over from the distribution's lib folder to your game folder and require it in, then do

Code:
Benchmark.bmbm do |x|  x.report("method1") { method1() }  x.report("method2") { method2() }end
I see what you mean by it not being so often now. I thought it was a heavy operation more often than once every now and then. In that case, why not? The less RAM being used, the better.

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