Gw Temp

Menu

Tutorial - 'Variable Splitting' by dragonheartman

An item about RPGMaker 2000 posted on

Blurb

Dragonheartman shows you how to split a variable into it's cooresponding digits as their own separate variable.

Body


This tutorial basically explains how to split a multi digit variable into separate individual variables. The reason you would want to do this is to lessen any fork amount to show numbers in a CBS or CMS. First, you must know about MOD, our best variable changing friend.

USING MOD:
When you change a variable by MOD, just think of dividing, but giving a remainder. So 1234 MOD 100 would be... 34. With MOD it is possible to separate variables and even add decimal values into your variables(tutorial soon to come).

Here are some practice problems :

15 MOD 10 =

15 MOD 5 =

132 MOD 10 =

135 MOD 1000 =

SPLITTING A VARIABLE
To get the ones digit of any number, what could you do? You could MOD by 10 of course. 1,234 MOD 10 is 4. MODing 235,434,763,525,324,532 by 10 gives you 2. See, it really works.

To get the tens digit, you can first mod by 100 and then divide by 10 (ex. 1,234 MOD 100 is 34. 34 divided by 10 is 3) ps, don't worry, RM2k isn't smart enough to round variables.

To get the hundreds digit, first MOD 1000 and then divide by 100. (ex. 1,234 MOD 1000 is 234, divided by 100 is 2.)

To get the thousands digit, MOD 10,000 and then divide 1,000 (ex. 1,234 MOD 10,000 is 1234, divided by 1,000 is 1)

Thus a pattern can be formed, and you can split any variable. Here would be an RM2k code example:

<>var ch. [1.random4digit#] set 1111-9999 -random
<>var ch. [2.onesdigit] set, var 1 value
<>var ch. [2.onesdigit] MOD 10
<>var ch. [3.tensdigit] set, var 1 value
<>var ch. [3.tensdigit] MOD 100
<>var ch. [3.tensdigit]/10
<>var ch. [4.hundredsdigit] set, var 1 value
<>var ch. [4.hundredsdigit] MOD 1000
<>var ch. [4.hundredsdigit]/100
<>var ch. [5.thousandsdigit] set, var 1 value
<>var ch. [5.thousandsdigit] MOD 10000
<>var ch. [5.thousandsdigit]/1000
<>show message:
:Look Mama, I took the random
:number \V[1] and split it into
:four separate variables, \V[2]
:is the ones digit,
<>show message:
:\V[3] is the tens digit,
:\V[4] is the hundreds digit
:\V[5] is the thousands digit.
:Don't I rule, Mama?

(Make that into a parallel process event so you get plenty of random numbers and splitters)

Any questions, comments, or whatever go ahead PM me.