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.

Check if a method is called? [RESOLVED]

Status
Not open for further replies.
Does Ruby have a built in function that will check if a certain method is called from the same class? (Or any class for that matter.)

I want to abort a loop from one method if another method is called. Anybody know the most efficient way to do this?
 
I don't know of such function.
Can't you do something like:
Code:
$game_temp.method_was_called = true
inside the method, and check the variable in the loop:
[rgss]loop do
#whatever
break if $game_temp.method_was_called
end
[/rgss]
 
Well there's no real reason I need to do this. I'm just messing around with some code. (I just started with RGSS) I was just wondering if there was some built in function that could check if a certain method was called. It could return true or false and then you could use that to accomplish a desired effect.

It would be kind of like what silver wind said but without having to use a variable. It seems like it would be useful for debugging purposes mainly, but I'm sure it could be used in other ways. Also, there could be a built in function that checks if a method even exists...

Seems useful, just me thinking out loud.

What do you think?
 
You can already check if methods are defined, however that is restricted to modules (as that's the only sense-making thing, if you ask me). For further information, check this out.

As far as checking if a method is called goes... I don't really see a purpose behind it. Methods aren't called randomly, there's always a reason behind it. Let's say your Game_Player class has a method named move_down that is called when you press the DOWN key - instead of checking if the method is called, you can just check if the DOWN key is pressed. Also, you'd only need to check that either way if you want to trigger something in multiple classes at once. While that contains a high probability of being unneeded, you'd do that using a temp variable as silver suggested.
That basically is why I was asking for a reason, as I don't see a case where you'd actually have to check if a method is called... and even if it'd be, you'd have to use silvers method, for the simple reason that even if there'd be a way to do this with Ruby, it'd have to store the currently executed method somewhere, like this:
Code:
def method

  $method_running = 'method'

  # actual method goes here

  $method_running = ''

end
It'd be heavily inefficient and, as said above, not necessary.
 
Status
Not open for further replies.

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