Main Menu
  • Welcome to The RPG Maker Resource Kit.

Problems with $data_classes[@class_id]

Started by l33tmaster104, February 25, 2009, 12:04:40 AM

0 Members and 1 Guest are viewing this topic.

l33tmaster104

I just started messing with RMXP's scripting system and I'm trying to add a formula for HP, but there's something I need that doesn't seem to be working: the class id of an actor... The code I'm using is below


[[(35 + 5 * base_maxhp + $hp_factor[$data_classes[@class_id]] *
(1..base_maxhp).inject{|a,b| a+b})*(1 + 1 / 100), 1].max, 9999].min


The problem is it continues to give me the error "NoMethodError occurred. undefined method for `*' for nil:nilClass". I tried my hardest to figure it out but I just couldn't  :(

Hopefully this is enough info, and thanks in advance  :D

vgvgf

Firstly, "1 / 100" will not work, because it will be rounded. Insteand of (1 + 1 / 100) use
1.01 . For having values after the point, you have to use Float objects, that include the "." in their declaration.
Secondly, ruby hasn't the "inject" method you used there, have you declarated it for the Range class?
Quoteclass Range
  def inject(n = 0)
     each { |value| n = yield(n, value) } 
     n 
  end 
end
Third, you should make your coding more clearly, all that code is a really mess, hehe. Do things like this:
Quotehp = 35 + 5 * base_maxhp
hp_factor = $hp_factor[$data_classes[@class_id]]
hp_factor *= (1..base_maxhp).inject {|a, b| a + b}
hp_factor *= 1.01
[[hp + hp_factor, 1].max, 9999].min
It will look much more clear and understandable, and also easy for debuging.

Well, about your problem. The NoMethodError for NilClass is a really common error, just get used to it, if you script you will see it a lot. That error says that it can't call the method "*"(multiply) for a NilClass, or in other words for a non declared object.
Maybe you have some trouble with the inject method. Or maybe $hp_factor[$data_classes[@class_id]] is returning this Nil object, because $hp_factor is bad declared. It is a Hash? I see it difficult to have as index value a RPG::Class object, the result from "$data_classes[@class_id]". You should use @class_id directly. I don't know what do you want to do, if you explain a bit more the situation, it would be easier.