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.

Asignation bug

My problem is a strange bug. I iterate through a hahs that have various subhashes, looking for certain keys to sum two values depending on the contents.
It works well but when i add that sum the values based on game_variables, :)variables),
that asignation broke the code somewhat and every time it try to add with the += to it
its like a = so at the end the values are 0 and -150, and not the sum of all.
Code:
    elsif key == :variables

        for key, val in arr

          p key, val

          if val[0] == 1

            

             v += $game_variables[key]

             p 'var v', v

          else

             

             r += $game_variables[key] / 100

             p 'var r', r, $game_variables[key] / 100

          end

        end

        next

This is the problematic code, if i comment it, it works well.

Full code:
Code:
  

module Wep 

 

Actions = {

    

    :attack => {

        :weapons => {

          1 => [1, -150],

          2 => [2, 0.50]

        },

        

        :armors => {

          13 => [1, -150],

          2 => [2, 0.50]

        },

        :shields => {

          1 => [2, 0.50]

        },

        :actors => {

          1 => [1, -150],

          2 => [2, 0.50]

        },

        

        :classes => {

          1 => [1, -150],

          2 => [2, 0.50]

        },

        :variables => {

          1 => [1],

          2 => [2]

        }, 

          

    },

  

    :general => {

        :weapons => {

          1 => [1, -150],

          2 => [2, 1.50]

        },

        

        :armors => {

          1 => [1, -150],

          2 => [2, 1.50]

        },

        

        :actors => {

          1 => [1, -150],

          2 => [2, 1.50]

        },

        

        :classes => {

          1 => [1, -150],

          2 => [2, 1.50]

        },

        :variables => {

          1 => [1],

          2 => [2]

        }, 

          

    },  

 

  }

  

  

  

  # 6 accions

  # iteres segons el codi,sumant tot, i executant segons el subcodi, i rula be

  # hiperfacil!

  

  

  # fer que lo de accions usi codi dels hashes, hipersimple aleshores

  # tu pases :defend i amb aixo, extreu de un hash de acciones, la bona, i ale

  def self.test(action_type, actor, extra=false)

    v = 0

    r = 0.0

    

    for key, arr  in Actions[action_type]

     #p key, arr

      if key == :weapons and actor.weapon_id != 0 

        id = actor.weapon_id

      elsif key == :shields and actor.armor1_id != 0

        id = actor.armor1_id

      elsif key == :variables

        for key, val in arr

          p key, val

          if val[0] == 1

            

             v += $game_variables[key]

             p 'var v', v

          else

             

             r += $game_variables[key] / 100

             p 'var r', r, $game_variables[key] / 100

          end

        end

        next

      else 

        next

      end

      #p arr, id

      if arr[id] != nil

        

          if arr[id][0] == 1

             v += arr[id][1]

          else

             r += arr[id][1]

          end

          p 'c', v, r

      end

 

    end

     p v, r

     

     # comprova q r no sigui negatiu

   end

end

 
 
Sorry but I just don't understand what are you trying to accomplish here, could you give more explanation?
But from a quick look I at the code I'm pretty sure that this:

Code:
 

    elsif key == :variables

        for key, val in arr

          p key, val

          if val[0] == 1 #<---------------- THIS

           

             v += $game_variables[key]

             p 'var v', v

          else

 

Could be causing a problem, since Hashes are hashes, they use KEYS as indexers not array index. So you should use there 1 or 2 like you specified above.
 
It looks like your problem stems from way over-complicating whatever it is that you're doing. Do away with using symbols, all the lamda syntax, and probably just use sub-arrays and not hashes, and your code will be much easier to manage. It looks all complicated at a glance, but if you break it down, what you're doing is quite simple and is not worth all that code.
 
First,
You should really reconsider how you're going about this and simplify; also, you could clean this up and shorten it with a few simple changes in logic.

Second,
Even without the :variables part, id is going to get set by the actor's eq, but you are also using that number to determine whether you are adding to r or v; so, unless you have armor and weapon id's not equal and one is 1 and one 2, then one of these (v or r) is going to stay 0.

Third,
There is no need to use 1 -> [1], 2 -> [2]; using 1 and 2 to determine which value to add too is not a good method, you can simplify your code and make it easier to understand by not doing this; and when you are doing $game_variables[key] / 100, are you sure that $game_variables contains a float? Also, the outcome depends on what is in game_variables and what the actor's weapon/armor id's are, so without that info, this is hard to help with.

Finally,
I can't tell what it is you want to do, but if you tell me, I can help you clean this up and get it working. PM me if you'd like assistance.
 

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