Difference between revisions of "0050"

From GTAMods Wiki
Jump to navigation Jump to search
(improved code)
Line 1: Line 1:
{{Icon|trilogy}} '''GOSUB'''
+
{{Icon|t}} '''GOSUB'''
 
<hr />
 
<hr />
<onlyinclude>{{#ifeq:{{{transcludesection|opcode}}}|opcode|
 
 
'''Description'''
 
'''Description'''
 
: Gosub
 
: Gosub
Line 11: Line 10:
 
:: The position in the script it will jump to; can also be a [[label]]
 
:: The position in the script it will jump to; can also be a [[label]]
  
This opcode allows the code to jump to a line anywhere in the script. Between that line and a RETURN ([[0051]]), this secrion acts like a simple subroutine. The subroutine is independent of the [[thread]] that the GOSUB is located. When the code reaches a RETURN, it will jump back to where the GOSUB is located and continues from there. GOSUBs can help dramatically simply repetitive code, and can help make the code cleaner and easier to read for the user. GOSUBs can be nested within GOSUBs (like a [[wikipedia:Matryoshka doll|Matryoshka doll]]) but there is a limit of up to 6 nested in each other. Nesting more will crash the game. GOSUBs are also important to the structure of [[Create a mission|missions]].
+
This opcode allows the code to jump to a line anywhere in the script. Between that line and a RETURN ([[0051]]), this section acts like a simple subroutine. The subroutine is independent of the [[thread|script]] that the GOSUB is located. When the code reaches a RETURN, it will jump back to where the GOSUB is located and continues from there. GOSUBs can help dramatically simply repetitive code, and can help make the code cleaner and easier to read for the user. GOSUBs can be nested within GOSUBs (like a [[wikipedia:Matryoshka doll|Matryoshka doll]]) but there is a limit of up to 6 nested in each other. Nesting more will crash the game. GOSUBs are also important to the structure of [[Create a mission|missions]].
}}</onlyinclude>
 
  
 
== Example ==
 
== Example ==
 
The following example, using Sanny Builder, will force a change to the colors of the specified vehicles.
 
The following example, using Sanny Builder, will force a change to the colors of the specified vehicles.
<source lang="scm">
+
<syntaxhighlight lang="scm">
 
// set constants
 
// set constants
 
const
 
const
Line 53: Line 51:
 
0229: set_car PLAYER_CAR color_to PRIM_COLOR SECD_COLOR
 
0229: set_car PLAYER_CAR color_to PRIM_COLOR SECD_COLOR
 
return
 
return
</source>
+
</syntaxhighlight>
  
 
== Tricks ==
 
== Tricks ==
There are tricks one can use that Rockstar had never employed in their scripts.
+
There are tricks one can use with GOSUB that Rockstar had never employed in their scripts.
 
=== Array ===
 
=== Array ===
 
:''See also [[VC Arrays]]''
 
:''See also [[VC Arrays]]''
The following example functions similarly to the code above but is slightly slower due to the while loop. As you can see it is more complicated but can be the preferred method if the array becomes huge.
+
The following example functions similarly to the example code above. As you can see it is more complicated but its extensibility allows for large arrays with fewer lines of code.
<source lang="scm">
+
<syntaxhighlight lang="scm">
 
// set constants
 
// set constants
 
const
 
const
Line 67: Line 65:
 
CAR_MODEL = 2@
 
CAR_MODEL = 2@
 
end
 
end
 
+
 +
COLOR_LABEL = @SET_COLOR  // gets the position of label SET_COLOR
 +
CAR_MODEL = #LINERUN  // initializes here so that this example works for III/VC/SA
 
while true
 
while true
 
     wait 10
 
     wait 10
     COLOR_LABEL = @SET_COLOR  // gets the position of label SET_COLOR
+
     if
    CAR_MODEL = #LINERUN  // so that this code works for III/VC/SA
+
        CAR_MODEL > #SENTINEL // reached past upper limit
     while #SENTINEL >= CAR_MODEL  // while CAR_MODEL is LINERUN, PEREN, or SENTINEL
+
    then
        wait 0
+
        COLOR_LABEL = @SET_COLOR  // reset position of label to beginning
         if
+
        CAR_MODEL = #LINERUN  // reset car model to beginning
            00DD:  actor $PLAYER_ACTOR driving_vehicle_type CAR_MODEL
+
     end
        then
+
    if
            03C0: PLAYER_CAR = actor $PLAYER_ACTOR car
+
         00DD:  actor $PLAYER_ACTOR driving_vehicle_type CAR_MODEL
            gosub COLOR_LABEL  // gosub to set color
+
    then
        end
