Jump to content


Photo

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


  • Please log in to reply
1541 replies to this topic

#1 kal

kal

    Advanced Member

  • Ace Member
  • 119 posts
  • RM Skill - Coder

Posted 03 December 2011 - 05:43 AM

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, 03 December 2011 - 05:44 AM.


#2 Jet

Jet

    Coder

  • Ace Member
  • 92 posts
  • LocationCalifornia
  • RM Skill - Coder

Posted 03 December 2011 - 09:18 AM

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.

All my scripts are made under this license:
88x31.png

 

Get 20GB of cloud storage with Copy under my referral link.


#3 kal

kal

    Advanced Member

  • Ace Member
  • 119 posts
  • RM Skill - Coder

Posted 03 December 2011 - 09:46 AM

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?

#4 YF

YF

    (。´◕ ‿‿ ◕`。)

  • Ace Member
  • 187 posts
  • RM Skill - Coder

Posted 03 December 2011 - 10:12 AM

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.

#5 kal

kal

    Advanced Member

  • Ace Member
  • 119 posts
  • RM Skill - Coder

Posted 03 December 2011 - 12:42 PM

Ahh! I get it now (it took awhile :P)! I see how an infinite loop is created there. Thanks for explaining that.

#6 mitchi.exe

mitchi.exe

    Premium Member!

  • Ace Member
  • 127 posts
  • RM Skill - Designer

Posted 07 December 2011 - 07:34 PM

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? >.<
Posted Image

#7 YF

YF

    (。´◕ ‿‿ ◕`。)

  • Ace Member
  • 187 posts
  • RM Skill - Coder

Posted 07 December 2011 - 08:16 PM

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.

#8 mitchi.exe

mitchi.exe

    Premium Member!

  • Ace Member
  • 127 posts
  • RM Skill - Designer

Posted 07 December 2011 - 08:55 PM

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, 07 December 2011 - 08:58 PM.

Posted Image

#9 Jet Kobayashi Zala

Jet Kobayashi Zala

    Taco Coder

  • Ace Member
  • 7 posts
  • LocationDenver, CO
  • RM Skill - Jack of All Trades

Posted 08 December 2011 - 02:14 PM

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.
- Jet Kobayashi Zala
ZGMF-X33A Eternal Saviour Pilot

Posted Image

#10 YF

YF

    (。´◕ ‿‿ ◕`。)

  • Ace Member
  • 187 posts
  • RM Skill - Coder

Posted 08 December 2011 - 02:32 PM

It's always been zero-index. :S

#11 Jet Kobayashi Zala

Jet Kobayashi Zala

    Taco Coder

  • Ace Member
  • 7 posts
  • LocationDenver, CO
  • RM Skill - Jack of All Trades

Posted 08 December 2011 - 02:40 PM

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, 08 December 2011 - 02:41 PM.

- Jet Kobayashi Zala
ZGMF-X33A Eternal Saviour Pilot

Posted Image

#12 YF

YF

    (。´◕ ‿‿ ◕`。)

  • Ace Member
  • 187 posts
  • RM Skill - Coder

Posted 08 December 2011 - 02:47 PM

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

#13 Jet Kobayashi Zala

Jet Kobayashi Zala

    Taco Coder

  • Ace Member
  • 7 posts
  • LocationDenver, CO
  • RM Skill - Jack of All Trades

Posted 08 December 2011 - 02:53 PM

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.
- Jet Kobayashi Zala
ZGMF-X33A Eternal Saviour Pilot

Posted Image

#14 YF

YF

    (。´◕ ‿‿ ◕`。)

  • Ace Member
  • 187 posts
  • RM Skill - Coder

Posted 11 December 2011 - 12:08 PM

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.

#15 BigEd781

BigEd781

    Advanced Member

  • + Premium Members
  • 134 posts
  • RM Skill - Jack of All Trades

Posted 14 December 2011 - 01:15 PM


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.
My blog - It's awesome, I assure you

While sloppy writing does not invariably mean sloppy thinking, we've generally found the correlation to be strong -- and we have no use for sloppy thinkers. If you can't yet write competently, learn to. - Eric Raymond


Posted Image

#16 Jet Kobayashi Zala

Jet Kobayashi Zala

    Taco Coder

  • Ace Member
  • 7 posts
  • LocationDenver, CO
  • RM Skill - Jack of All Trades

Posted 15 December 2011 - 12:54 AM



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, 15 December 2011 - 12:55 AM.

- Jet Kobayashi Zala
ZGMF-X33A Eternal Saviour Pilot

Posted Image

#17 mitchi.exe

mitchi.exe

    Premium Member!

  • Ace Member
  • 127 posts
  • RM Skill - Designer

Posted 15 December 2011 - 05:00 AM

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

Posted Image

#18 Kread-EX

Kread-EX

    Advanced Member

  • Ace Member
  • 159 posts
  • RM Skill - Coder

Posted 15 December 2011 - 05:09 AM

Append operator. Works on arrays (it's like the push method) and strings (it concatenates two strings). IO objects also can use it for string output.

#19 kal

kal

    Advanced Member

  • Ace Member
  • 119 posts
  • RM Skill - Coder

Posted 15 December 2011 - 06:27 AM

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, 15 December 2011 - 06:29 AM.


#20 Kread-EX

Kread-EX

    Advanced Member

  • Ace Member
  • 159 posts
  • RM Skill - Coder

Posted 15 December 2011 - 06:42 AM

You know kal, you're very good at explaining things (unlike me) and reading your posts is always a pleasure.

Just wanted to say that.

Edited by Kread-EX, 15 December 2011 - 06:42 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users