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.

Optional script loading

Imagine im writing a script that modifies the main menu, but i want that option to be optional.

I know i can do it using alias, checking for config and then calling for the original method. Or just using condition to do X or Y.

But i can do it better? What im was wondering is if i can make that a part of script only loads if the config option is true. Something like:

Is condition true?
class Game_System
rewrite of methods,etc
end

The other way will be creating it in addon, to put before main if you want to active it. But anyway im asking for help.
 
The best way to do it is to make it load every time, but your custom code (within the methods) will be within an if statement. For example:
Code:
class

  alias method_old method

  def method

    if ENABLED

      #code here

    end

    method_old

  end

  def newmethod

    if ENABLED

      #code here

    end

  end

end

 
Note that you can also make methods return something entirely different if the code is not set to enabled. My example merely makes the method end processing without doing anything but check an if statement's conditional.

The advantage to doing it this way is that it allows you to dynamically turn a script on and off. Instead of simply forcing a check at load (or reload, since it would also be checked at F12), it can change in the middle of an active session.
 
But some aliased methods(when are a lot or the scene is totally changued) can make errors, no? Or im wrong?

I use alias and load selection. The load selection is just and ultimate compatibality option(configurable by user without scripting). It removes totally some code and his functions if the alias or whatever goes wrong.

If im creating a save system, but i adds a little functions for other scenes, the idea is to make that totally skipabble. And its pretty quick to make, also.
 

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