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.

Script error

Status
Not open for further replies.
I made this script:

Code:
#--------------------------------------------------------------------------
# * Add on Script.
# V 1.0
# By Cheatking
#--------------------------------------------------------------------------

class Add_On
  if FileTest.exist?("add on 1.txt")
# Switches
  $game_switches["switch_1"] = true
else
  $game_switches["switch_1"] = false
  end
end
When i run it i get this error:
Script ' ' line 10: NoMethodError occurred.
undefined method `[]='for nil:NilClass
 
That isn't how you should be doing something like that.

A) You created a class which just has no methods whatsoever
B) Never create a class for just a function (switching variables if file detected), use a module
C) You use an integer, not a string for the variable id (1, 6, 18, 37, 126, ... )
D) Just to be picky, it is better to use single ' marks instead of double " marks when defining strings, unless you have a complex string like "#{actor.name} : Hp - #{actor.hp} / #{actor.map_hp}"

The reason you are getting an error is that your little code there isn't within a method. Once you start your game, that code is read. It errors because $game_switches is not created until your select new game or load a saved game.

Try something like this:

Code:
module YourModuleNameHere
  def self.check_savefile
    $game_switches[1] = FileTest.exist?('add on 1.txt')
  end
end

Now to use this, create a call script or whatever with :
Code:
Script : YourModuleNameHere.check_savefile
 
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