RGGS3 Script help

Posted in the wrong section before.

Wondering if anyone could tell me whats wrong with this code:

def activate() $game_switches[998] = trueendactivate()I keep getting this error:

c015NFW.png
Is the script that contains it above Game_Switches in the list?
No, is that the problem?
No, that would have been a problem had it been the case.

Hmm, is the snippet just by itself or is it part of a larger script?
By itself.
And where exactly in the script editor is it currently located?
Below a few other scripts under Materials.
But above Main, yes?
Of course lol.
Aha. I'll tell you what the problem is:

RGSS, being based on Ruby which is an interpreted language, basically runs through your entire script list when you start the game and interprets all the code. Your call to "activate" is outside of a class or module, so Ruby takes it as something it has to process immediately, so it tries to set $game_switches[998] to a value...but because it hasn't yet reached the part where it has DataManager create all the global variables, $game_switches doesn't exist yet.
Do you want the switch set at game start-up or whenever a new game is started?
Aha. I'll tell you what the problem is:

RGSS, being based on Ruby which is an interpreted language, basically runs through your entire script list when you start the game and interprets all the code. Your call to "activate" is outside of a class or module, so Ruby takes it as something it has to process immediately, so it tries to set $game_switches[998] to a value...but because it hasn't yet reached the part where it has DataManager create all the global variables, $game_switches doesn't exist yet.
Ah! Makes sense.

Do you want the switch set at game start-up or whenever a new game is started?
Whenever a new game is started.
Use something like this, then. Make your own module, then call the new method just after the databases are loaded in DataManager.

Code:
#==============================================================================# ** Addendum#==============================================================================module Addendum  #--------------------------------------------------------------------------  # * Activate  #--------------------------------------------------------------------------    def self.activate    $game_switches[998] = true  endend # Addendum#==============================================================================# ** DataManager#==============================================================================module DataManager  #--------------------------------------------------------------------------  # * Set Up New Game  #--------------------------------------------------------------------------  class << self    unless method_defined?(:data_q36N9Sjn_dm_setup_new_game)      alias_method(:data_q36N9Sjn_dm_setup_new_game, :setup_new_game)    end  end    def self.setup_new_game(*args, &block)    data_q36N9Sjn_dm_setup_new_game(*args, &block)    Addendum.activate  end  end # DataManager
Well that worked, thanks a lot.
You can keep adding methods, constants and so on to the 'Addendum' module, as well. Use it for a dumping ground for anything that is too small for its own dedicated module.

Original thread: https://forums.rpgmakerweb.com/threads/rggs3-script-help.40652/