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.

Checking if an object exists or has been defined?

Good day everybody, I was curious if there was an easy method to check if certain objects exist, or certain objects have been defined?

For example, my Custom Menu System enhances alot of other Window/Scene scripts I've made, but the enhancements are supposed to only be optional as long as the 'CMS' module exists.

So, for the CMS plug-ins I need to be able to detect if the module CMS is there or not. I know there are certain ways you could possibly do it, like a statment such as 'unless CMS.nil?', but what is the proper way to check if something exists or if it's been defined?

This question is talking about all objects in general, such as classes, modules, constants, etc.

I'm not sure if there is a different syntax to check each kind of object, but if there is can somebody fill me in on what could come in handy for checking an objects existance?
 
Well, using print statements and such, so far as I can tell, classes and modules can simply be checked for existance through an if statement such as
Code:
if Game_Actor != nil
  do something
end

that won't work for methods or constants though.
 
Well, sometimes a module might be interpreted as a constant if the module doesn't exist, which isn't much of a problem, except for the fact that I have many extra menu components with CMS plugins.

Right now I have it where it checks...

if PlugIns.include?('Kain Nobel CMS')
  # all window classes have CMS settings applied.
  # all windows move based off CMS movable windows class.
end

But is there a way to reword that, like...?

if Module.defined?:)CMS)
  # do something
end

Because there's obviously some kinda difference between something not existing and something just being 'nil'. I guess in the meantime the PlugIn thing works fine, but still sometimes is required to call the CMS module, and if the plugin string is there and the module doesn't exist it'll still error out because its calling a non-existant module.

I just want it to check if module CMS exists then do its thing, or just ignore the entire enclosed code block if it doesn't. Is there a proper way to do that already defined in Ruby?
 
Names of classes and modules are constants and that's why they have to begin with an upper-case letter.
Code:
if defined?(CMS) && CMS.is_a?(Module)
 p 'CMS is defined and is a Module object.'
else
 p 'CMS is not defined or is not a Module object.'
end

Explanations:
defined?
Object#is_a?

Hope this helps!
 

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