Display float in text

From GTAMods Wiki
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. 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: [email protected] = 90.4                 // your float value, example shown is 90.4
0092: [email protected] = float [email protected] to_integer  // convert the original value to an integer, 90
0093: [email protected] = integer [email protected] to_float  // convert that value back to a float, 90.0
0087: [email protected] = [email protected]                   // get the original value so that original value is not lost, 90.4
0063: [email protected] -= [email protected]                  // subtract that value from the converted float, 90.4 - 90.0 = 0.4
0013: [email protected] *= 100.0               // multiply that value by a hundred, 0.4 * 100.0 = 40.0
0092: [email protected] = float [email protected] to_integer  // convert that value into an integer, 40
0095: make [email protected] absolute_integer  // make value absolute
if and
    // if original value is between -1.0 and 0.0
    0.0 > [email protected]
    [email protected] > -1.0
then
    if
        // if first decimal place of value is not zero
        [email protected] > 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' [email protected] [email protected] 1000 ms 1
    else
        // if original value was -0.04, this will display it instead
        02FD: text_2numbers_lowpriority 'TESTN0' [email protected] [email protected] 1000 ms 1
    end
else
    if
        // if first decimal place of value is not zero
        [email protected] > 9
    then
        // original value was 90.40, so this will display
        02FD: text_2numbers_lowpriority 'TEST' [email protected] [email protected] 1000 ms 1
    else
        // if original value was 90.04, this will display it instead
        02FD: text_2numbers_lowpriority 'TEST0' [email protected] [email protected] 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.

External links

GTA III Post.png Post on GTAForums – a CLEO script by Seemann displaying player coordinates as floats