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.

detecting a method defined within the SDK

Status
Not open for further replies.
Ok, I'm using unless Object.const_defined?:)SDK)[/FONT] to detect the sdk. if the sdk has not been defined, my script will then emulate SDK.enabled?(script)[/FONT] in order to enable the script. the problem is that when multiple scripts run this, each script after the first will always return true then run an error when they try to log the script. I tried to add SDK.method_defined?:)log)[/FONT] but that returns false even if the SDK is installed, I tried SDK.method_defined?:)SDK.log)[/FONT] but that encounters an error. SDK.method_defined?:)self.log)[/FONT] also does not work.

I've found a workaround by using SDK.const_defined?:)PARTS)[/FONT] but was wondering, am I doing something wrong with the aforementioned commands?
 
Let me rephrase my question:

Why does the following not work? And how can I make it work if i can?

Code:
module SDK
  def self.log
    #do this
  end
end

SDK.method_defined?(:log)
  # returns false
SDK.method_defined?(:self.log)
  # Error :undefined method `log' for :self:Symbol
SDK.method_defined?(:SDK.log)
  # Error :undefined method `log' for :SDK:Symbol
Nevermind, I think i figured out why they didn't work. SDK.log is not an ordinary method, but a singleton method. I can detect it if i do the following:
Code:
If SDK.singleton_methods.include?("log")
  print "Yes, SDK.log is defined."
end
 
You can also use the methods Trickster suggested or the one DerVVulfman suggested:

Code:
 module SDK
    # SDK not installed
    $sdk_defined = false
    if defined?(log_alias)
      # SDK installed
    $sdk_defined = true
    end
  end
DerVVulfman's suggestion

or

Code:
begin
  SDK_AUTO_DETECT = SDK::Version
rescue
  SDK_AUTO_DETECT = nil
end
Trickster's suggestion
 
Due to its elegance and simple construct... I recommend Trickster's version.

As Trickster suggested, Seph may perform rewrites that could add or remove routines in the SDK and my earlier version relied on detecting the log_alias routine. If a later version of the SDK doesn't include this routine, the detection wouldn't work.

Trickster's version doesn't rely on that and is shorter by contrast.
 
This topic has been resolved. If DraycosGoldaryn 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