• Announcements

    • RagingHobo

      Forum Upgraded!   07/17/2017

      We have successfully upgraded our forum to the latest version! Some things have moved around but don't worry everything is still there, we still need to find updated versions of the plugins we were using previously to restore everything to the way it was. There will be a running topic in the Feedback and Support in case you notice something wrong. Before you do so please clear your browser cache and close your browser completely to rule out a caching issue. Thank you for your patience during the outage and while we get everything back to 150%  
kal

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

1,944 posts in this topic

Here's one to start with: What exactly is the purpose of using doing an unless $@ check when aliasing a method? I know now that the $@ variable is an array that holds the backtrace of the last exception. So when you press F12 to enter debug mode, it generates an exception so $@ will evaluate to a true value (is that right?) and the aliasing won't happen. So why is it a bad idea to alias methods when you enter debug mode?

 

And a somewhat related question: do "stack level too deep" errors occur when the same method aliasing code is run more than once? If so why is this?

Edited by kal

Share this post


Link to post
Share on other sites

No, it does not occur when "any" method is aliased a second time.

"unless $@" is really only needed if you are aliasing a method that isn't in the editor, or specifically made in the class.

Such as, aliasing Graphics#update. This isn't re-evaluated in the F12 reset, and aliasing it a second time will cause a stack error.

Another example would be (at least in vx) the Game_Actor#item_effect. This method id not defined specifically in Game_actor, rather inhetired from Game_Battler. Alaising this method a second time will also cause a stack error.

Using "unless $@" will prevent the alias if $@ is not nil, which it isn't after the Reset exception is thrown.

Share this post


Link to post
Share on other sites

I'm still unclear on how this works. Why does aliasing a method defined in a superclass throw a stack error? I've been trying to reproduce the error in plain Ruby but I can't do it. For example this code here does not give an error:

 

 

class AliasBase
 def a_method
   puts "you called a method"
 end
end

class AliasTest < AliasBase
end
klass =
"
class AliasTest
 @@evaled ||= 0
 @@evaled += 1
 alias_method :a_method_orig, :a_method
 def a_method
   puts 'you called an aliased method #' + @@evaled.to_s
 end
end 
"
1000.times do
 eval klass
end
test = AliasTest.new
test.a_method
test.a_method_orig

Both test.a_method and test.a_method_orig returns "you called an aliased method #1000", so it's not like there's a thousand copies of an aliased method in the class: each time an alias happens it overwrites the method that a_method_orig points to the a_method method. So why the stack errors in RGSS? And can this be reproduced in plain Ruby?

Share this post


Link to post
Share on other sites

You wouldn't be able to reproduce it in plain Ruby.

 

When F12 is pressed in VX and resets the game, it actually reloads all of the scripts in the editor. Unfortunately, it's only the scripts in the editor. Any "hidden" classes like Graphics, Bitmap, etc. remain the same and the aliases that were made before for those such classes remain with them. Therefore, when an alias aliases an alias with the same alias name (I swear, not trying to be confusing here), it'll create an endless loop.

 

