Difference between revisions of "Display float in text"

From GTAMods Wiki
Jump to navigation Jump to search
(very useful)
 
(errors fixed)
Line 5: Line 5:
 
  TEST    ~1~.~1~
 
  TEST    ~1~.~1~
 
  TEST0    ~1~.0~1~
 
  TEST0    ~1~.0~1~
 +
TESTN    -~1~.~1~
 +
TESTN0  -~1~.0~1~
  
 
Then get a floating-point value that you wish to display with a text. Use this code to convert the number into a readable text.
 
Then get a floating-point value that you wish to display with a text. Use this code to convert the number into a readable text.
 
<source lang="scm">
 
<source lang="scm">
 
//...
 
//...
0007: 0@ = 90.4                                       // your float value, example shown is 90.4
+
0007: 0@ = 90.4                                         // your float value, example shown is 90.4
0092: 1@ = float 0@ to_integer                         // convert the original value to an integer, 90
+
0092: 1@ = float 0@ to_integer                         // convert the original value to an integer, 90
0093: 3@ = integer 1@ to_float                         // convert that value back to a float, 90.0
+
0093: 3@ = integer 1@ to_float                         // convert that value back to a float, 90.0
0087: 2@ = 0@                                         // get the original value so that original value is not lost, 90.4
+
0087: 2@ = 0@                                           // get the original value so that original value is not lost, 90.4
0063: 2@ -= 3@                                         // subtract that value from the converted float, 90.4 - 90.0 = 0.4
+
0063: 2@ -= 3@                                         // subtract that value from the converted float, 90.4 - 90.0 = 0.4
0013: 2@ *= 100.0                                     // multiply that value by a hundred, 0.4 * 100.0 = 40.0
+
0013: 2@ *= 100.0                                       // multiply that value by a hundred, 0.4 * 100.0 = 40.0
0092: 4@ = float 2@ to_integer                         // convert that value into an integer, 40
+
0092: 4@ = float 2@ to_integer                         // convert that value into an integer, 40
 +
0095: make 4@ absolute_integer                          // make value absolute
 +
00D6: if and
 +
0023:  0.0 > 0@                                        // if original value is between -1.0 and 0.0
 +
0021:  0@ > -1.0
 +
004D: jump_if_false @TextPositive
 +
0002: jump @TextNegative
 +
 
 +
:TextPositive
 
00D6: if
 
00D6: if
0019:  4@ > 9                                         // skip over values that have a zero in the first decimal place
+
0019:  4@ > 9                                         // skip over values that have a zero in the first decimal place
 
004D: jump_if_false @TextFloat
 
004D: jump_if_false @TextFloat
02FD: text_2numbers_lowpriority 'TEST' 1@ 4@ 100 ms 1 // use any opcodes that can support at least two numbers, 90.40
+
02FD: text_2numbers_lowpriority 'TEST' 1@ 4@ 100 ms 1   // use any opcodes that can support at least two numbers, 90.40
 
0002: jump @TextFloat2
 
0002: jump @TextFloat2
  
 
:TextFloat
 
:TextFloat
02FD: text_2numbers_lowpriority 'TEST0' 1@ 4@ 100 ms 1 // if original value was 90.04, this will display it instead
+
02FD: text_2numbers_lowpriority 'TEST0' 1@ 4@ 100 ms 1 // if original value was 90.04, this will display it instead
 +
0002: jump @TextFloat2
 +
 
 +
:TextNegative
 +
00D6: if
 +
0019:  4@ > 9
 +
004D: jump_if_false @TextFloatN
 +
02FD: text_2numbers_lowpriority 'TESTN' 1@ 4@ 100 ms 1  // if original value was -0.4, this will display it instead
 +
0002: jump @TextFloat2
 +
 
 +
:TextFloatN
 +
02FD: text_2numbers_lowpriority 'TESTN0' 1@ 4@ 100 ms 1 // if original value was -0.04, this will display it instead
  
 
:TextFloat2
 
:TextFloat2

Revision as of 23:25, 4 July 2010

Opcodes can be used to display a GXT entry with numbers if the symbol ~1~ is present. It can only display integer values so any floating-point values will be displayed as an integer value instead. This code snippet converts float values with up to two decimal places into a format usable for all text with number support opcodes.

First make sure your GXT entries can support at least two numbers. Get two GXT entries and have the numbers arranged in this format.

Key      Entry
TEST     ~1~.~1~
TEST0    ~1~.0~1~
TESTN    -~1~.~1~
TESTN0   -~1~.0~1~

Then get a floating-point value that you wish to display with a text. Use this code to convert the number into a readable text.

//...
0007: 0@ = 90.4                                         // your float value, example shown is 90.4
0092: 1@ = float 0@ to_integer                          // convert the original value to an integer, 90
0093: 3@ = integer 1@ to_float                          // convert that value back to a float, 90.0
0087: 2@ = 0@                                           // get the original value so that original value is not lost, 90.4
0063: 2@ -= 3@                                          // subtract that value from the converted float, 90.4 - 90.0 = 0.4
0013: 2@ *= 100.0                                       // multiply that value by a hundred, 0.4 * 100.0 = 40.0
0092: 4@ = float 2@ to_integer                          // convert that value into an integer, 40
0095: make 4@ absolute_integer                          // make value absolute
00D6: if and
0023:   0.0 > 0@                                        // if original value is between -1.0 and 0.0
0021:   0@ > -1.0
004D: jump_if_false @TextPositive
0002: jump @TextNegative

:TextPositive
00D6: if
0019:   4@ > 9                                          // skip over values that have a zero in the first decimal place
004D: jump_if_false @TextFloat
02FD: text_2numbers_lowpriority 'TEST' 1@ 4@ 100 ms 1   // use any opcodes that can support at least two numbers, 90.40
0002: jump @TextFloat2

:TextFloat
02FD: text_2numbers_lowpriority 'TEST0' 1@ 4@ 100 ms 1  // if original value was 90.04, this will display it instead
0002: jump @TextFloat2

:TextNegative
00D6: if
0019:   4@ > 9
004D: jump_if_false @TextFloatN
02FD: text_2numbers_lowpriority 'TESTN' 1@ 4@ 100 ms 1  // if original value was -0.4, this will display it instead
0002: jump @TextFloat2

:TextFloatN
02FD: text_2numbers_lowpriority 'TESTN0' 1@ 4@ 100 ms 1 // if original value was -0.04, this will display it instead

:TextFloat2
//...

A more complex code is needed if you want to be more precise but the comments here should be enough to guide you. The most numbers one opcode can display separately is six (0308) but creativity can go a long way.