Difference between revisions of "Create a script"

From GTAMods Wiki
Jump to navigation Jump to search
(See Also)
(updating tutorial)
Line 5: Line 5:
 
<source lang="scm">create_thread</source>
 
<source lang="scm">create_thread</source>
 
Insert before it
 
Insert before it
<source lang="scm">004F: create_thread @mythread</source>
+
{| style="margin-right:auto"
''mythread'' is an arbitrary [[label]]. You can name the label with anything.
+
| width="500px" | <source lang="scm">
 +
004F: create_thread @mythread
 +
</source>
 +
| width="100px" style="text-align:center" | or
 +
| width="500px" | <source lang="scm">
 +
create_thread @mythread
 +
</source>
 +
|}
 +
 
 +
''mythread'' is an arbitrary [[label]]. It merely helps the game locate your thread. You can name the label anything.
  
 
== Insert your contents ==
 
== Insert your contents ==
 
Next you have to insert the contents into your thread. Find
 
Next you have to insert the contents into your thread. Find
 
<source lang="scm">//-------------Mission 0---------------</source>
 
<source lang="scm">//-------------Mission 0---------------</source>
That is where the MAIN section ends and the first mission begins. Insert your contents between it. The simplest format of a thread have this format
+
That is where the MAIN section ends and the first mission begins. Insert your contents between it. The simplest form of a thread has this format:
<source lang="scm">:mythread
+
{| style="margin-right:auto"
 +
| width="500px" | <source lang="scm">
 +
:mythread
 
// Insert your contents here
 
// Insert your contents here
004E: end_thread</source>
+
004E: end_thread
 +
</source>
 +
| width="100px" style="text-align:center" | or
 +
| width="500px" | <source lang="scm">
 +
:mythread
 +
// Insert your contents here
 +
end_thread
 +
</source>
 +
|}
 +
 
 
The contents can include simple [[opcode]]s or longer threads like in the examples of [[009A|creating a ped]] and [[00A5|creating a vehicle]].
 
The contents can include simple [[opcode]]s or longer threads like in the examples of [[009A|creating a ped]] and [[00A5|creating a vehicle]].
  
== Thread names ==
+
; Thread names
Your thread can have a name. Use opcode [[03A4]] to name your thread. It is essential if you need to end your thread from another part of the script with opcode [[0459]].
+
<hr/>
 +
It is optional for your thread to have a name. Use opcode [[03A4]] to name your thread. A maximum of 8 characters is allowed for the name. If needed, you can end your thread from another part of the script with opcode [[0459]].
 +
{| style="margin-right:auto"
 +
| width="500px" | <source lang="scm">
 +
:mythread
 +
03A4: name_thread 'MYTHREAD'
 +
 
 +
:mythread_start
 +
// Insert your contents here
 +
004E: end_thread
 +
</source>
 +
| width="100px" style="text-align:center" | or
 +
| width="500px" | <source lang="scm">
 +
:mythread
 +
thread 'MYTHREAD'
 +
 
 +
:mythread_start
 +
// Insert your contents here
 +
end_thread
 +
</source>
 +
|}
  
 
== Loops ==
 
== Loops ==
The example above shows you a thread that ends straight away. If you want the thread to run continuously, you have to loop the thread. For the most part, looping the thread requires opcode [[0001]] (or ''wait'' command) to be placed somewhere within the loop or else the game will crash. There are exceptions but it is safer to have it. The simplest loop has this format
+
The example above shows you a thread that ends straight away. If you want the thread to run continuously, you have to loop the thread. For the most part, looping the thread requires opcode [[0001]] (or ''wait'' command) to be placed somewhere within the loop or else the game will crash. There are exceptions but it is safer to have it. The simplest loop has this format:
<source lang="scm">:mythread
+
{| style="margin-right:auto"
 +
| width="500px" | <source lang="scm">
 +
:mythread
 
0001: wait 0 ms
 
0001: wait 0 ms
 
// Insert your contents here
 
// Insert your contents here
0002: jump @mythread</source>
+
0002: jump @mythread
 +
</source>
 +
| width="100px" style="text-align:center" | or
 +
| width="500px" | <source lang="scm">:mythread
 +
while true
 +
    wait 0
 +
    // Insert your contents here
 +
end
 +
</source>
 +
|}
 +
 
 
