RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Problems with $data_classes[@class_id]

0 Members and 1 Guest are viewing this topic.

*
Rep: +0/-0Level 84
"Just 'cause you feel it doesn't mean it's there"
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

Code: [Select]
[[(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
« Last Edit: February 25, 2009, 02:41:39 AM by l33tmaster104 »

**
Rep:
Level 84
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?
Quote
class 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:
Quote
hp = 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.