+
        03C0: PLAYER_CAR = actor $PLAYER_ACTOR car
        COLOR_LABEL += 11  // add 11 bytes to go to the next set color
+
        gosub COLOR_LABEL  // gosub to set color
        // 29 02 03 00 00 04 xx 04 xx 51 00, 11 bytes, is equivalent to
 
        // 0229: PLAYER_CAR xx xx 0051: return
 
        CAR_MODEL += 1  // increment car model ide number
 
 
 
 
     end
 
     end
 +
    COLOR_LABEL += 11  // add 11 bytes to go to the next set color
 +
    // 29 02 03 00 00 04 xx 04 xx 51 00, 11 bytes, is equivalent to
 +
    // 0229: PLAYER_CAR xx xx 0051: return
 +
    CAR_MODEL += 1  // increment car model ide number
 
end
 
end
 
+
 
:SET_COLOR
 
:SET_COLOR
 
0229: set_car PLAYER_CAR color_to 0 1
 
0229: set_car PLAYER_CAR color_to 0 1
Line 95: Line 95:
 
0229: set_car PLAYER_CAR color_to 20 21
 
0229: set_car PLAYER_CAR color_to 20 21
 
return
 
return
</source>
+
</syntaxhighlight>
  
 
[[Category:OpCodes]]
 
[[Category:OpCodes]]

Revision as of 23:40, 1 January 2016

GTA III Vice City San Andreas GOSUB


Description

Gosub

Syntax

0050: gosub [int]
gosub [int]

Parameter

[int]
The position in the script it will jump to; can also be a label

This opcode allows the code to jump to a line anywhere in the script. Between that line and a RETURN (0051), this section acts like a simple subroutine. The subroutine is independent of the script that the GOSUB is located. When the code reaches a RETURN, it will jump back to where the GOSUB is located and continues from there. GOSUBs can help dramatically simply repetitive code, and can help make the code cleaner and easier to read for the user. GOSUBs can be nested within GOSUBs (like a Matryoshka doll) but there is a limit of up to 6 nested in each other. Nesting more will crash the game. GOSUBs are also important to the structure of missions.

Example

The following example, using Sanny Builder, will force a change to the colors of the specified vehicles.

// set constants
const
PRIM_COLOR = 0@
SECD_COLOR = 1@
PLAYER_CAR = 2@
end

while true
    wait 10
    if
        00DD:   actor $PLAYER_ACTOR driving_vehicle_type #LINERUN
    then
        PRIM_COLOR = 0
        SECD_COLOR = 1
        gosub @SET_COLOR
    end
    if
        00DD:   actor $PLAYER_ACTOR driving_vehicle_type #PEREN
    then
        PRIM_COLOR = 10
        SECD_COLOR = 11
        gosub @SET_COLOR
    end
    if
        00DD:   actor $PLAYER_ACTOR driving_vehicle_type #SENTINEL
    then
        PRIM_COLOR = 20
        SECD_COLOR = 21
        gosub @SET_COLOR
    end
end

:SET_COLOR
03C0: PLAYER_CAR = actor $PLAYER_ACTOR car 
0229: set_car PLAYER_CAR color_to PRIM_COLOR SECD_COLOR
return

Tricks

There are tricks one can use with GOSUB that Rockstar had never employed in their scripts.

Array

See also VC Arrays

The following example functions similarly to the example code above. As you can see it is more complicated but its extensibility allows for large arrays with fewer lines of code.

// set constants
const
PLAYER_CAR = 0@
COLOR_LABEL = 1@
CAR_MODEL = 2@
end
 
COLOR_LABEL = @SET_COLOR  // gets the position of label SET_COLOR
CAR_MODEL = #LINERUN  // initializes here so that this example works for III/VC/SA
while true
    wait 10
    if
        CAR_MODEL > #SENTINEL // reached past upper limit
    then
        COLOR_LABEL = @SET_COLOR  // reset position of label to beginning
        CAR_MODEL = #LINERUN  // reset car model to beginning
    end
    if
        00DD:   actor $PLAYER_ACTOR driving_vehicle_type CAR_MODEL
    then
        03C0: PLAYER_CAR = actor $PLAYER_ACTOR car
        gosub COLOR_LABEL  // gosub to set color
    end
    COLOR_LABEL += 11  // add 11 bytes to go to the next set color
    // 29 02 03 00 00 04 xx 04 xx 51 00, 11 bytes, is equivalent to
    // 0229: PLAYER_CAR xx xx 0051: return
    CAR_MODEL += 1  // increment car model ide number
end
 
:SET_COLOR
0229: set_car PLAYER_CAR color_to 0 1
return
0229: set_car PLAYER_CAR color_to 10 11
return                            
0229: set_car PLAYER_CAR color_to 20 21
return