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.

Creating a new object from a given object type?

Hey all,

I'm making my own serialization script, and I can't seem to figure out how to create a new object based on a given class type - for instance, if the save file has the word Item, I would like to be able to tell RMXP to create a new instance of the class Item.

In other languages it's pretty easy to do, such as in C# where you do  Activator.createInstance( type ) and it makes the object.

Is there a way to do this in RGSS?

Thanks!
 
Yes, there's such a method in Ruby: Object#const_get(anString)

An example:
Code:
class Item
  def initialize
    p 'Some output.'
  end
end

a = Object.const_get('Item').new  # => Some output.
 

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