Math operations

From GTAMods Wiki
Revision as of 00:16, 16 June 2015 by Seemann (talk | contribs) (Timed opcodes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

From GTA III to Vice City Stories, these games support limited math operations through the use of opcodes in the SCM file. This language is unlike most programming languages so it may take a while to learn how to perform some math operations. Sanny Builder supports the use of some of the operations without the need to type in the opcode in front. On this page, these supported operations have underlines in the "Related opcodes" section. All examples on this page uses Sanny Builder.

All math operations should be done either using integers only or floats only. Otherwise the result will not be comprehensible. When using opcodes, make sure your variable type is supported by the opcode. Opcodes that calculate using global variables explicitly shouldn't be using local variables and vice versa.

Addition

1 + 4 = 5

0@ = 1  // stores the number 1 into the local variable 0@
0@ += 4  // adds 4 to the variable 0@, which stored the number 1

0@ now equals to 5.

Related opcodes

  • 0008: global variable += integer
  • 0009: global variable += float
  • 000A: local variable += integer
  • 000B: local variable += float
  • 0058: global variable integer += global variable integer
  • 0059: global variable float += global variable float
  • 005A: local variable integer += local variable integer
  • 005B: local variable float += local variable float
  • 005C: local variable integer += global variable integer
  • 005D: local variable float += global variable float
  • 005E: global variable integer += local variable integer
  • 005F: global variable float += local variable float

Subtraction

5.5 - 8.0 = -2.5

0@ = 5.5  // stores the number 5.5 into the local variable 0@
0@ -= 8.0  // subtracts 8.0 (remember, calculate either in integers only or floats only) to the variable 0@, which stored the number 5.5

0@ now equals to -2.5.

Related opcodes

  • 000C: global variable -= integer
  • 000D: global variable -= float
  • 000E: local variable -= integer
  • 000F: local variable -= float
  • 0060: global variable integer -= global variable integer
  • 0061: global variable float -= global variable float
  • 0062: local variable integer -= local variable integer
  • 0063: local variable float -= local variable float
  • 0064: local variable integer -= global variable integer
  • 0065: local variable float -= global variable float
  • 0066: global variable integer -= local variable integer
  • 0067: global variable float -= local variable float

Multiplication

0@ * 1@ = 20, where 0@ equals 5 and 1@ equals 4

var
0@ : integer  // declare variables as integers
1@: integer
end
0@ = 5
1@ = 4
1@ *= 0@  // multiply 1@ to 0@ and stores the result in 1@

1@ now equals to 20.

Related opcodes

  • 0010: global variable *= integer
  • 0011: global variable *= float
  • 0012: local variable *= integer
  • 0013: local variable *= float
  • 0068: global variable integer *= global variable integer
  • 0069: global variable float *= global variable float
  • 006A: local variable integer *= local variable integer
  • 006B: local variable float *= local variable float
  • 006C: local variable integer *= global variable integer
  • 006D: local variable float *= global variable float
  • 006E: global variable integer *= local variable integer
  • 006F: global variable float *= local variable float

Division

$var / 0@ = 6.0, where $var equals 13.8 and 1@ equals 2.3.

var
$var : float
0@: float
end
$var = 13.8
0@ = 2.3
$var /= 0@

$var now equals to 6.0.

Related opcodes

  • 0014: global variable /= integer
  • 0015: global variable /= float
  • 0016: local variable /= integer
  • 0017: local variable /= float
  • 0070: global variable integer /= global variable integer
  • 0071: global variable float /= global variable float
  • 0072: local variable integer /= local variable integer
  • 0073: local variable float /= local variable float
  • 0074: local variable integer /= global variable integer
  • 0075: local variable float /= global variable float
  • 0076: global variable integer /= local variable integer
  • 0077: global variable float /= local variable float

Absolute value

| -2.0 | = 2.0

0@ = -2.0
0097: make 0@ absolute_float

0@ now equals to 2.0

Related opcodes

  • 0094: global variable integer
  • 0095: local variable integer
  • 0096: global variable float
  • 0097: local variable float

Sine and cosine

  • Sine and cosine opcodes use degrees, not radians. They should be floats, not integers.
0@ = 30.0
02F6: 0@ = sine 0@

0@ now equals to 0.5

Related opcodes

  • 02F6: sine
  • 02F7: cosine
  • 02F8: car's z angle sine
  • 02F9: car's z angle cosine

Timed opcodes

Adding a value to the variable is FPS-dependent when placed in a loop with zero delay (wait 0), as such a loop iterates over more often on high frame rate. With the frame limiter being turned off, the variable value rises rapidly, and this effect is undesirable. That is the reason, timed value opcodes are introduced to vanish the difference in calculations on different FPS. The timed value is multiplied by the current value of the fTimeStep[*] variable which is inversely proportional to the frame rate. The higher frame rate, the timed value is lower, and vice versa. So incrementing or decrementing variables in a loop is generally better with timed opcodes rather than usual ones.

Related opcodes

The following opcodes only work with floating-point values.

  • 0078: global variable += timed float
  • 0079: local variable += timed float
  • 007A: global variable += timed global variable
  • 007B: local variable += timed local variable
  • 007C: local variable += timed global variable
  • 007D: global variable += timed local variable
  • 007E: global variable -= timed float
  • 007F: local variable -= timed float
  • 0080: global variable -= timed global variable
  • 0081: local variable -= timed local variable
  • 0082: local variable -= timed global variable
  • 0083: global variable -= timed local variable

^ Post.png Post on GTAForums – a post by Seemann describing how the timer function works