Display float in text

From GTAModding
Jump to: navigation, 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. Have your GXT entries inputted in this format.

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

Have a floating-point value that you wish to print on the screen. Use this code to convert that 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
if and
    // if original value is between -1.0 and 0.0
    0.0 > 0@
    0@ > -1.0
then
    if
        // if first decimal place of value is not zero
        4@ > 9
    then
        // use any print text opcodes that can support at least two numbers
        // if original value was -0.4, this will display it instead
        02FD: text_2numbers_lowpriority 'TESTN' 1@ 4@ 1000 ms 1
    else
        // if original value was -0.04, this will display it instead
        02FD: text_2numbers_lowpriority 'TESTN0' 1@ 4@ 1000 ms 1
    end
else
    if
        // if first decimal place of value is not zero
        4@ > 9
    then
        // original value was 90.40, so this will display
        02FD: text_2numbers_lowpriority 'TEST' 1@ 4@ 1000 ms 1
    else
        // if original value was 90.04, this will display it instead
        02FD: text_2numbers_lowpriority 'TEST0' 1@ 4@ 1000 ms 1
    end
end
//...

A more lengthy 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.