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.

Function Pointers in Ruby

Status
Not open for further replies.
Is it possible in Ruby/RGSS to have a variable which stores the address of a function? (Or a copy of a function itself?)

Purpose being so that when I call the variable, it returns something according to the function it (the variable) holds. I'm programming an AI system, and it would be fantastic if I could write AI scripts/methods and have the appropriate enemies store the methods that concern them (or addresses to those methods).
 
Yeyinde;308890 said:
All arguments in Ruby are passed by reference! Isn't it great?

Out of curiosity, is this true for basic data types, as well? Ah, sorry, never mind. No need to answer. It can be figured out with 2 minutes of experimental effort. :P

Thanks for the answer, but, I was wondering how functions/methods can be stored in variables (actually, just methods would be alright). Sorry for not being more clear.
 

khmp

Sponsor

I'm glad this was asked because I didn't know either.

Code:
class MyClass 
  def myMethod(i, j) 
    i + j 
  end 
end 

# instanciate a MyClass 
m = MyClass.new 

# get the symbol representing myMethod inside the MyClass m 
fp = m.method( :myMethod ) 

# call the method through the pointer fp 
fp.call(1, 2) # -> 3

That was taken directly from http://renaud.waldura.com/doc/ruby/idioms.shtml

Viva la runtime decisions!

Good luck with it Retlaf! :thumb:
 

Anonymous

Guest

This topic has been resolved. If Retlaf or any other users have any questions or further problems regarding this topic, please create a new thread about them.

Thank you!
 
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