Split Numbers Into Digits

Discussion in 'RPG Maker VX Ace' started by HexMozart88, Aug 2, 2016.

  1. HexMozart88

    HexMozart88 The Master of Random Garbage Veteran

    Messages:
    1,597
    Likes Received:
    2,866
    Location:
    A small hut in the forest
    First Language:
    English
    Primarily Uses:
    RMVXA
    I am fairly certain this is possible, but how would I go about splitting a number (example HP) into individual digits and loading them into a variable? 
     
    #1
  2. mjshi

    mjshi Jack of Most Trades Veteran

    Messages:
    864
    Likes Received:
    692
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    N/A
    A single variable can't hold multiple values. So 123 can't be split into 1, 2, and 3 and stored in $game_variables[1], unless you're using either a hash or an array.


    Otherwise, this would split up the variables into digits:

    Code:
    def split_digits(num)
      digits = []
      num.to_s.each_char do |bit|
        digits.push(bit.to_i)
      end
      return digits
    end
    Calling split_digits(123) would return [1, 2, 3]. This is called an 'array'. If you then wanted to store it into variables, you'd do a script call and say


    $game_variables[3], $game_variables[2], $game_variables[1] = split_digits(421)


    And variable 3 would get 4, variable 2 would get 2, and variable 1 would get 1.


    If instead you had called split_digits(53), variable 3 wouldn't get anything (it'd be empty) variable 2 would get 5, and variable 1 would get 3.
     
    #2
  3. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,047
    Location:
    Toronto
    First Language:
    English
    What are you trying to do?
     
    #3
  4. HexMozart88

    HexMozart88 The Master of Random Garbage Veteran

    Messages:
    1,597
    Likes Received:
    2,866
    Location:
    A small hut in the forest
    First Language:
    English
    Primarily Uses:
    RMVXA
    @Tsukihime I am trying to show the player's level, MP and HP using pictures which I sort of know how to do, I just wanted to know how to split the digits of those numbers into separate variables so that I can reset one digit when switching to the next, example resetting the ones column when switching to the tens column. 
     
    #4

Share This Page