This thread will repeat itself indefinitely so be careful what you put in it.
 
This thread will repeat itself indefinitely so be careful what you put in it.
  
 
== Conditions ==
 
== Conditions ==
Conditional opcodes checks whether the action is performed rather than to perform the action. In Sanny Builder, they are noted by spaces between the opcode and the description of the opcode. Conditions start with IF statements that checks if an action is performed.
+
Conditional opcodes checks whether the action is performed rather than to perform the action. If the condition is satisfied, it returns true, otherwise it returns false. In Sanny Builder, conditional opcodes are noted by spaces between the opcode and the description of the opcode. Conditions start with IF statements that checks if an action is performed.
<source lang="scm">:MyThread
+
<source lang="scm">
 +
:mythread
 
0001: wait 0 ms
 
0001: wait 0 ms
 
00D6: if
 
00D6: if
00E1:  player 0 pressed_key 4  // Conditional opcode
+
// Conditional opcode, i.e.
004D: jump_if_false @CheckEnd
+
00E1:  player 0 pressed_key 13
// Command
+
004D: jump_if_false @check_failed
 
+
// Command, if the condition returns true, i.e. if the key is being pressed, then add $2000
:CheckEnd
+
0109: player $PLAYER_CHAR money += 2000
0002: jump @MyThread</source>
+
0002: jump @threadend
This shows if '''one''' condition (if key 4 is pressed in this example) is met, the command will be performed. Otherwise, the code will skip the command and jump to label ''CheckEnd''.
+
 
 +
:check_failed
 +
// Command, if the condition returns false, i.e if the key is not being pressed, then subtract $10
 +
0109: player $PLAYER_CHAR money += -10
 +
 
 +
:threadend
 +
0002: jump @mythread
 +
</source>
 +
or
 +
<source lang="scm">
 +
:mythread
 +
while true
 +
    wait 0
 +
    if
 +
        // Conditional opcode, i.e.
 +
        00E1:  player 0 pressed_key 13
 +
    then
 +
        // Command if the condition returns true, i.e. if the key is being pressed, then add $2000
 +
        0109: player $PLAYER_CHAR money += 2000
 +
    else
 +
        // Command if the condition returns false, i.e if the key is not being pressed, then subtract $10
 +
        0109: player $PLAYER_CHAR money += -10
 +
    end
 +
end
 +
</source>
 +
 
 +
This shows if '''one''' condition is met (if the CAMERA key is pressed), the condition is true and the first command will be performed (add $2000). Otherwise, the condition would be false and reaches the alternate command (subtract $10).
  
 
For IF statements with more than one conditions, you need to either add ''and'' or ''or'' after ''if''.
 
For IF statements with more than one conditions, you need to either add ''and'' or ''or'' after ''if''.

Revision as of 17:50, 11 January 2012

Creating a simple thread is one of the first steps in understanding how to code. This article will show you the basic steps on how to create the simplest thread using Sanny Builder. It should work for GTA 3, Vice City, and San Andreas.

Create your thread

First create your thread using opcode 004F (or create_thread command). Find

create_thread

Insert before it

004F: create_thread @mythread
or
create_thread @mythread

mythread is an arbitrary label. It merely helps the game locate your thread. You can name the label anything.

Insert your contents

Next you have to insert the contents into your thread. Find

//-------------Mission 0---------------

That is where the MAIN section ends and the first mission begins. Insert your contents between it. The simplest form of a thread has this format:

:mythread
// Insert your contents here
004E: end_thread
or
:mythread
// Insert your contents here
end_thread

