Difference between revisions of "Tutorial - Basic Scripting"

From FreeSpace Wiki
Jump to: navigation, search
(WIP page)
 
m (added a bit)
Line 3: Line 3:
 
==Very Basics==
 
==Very Basics==
  
 +
First take a good look at the [[Scripting.tbl]] page. From there you can see the hooks you can use for scripting. In addition to these there are few additional in [[Weapons.tbl]], namely '''$Collide Ship:''' and '''$Collide Weapon:''' but these arent supported by the 3.6.9 code branch.
 +
<br><br>
 +
All the hooks require square brackets after them into where the actual script is inserted.
 +
<br><br>
 +
'''Scripting.tbl'''
 +
<pre>$Simulation:
 +
 +
[
 +
...
 +
]</pre>
 +
 +
Commenting rest of the line away is done with double dashes, like so '''''--'''''.
  
 
==First Example==
 
==First Example==
Line 19: Line 31:
  
 
To write text (or rather a text string) to the screen we need to use certain Lua functions. The needed function '''''drawString()''''' is located in the '''Graphics''' library (abreviated '''gr'''), [http://fs2source.warpcore.org/temp/scripting.html#Graphics Graphic library in Scripting.html]. To use that function we first need to create a '''''$HUD:''''' hook where we can place it. The function also needs some arguments and we ought to give it the required arguments. First we need the string ''''Hello, World'''' followed by the location coordinates (pixels) where the game is instructed to draw the string. Also note that as the function is from the '''Graphics''' library we need to add '''''gr.''''' before the function (that is to write gr.drawString instead of just drawString).
 
To write text (or rather a text string) to the screen we need to use certain Lua functions. The needed function '''''drawString()''''' is located in the '''Graphics''' library (abreviated '''gr'''), [http://fs2source.warpcore.org/temp/scripting.html#Graphics Graphic library in Scripting.html]. To use that function we first need to create a '''''$HUD:''''' hook where we can place it. The function also needs some arguments and we ought to give it the required arguments. First we need the string ''''Hello, World'''' followed by the location coordinates (pixels) where the game is instructed to draw the string. Also note that as the function is from the '''Graphics''' library we need to add '''''gr.''''' before the function (that is to write gr.drawString instead of just drawString).
<br>
+
<br><br>
 
'''Scripting.tbl'''
 
'''Scripting.tbl'''
 
<pre>#Global Hooks
 
<pre>#Global Hooks
Line 36: Line 48:
  
 
In the first example we draw the string so that it started from the position 100, 100. Now lets draw the same string also elsewhere on the screen.
 
In the first example we draw the string so that it started from the position 100, 100. Now lets draw the same string also elsewhere on the screen.
<br>
+
<br><br>
 
'''Scripting.tbl'''
 
'''Scripting.tbl'''
 
<pre>#Global Hooks
 
<pre>#Global Hooks
Line 56: Line 68:
  
 
Now that we have several text strings drawn to the screen we might want to try to alter their colors. For this we need another function from the '''Graphics''' library, '''''setColor()''''' that accepts four arguments. These are basic RGBA values of which the A (alpha or transparency) is optional.
 
Now that we have several text strings drawn to the screen we might want to try to alter their colors. For this we need another function from the '''Graphics''' library, '''''setColor()''''' that accepts four arguments. These are basic RGBA values of which the A (alpha or transparency) is optional.
<br>
+
<br><br>
 
'''Scripting.tbl'''
 
'''Scripting.tbl'''
 
<pre>#Global Hooks
 
<pre>#Global Hooks

Revision as of 06:09, 21 August 2006

As scripting seems to be something difficult for most people to even start utilizing i thought it might be a good idea to write a short tutorial about the issue. Basic understanding of Lua or practically any other scripting or coding language is helpful but is not actually required as the internet is full of well written guides to scripting. For example see links in Scripting.tbl page. Especially the Tutorial Directory in Lua-Users wiki is very usefull in learning to use Lua scripting with FreeSpace Open.

Very Basics

First take a good look at the Scripting.tbl page. From there you can see the hooks you can use for scripting. In addition to these there are few additional in Weapons.tbl, namely $Collide Ship: and $Collide Weapon: but these arent supported by the 3.6.9 code branch.

All the hooks require square brackets after them into where the actual script is inserted.

Scripting.tbl

$Simulation:

[
...
]

Commenting rest of the line away is done with double dashes, like so --.

First Example

Lets start with creating a small script that writes text to the screen. That is the very old Hello, World example. To achieve that effect we first need to create a scripting.tbl file and then also to create the needed sections. In order to achieve that goal first create a plain text document using (for example) notepad or similar non-formatting text editor.

  • Name the empty text file as scripting.tbl
  • Move it to data/tables directory
  • Add the needed #Global Hooks and #End to the table.



Scripting.tbl

#Global Hooks

#End

Draw Text to Screen

To write text (or rather a text string) to the screen we need to use certain Lua functions. The needed function drawString() is located in the Graphics library (abreviated gr), Graphic library in Scripting.html. To use that function we first need to create a $HUD: hook where we can place it. The function also needs some arguments and we ought to give it the required arguments. First we need the string 'Hello, World' followed by the location coordinates (pixels) where the game is instructed to draw the string. Also note that as the function is from the Graphics library we need to add gr. before the function (that is to write gr.drawString instead of just drawString).

Scripting.tbl

#Global Hooks

$HUD:

[

gr.drawString('Hello, World', 100, 100)

]

#End

Drawing in Different Locations

In the first example we draw the string so that it started from the position 100, 100. Now lets draw the same string also elsewhere on the screen.

Scripting.tbl

#Global Hooks

$HUD:

[

gr.drawString('Hello, World', 100, 100)
gr.drawString('Hello, World', 130, 160)
gr.drawString('Hello, World', 160, 220)
gr.drawString('Hello, World', 190, 280)

]

#End

Drawing in Different Colors

Now that we have several text strings drawn to the screen we might want to try to alter their colors. For this we need another function from the Graphics library, setColor() that accepts four arguments. These are basic RGBA values of which the A (alpha or transparency) is optional.

Scripting.tbl

#Global Hooks

$HUD:

[

gr.setColor(0, 255, 0)
gr.drawString('Hello, World', 100, 100)
gr.setColor(255, 0, 0)
gr.drawString('Hello, World', 130, 160)
gr.setColor(0, 0, 255)
gr.drawString('Hello, World', 160, 220)
gr.setColor(255, 255, 255, 128)
gr.drawString('Hello, World', 190, 280)

]

#End

Drawing Particles

Using Statements

Drawing Scripted Weapon Effect