As for VX Ace, I'm lead to believe everything is reloaded this time around. Not sure yet as I haven't done extensive testing on it (since it's so annoying). Someone correct me if I'm wrong though.

Oasiris likes this

Share this post


Link to post
Share on other sites

Question!

In RGSS2, obtaining the actor id via game party can be done with this:

$game_party.members[i].id

 

How can I do that in RGSS3?

I have tested the code above and it works on the console window by using puts, but on the other hand, it gave me a NoMethodError on the game window. (undefined method "id")

I checked the Game_Actor class and it has a definition called "id", and now I don't know what is making it fail.

Why is that? >.<

Share this post


Link to post
Share on other sites

It should work. The method's there. Just remember that when you pull for "i", it's the index.

 

0 = first member.

1 = 2nd member.

2 = 3rd member.

3 = 4th member.

 

If you have inserted, say, 4 for i, it'll pull nil, to which, there's no method "id" defined for nil.

Share this post


Link to post
Share on other sites

Got it! I did use the members.size for i.

It puts 4, so that might be the problem. I just have to fix my loop now.

Thanks YF! ;3

Edited by mitchi.exe

Share this post


Link to post
Share on other sites

It should work. The method's there. Just remember that when you pull for "i", it's the index.

 

0 = first member.

1 = 2nd member.

2 = 3rd member.

3 = 4th member.

 

If you have inserted, say, 4 for i, it'll pull nil, to which, there's no method "id" defined for nil.

 

Interesting. Is RGSS3 moving to a zero-index standard? I haven't had a chance to browse the new scripts yet.

Share this post


Link to post
Share on other sites

It's always been zero-index. :S

 

It's been a while for me, I've probably just forgotten. I was thinking with VX, for instance, that $data_actors[1] referred to the first actor instead of 0.

 

EDIT: I guess technically, that'd be a RMVX convention and not RGSS.

Edited by Jet Kobayashi Zala

Share this post


Link to post
Share on other sites

The $data_* arrays are also zero index. The zero index for those are just nil, that's all.

 

Yeah. That's probably why I just assumed them to effectively be 1-indexes as 0 is just always left nil.

Share this post


Link to post
Share on other sites

Figured I'd post here since there's really nowhere else to post.

 

I did some testing with the script editor and have concluded that "unless $@" is no longer necessary for VXA scripts. Scripts are only ever loaded once every time you open up a VXA game. Even if you hit F12, the scripts are never reloaded. Unless there's another reason to use "unless $@" for aliased methods, we can finally be rid of it.

Share this post


Link to post
Share on other sites

It should work. The method's there. Just remember that when you pull for "i", it's the index.

 

0 = first member.

1 = 2nd member.

2 = 3rd member.

3 = 4th member.

 

If you have inserted, say, 4 for i, it'll pull nil, to which, there's no method "id" defined for nil.

 

Interesting. Is RGSS3 moving to a zero-index standard? I haven't had a chance to browse the new scripts yet.

 

RGSS3 is a library, it has nothing to do with ruby the language, which has always used zero-indexed arrays.

 

If you look at the $data collections you will see that they are padded at the beginning as to allow more natural access to elements based upon their id in the database.

A. and Oasiris like this

Share this post


Link to post
Share on other sites

It should work. The method's there. Just remember that when you pull for "i", it's the index.

 

0 = first member.

1 = 2nd member.

2 = 3rd member.

3 = 4th member.

 

If you have inserted, say, 4 for i, it'll pull nil, to which, there's no method "id" defined for nil.

 

Interesting. Is RGSS3 moving to a zero-index standard? I haven't had a chance to browse the new scripts yet.

 

RGSS3 is a library, it has nothing to do with ruby the language, which has always used zero-indexed arrays.

 

If you look at the $data collections you will see that they are padded at the beginning as to allow more natural access to elements based upon their id in the database.

 

Yeah, I later checked up on it, thinking the same applied to other collections like $game_party.members, so when Yanfly mentioned 0 - 1st member, I was thinking they removed the padding. It was a herp derp post on my part.

Edited by Jet Kobayashi Zala

Share this post


Link to post
Share on other sites

I've been wondering this for some time now...

What's the deal with "<<"? What does it do? Is there an alternative syntax for it?

 

I was trying to convert a Japanese script and it gave me a "<<" NoMethodError.

 

Like this one for example...

$game_temp.effect_sprites << sprite

Share this post


Link to post
Share on other sites

Yep, that's how << works, just as Kread explained. It's quite interesting how it's implemented too. When you do something like $game_temp.effect_sprites << sprite it looks like << is a built in language operator, but it is actually a method call! (and that is why it does different things when called on an array, string or IO object - you're actually calling the << method on that object). This means that you can implement your own version of << if you want to like this:

 

 

class MyArray < Array
 def <<(object)
self.unshift(object)
 end
end

my_array = MyArray.new
my_array << "woot"	  # => ["woot!"]
my_array << "wooter"   # => ["wooter", "woot!"]
my_array << "wooter!"  # => ["wooter!", "wooter", "woot"]

 

So that's a pretty useless class that overrides the << method so that it calls unshift instead (which adds an object to the beginning of the array, as opposed to the end of the array). As you see it's defined just as a normal method with a parenthesis and argument list (object) but you don't need to call it like a normal method (in which case it would look like this my_array.<<("woot") which still works by the way because this is what happens behind the scenes!). The reason you can omit the dot and add a space is to make it look more natural, and more operator like.

 

The same is true for most other "operators" such as +, +=, -= and so on.

Edited by kal
TaranAlvein, A., YF and 1 other like this

Share this post


Link to post
Share on other sites

I see. The syntax was right in the first place. But I sure don't know where I'm getting these NoMethodErrors. I'm not sure, but I think it has to do with kal's load .rb file method.

Share this post


Link to post
Share on other sites

You are most likely getting NoMethodErrors because the global or instance variable you are using is set to nil for some reason. This is the deafult value for global and instance variables in Ruby. So if I do something like this:

 

$IHaveNotBeenSet << "woot"

 

I won't get an error saying saying something about that $IHaveNotBeenSet does not exists or is not initialized, because the first time you use this variable it's value is nil. And nil does not have a << method, so that is why you get the NoMethodError.

 

There can be many reasons why the global variable you are trying to use is set to nil. For example, some code could have just assigned it to nil (this is one of the dangers with globals, you don't have any control at all about what code might change them), but it's more likely that the global is initialized in another script (so the script you are attempting to use is dependent on a script that is not there) or that the code that initializes the variable is not being executed for some reason.

 

If you want help figuring out what it is post the script here and we can probably help you out. :)

Share this post


Link to post
Share on other sites

<= is 'less than or equal to', which is different from <<

You're likely comparing two numbers, but at least one of them (probably the first) has not been set. See if it's set inside a conditional branch - if that condition is false, by the time it reaches this statement it won't contain a value.

Share this post


Link to post
Share on other sites

^ Yeah, but that's not the problem, shaz. The script was made by Yanfly, and I didn't even edit anything to it. Which disturbs me since it's highly unlikely that YF releases a script that doesn't work right off the bat.

Edited by mitchi.exe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.