The contents can include simple opcodes or longer threads like in the examples of creating a ped and creating a vehicle.

Thread names

It is optional for your thread to have a name. Use opcode 03A4 to name your thread. A maximum of 8 characters is allowed for the name. If needed, you can end your thread from another part of the script with opcode 0459.

:mythread
03A4: name_thread 'MYTHREAD'

:mythread_start
// Insert your contents here
004E: end_thread
or
:mythread
thread 'MYTHREAD'

:mythread_start
// Insert your contents here
end_thread

Loops

The example above shows you a thread that ends straight away. If you want the thread to run continuously, you have to loop the thread. For the most part, looping the thread requires opcode 0001 (or wait command) to be placed somewhere within the loop or else the game will crash. There are exceptions but it is safer to have it. The simplest loop has this format:

:mythread
0001: wait 0 ms
// Insert your contents here
0002: jump @mythread
or
:mythread
while true
    wait 0
    // Insert your contents here
end

This thread will repeat itself indefinitely so be careful what you put in it.

Conditions

Conditional opcodes checks whether the action is performed rather than to perform the action. If the condition is satisfied, it returns true, otherwise it returns false. In Sanny Builder, conditional opcodes are noted by spaces between the opcode and the description of the opcode. Conditions start with IF statements that checks if an action is performed.

:mythread
0001: wait 0 ms
00D6: if
// Conditional opcode, i.e.
00E1:   player 0 pressed_key 13
004D: jump_if_false @check_failed
// Command, if the condition returns true, i.e. if the key is being pressed, then add $2000
0109: player $PLAYER_CHAR money += 2000
0002: jump @threadend

:check_failed
// Command, if the condition returns false, i.e if the key is not being pressed, then subtract $10
0109: player $PLAYER_CHAR money += -10

:threadend
0002: jump @mythread

or

:mythread
while true
    wait 0
    if
        // Conditional opcode, i.e.
        00E1:   player 0 pressed_key 13
    then
        // Command if the condition returns true, i.e. if the key is being pressed, then add $2000
        0109: player $PLAYER_CHAR money += 2000
    else
        // Command if the condition returns false, i.e if the key is not being pressed, then subtract $10
        0109: player $PLAYER_CHAR money += -10
    end
end

This shows if one condition is met (if the CAMERA key is pressed), the condition is true and the first command will be performed (add $2000). Otherwise, the condition would be false and reaches the alternate command (subtract $10).

For IF statements with more than one conditions, you need to either add and or or after if.

if and means if all of the conditions are met.

:MyThread
0001: wait 0 ms
00D6: if and
00E1:   player 0 pressed_key 4  // First condition
00E1:   player 0 pressed_key 19  // Second condition
004D: jump_if_false @CheckEnd
// Command
  
:CheckEnd
0002: jump @MyThread

This shows that if all conditions (if both keys 4 and 19 are pressed in this example) are met, the command will be performed. Otherwise, the code will skip the command and jump to label CheckEnd.

if or means if either one of these conditions are met.

:MyThread
0001: wait 0 ms
00D6: if or
00E1:   player 0 pressed_key 4  // First condition
00E1:   player 0 pressed_key 19  // Second condition
004D: jump_if_false @CheckEnd
// Command
  
:CheckEnd
0002: jump @MyThread

This shows that if either condition (if either key 4 or key 19 is pressed in this example) is met, the command will be performed. Otherwise, the code will skip the command and jump to label CheckEnd.

Opcodes normally starts with the number 0, but conditional opcodes can start with the number 8. This checks if the condition is not performed.

00E1:   player 0 pressed_key 4 // IS pressed
80E1:   not player 0 pressed_key 4 // is NOT pressed

Final Notes

Using this format requires you to start a new game. If you do not understand what is being said here, try looking into the Tutorial Forum for more in-depth tutorials or the Mission Coding Forum on how to understand this.

See also