Jump to content
TaranAlvein

Undefined method error in script

Recommended Posts

I'm getting the error "Undefined method '[]=' for nil:NilClass.  I've looked up advice online, but it's utterly useless, since the problem itself should be impossible.  All I'm doing is using a script to adjust the value of a variable.  Please take a look at my code and let me know if there is anything that would cause a problem.

 

This snippet of code represents the sum total of all scripts that interact with the game's variables in any way.  The other two scripts are entirely self-contained, and at any rate, they've been commented out to rule them out as suspects.

Error script.png

Share this post


Link to post
Share on other sites

Uh... Looks to me like that code was made for an event call or something and you just stuck it in a class without understanding how classes work. You aren't actually defining a method, so your code just gets run once when the program starts up. I mean, when else would it run? But $game_variables isn't set yet. In general whenever you see an undefined method error about nil:NilClass it means you are trying to do something with something that isn't actually there. Even if it was, when starting or loading a game $game_variables gets reset anyway.

 

What are you trying to do with this? I mean, when is this code going to actually be used? If you want to use it as part of an event call, you should define it as a method in the Game_Interpreter class like this:

 

class Game_Interpreter
  
  def Calc_Hit_Chance
    <insert your code here>
  end
  
end

 

Or heck, just not using a class at all will work:

def Calc_Hit_Chance
  <insert your code here>
end

 

Generally very few programmers program that way anymore, but hey, it works and is pretty simple! If you are just starting out, maybe ditching classes if all you use is event script calls is the right move. Though you have to be careful what you name your methods if you don't use a class or module, since if you name a method something that is already used it will be overwritten and that can cause issues. Half the reason programmers don't tend to do it that way anymore is because it can get annoying in large programs when you have to name every function something completely different.

 

Of course in any case the code won't do anything unless you actually call it somewhere, but I assume you are using events to do that right?

Edited by Kayzee

Share this post


Link to post
Share on other sites

I know what I forgot to do.  I forgot to put a method in the class.  Thanks for pointing that out.

Edited by TaranAlvein
Tranposed method and class when writing. Derp.

Share this post


Link to post
Share on other sites

Also remember to use a class's instance methods you need to make an object of that class! Though you can also define methods for the class object it's self, but in that case you should probobly make it a module instead. For example:

 

module MyModule

	def self.calc_hit_chance
  	<insert your code here>
	end

end

 

when you use self. in front of a method like that it means you run it directly off the thing you are defining rather then needing to make an object. So MyModule.calc_hit_chance in this case.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×