Split Numbers Into Digits

HexMozart88

The Master of Random Garbage
Veteran
Joined
May 15, 2016
Messages
1,879
Reaction score
3,349
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? 
 

mjshi

Jack of Most Trades
Veteran
Joined
Feb 16, 2013
Messages
969
Reaction score
807
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.
 

HexMozart88

The Master of Random Garbage
Veteran
Joined
May 15, 2016
Messages
1,879
Reaction score
3,349
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. 
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top