Difference between revisions of "0051"

From GTAMods Wiki
Jump to navigation Jump to search
m (Added implementation and linked to gosub)
 
Line 8: Line 8:
  
 
This opcode returns from the current subroutine.
 
This opcode returns from the current subroutine.
When the thread encounters a [[0051]], it will return from where the last [[0050]] was called.
+
When the script encounters a [[0051]], it will return from where the last [[0050]] was called.
  
 
The game will crash if:
 
The game will crash if:

Latest revision as of 15:07, 1 July 2021

GTA III Vice City San Andreas RETURN


Description
Return from the current subroutine.
Syntax
0051: return
Parameter
none

This opcode returns from the current subroutine. When the script encounters a 0051, it will return from where the last 0050 was called.

The game will crash if:

  • 0051 was read before any 0050 was called.

Implementation

See gosub for further details.

/* Note: This is not real code and will not compile. */
/* Based on decompilation of SA v2.0 */

/*
  The game will crash if the call stack is empty
  (i.e. script->callStackPosition == 0), because
  script->callStackPosition - 1 would be -1, and
  therefore an invalid index.

  This can happen if "return" is used outside a
  subroutine.
*/

// Move up to the previous stack frame.
script->callStackPosition = script->callStackPosition - 1;

// Set the current offset to the previous one.
// The previous offset will be the offset of the next instruction after the ''gosub''.
script->readPointer = (&script->callStack)[script->callStackPosition];

Keywords

subroutine, gosub, go sub