Display float in text

From GTAMods Wiki
Revision as of 21:36, 22 February 2010 by Spaceeinstein (talk | contribs) (very useful)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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~

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
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

: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.