I came across a weird thing about hashes.
here's my code:
module RPGMakerWeb Message_If_Actor = { 1 => "Attack", 128 => "Attack 128!" }endclass Window_BattleLog < Window_Selectable #-------------------------------------------------------------------------- # * Display Skill/Item Use #-------------------------------------------------------------------------- def display_use_item(subject, item) if item.is_a?(RPG::Skill) add_text(subject.name + RPGMakerWeb.values[item.id]) unless item.message2.empty? wait add_text(item.message2) end else add_text(sprintf(Vocab::UseItem, subject.name, item.name)) end endendso apparently, I want the subject to say for example: "Eric: Attack 128"! if I use skill 128. As you can see on my hash I have 128 key. But whenever I execute the script, somehow the hash doesn't recognize 128 as a key and so it won't display the value of key 128. It says can't convert String to nil.
Also, if possible, is there a way to make the subject talk different words depending on if they're actors or not?
Please show a screenshot of the error. According to your first post, there is no value for 2.
simply you also need to input the to_s because this need to be convert into string
but I think you have two proble ...but not sure but you have to do this
def display_use_item(subject, item) @value = RPGMakerWeb::Message_If_Actor[item_id] if item.is_a?(RPG::Skill) add_text(subject.name + @value.to_s) unless item.message2.empty? wait add_text(item.message2) end else add_text(sprintf(Vocab::UseItem, subject.name, item.name)) end endendthe to_s allow to value to be convert into string and don't call module instance directly in the method like that put it into a Global like ( @value ) it is better for comprension and advoid ridiculous bugs
@Shaz - Woah, not sure if you were confused as to whether the OP was using a method or not...your message_if_actor is lowercase, not a constant like the OP has in their code, so your explanation is error-prone and as the OP suggested, didn't work.
@OP - When we look at a hash, it is technically made up of two arrays: keys, and values. Each object in keys refers to the same-indexed value in the values array.
Now, in Ruby 1.9 and up, hashes are ordered; that is, when you create a key/value pair and add it to a hash, then it is the nth pair in the hash. Let's look at this hash:
module RPGMakerWeb Message_If_Actor = { 1 => "Attack", 128 => "Atack 128!" }end How do we access this hash? Well, it's a constant in the module RPGMakerWeb, so to access it, would look something like:
RPGMakerWeb::Message_If_Actor Now. When we use the #keys and #values methods of Hash, it returns separate arrays. We have added 1 => "Attack", then 128 => "Attack 128!", so the key and value arrays would look something like:
RPGMakerWeb::Message_If_Actor.keys #=> [1, 128]RPGMakerWeb::Message_If_Actor.values #=> ["Attack", "Attack 128!"] So, we see that "Attack 128!" is index 1 of the values array. You have tried to do this:
RPGMakerWeb.values[128] There are two things wrong with this code; first of all, RPGMakerWeb is not the hash; it's the container module in which the hash is stored, so we change that to
RPGMakerWeb::Message_If_Actor.values[128] ..But it still errors! Ah, the reason for that is simple. When you call #values on the hash, it returns an array, not the hash itself! Therefore, the index you need to use is 1. This is why you need to leave out '.values', and do this instead:
RPGMakerWeb::Message_If_Actor[128] That is the correct code; see, when you access a hash, the value inbetween the []s is the 'key', and what is returned is the 'value'.
Hope this explanation helped 😛
which is pretty much what I had, without the lower case typo (sorry about that - I am way more familiar with calling methods than constants) ... and a much more interesting and helpful explanation 🙂
However, what happens if the hash doesn't contain the key? It's honestly too early in the morning for me to work through it, but does there need to be a .has_key? test in there somewhere, or a default value stored with the hash to return in case of no key?
Did you get that error message when using
RPGMakerWeb.values
or
RPGMakerWeb::message_if_actor
?
As it was already stated above, none of these calls should work because the respective methods should not exist for a Module object, so I would have expected a corresponding error message (however, I don't know your entire scriptset of course and I might overlook something). Are you sure this line caused the error?
When you are sure you used the right call but are still getting funny results, consider to do a console output of all elements involved to check if they have correct values, for example
p RPGMakerWeb::Message_If_Actor, item.id
When you test a script using the Battle-Test-Mode from the database, be sure to save your project before you do. Otherwise the game won't use the latest scripts (you won't have to save when you just edited items in the database).
You can test if a battler is an actor by using either its actor? or enemy? method.
I was just thinking it might throw an error or give unwanted results if the key wasn't there, which would prompt another request for help 🙂
Thanks, now solved! Requesting the thread to be closed.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.