The Very Basics Of Scripting

the_holethe_hole Join Date: 2004-01-03 Member: 25019Members, Constellation
edited January 2005 in Scripting Discussion
<div class="IPBDescription">Exception tutorial</div> Repost from NSLearn.org
<a href='http://nslearn.org/EEpVApVkZZTFjWVVbv.shtml' target='_blank'>http://nslearn.org/EEpVApVkZZTFjWVVbv.shtml</a>
It is color coded and prettier on nslearn.org :>

People are reading this to gain knowledge as to what a script is, and how to use it. I greatly suggest that if you do not want to learn what scripting is, and do not agree with scripting, then do not read this. This is an informative thread for people to learn the very basics of scripting and Half-Life consoling.

---------------------------------

What is a script?
Basically a script is a series, or string, of commands executed by the hitting of one key.

A very basic script would be:
Bind "z" "say_team I need a medpack!; impulse 10"

All it's doing, is binding to your 'z' key, 2 commands, a basic 'say' to your teamates, and the impulse necessary to call for medpacks.
Scripting has been around since the beginning of Half-Life games and official mods, and is used in MANY different ways.


Binds:
The simplest form of console commands. Basically by typing "bind " in console, (without the <>) you have executed a bind to a key. Any key on your keyboard can have any command in the game bound to it. Right now, if you've never changed your movement keys, and you type "bind w" in console, it will display something like this:
"w" = "+forward"
It is letting you know that, bound to your 'w' key, is +forward, the basic forward movement key. Any command in the game can be rebound to any key on your keyboard, allowing more comfort. I have been using a setup like this for quite some time now.

"s" = "+back" Move backward
"d" = "+moveleft" Strafe left
"f" = "+moveright" Strafe right
"g" = "+forward" Move forward
Uncommon, I know, but comfortable for me.


The alias
Aliases are used for multiple reasons. One of the most common, is the changing and manipulating of console commands, to be easy to remember. This may be hard to comprehend, so here is a small example:

alias wa "stopsound"
bind "w" "wa"

This is the same as binding 'w' to 'stopsound', like mentioned before. So basically, in this example, alias just changes the name of a single command, but more advanced aliasing involves multiple commands aliased into one name... like this:

alias "med" "say_team I need a medpack!; impulse 10"
Bind "z" "med"

This is the exact same script from above, just formed into a simple alias that is easy to bind and unbind for whatever reason you would see fit.


Plus and minus (+, -)
In reality, for every action, there is a reaction, for every up, there is a down, and in Half-Life, for every +, there is a -. To make this as simple as possible, +attack right now is bound to your mouse1 key (the left mouse button) unless of course you've changed it. When you shoot your LMG, you hold down the key you have +attack bound to. +attack is then executed until you release, which triggers a -attack, which in turn, stops your firing. When you click your pistol, it is programmed to not fire repeatedly by you just holding down the key, so automatically after pressing +attack with your pistol, it stops firing. Now, mind you, -attack is not executed until you release the key, so +attack cannot again be executed until you release mouse1.

Scripting comes into play mostly with + and - commands, and it is the mostly what people tend to complain about. But what they always fail to realize, is that no script right now, allows you to do anything the wouldn't be possible by binding multiple keys to a command, or using just one key, it just makes it easier. When you learn scripting in every aspect, you will see what I mean.

First, before going any further, you must learn the 'wait' command. Wait is what allows you to execute one command and then another command shortly after. No two + or - commands can be executed without a wait, and this is what balances scripting, and keeps it in fine tune with what is possible by your normal hand. The wait command is basically a small halt, that prevents running one command, and running another DIRECTLY, literally DIRECTLY after the first command has been executed.
For example:

Bind "space" "+jump; wait; -jump"

This is what jump does with your space key, it executes +jump, then, due to restrictions of not allowing you to hold down space to repeatedly jump, a -jump.

**- commands completely destroy commands, and must be done for another + command to be executed.


Aliasing + and - commands
This is where scripting gets to be fun :-D. When you make an alias, you can ALSO make it to where it acts as a + and -
command, which allows even MORE flexibility. Let's bring down the health call script, and turn it into a +, - alias.

Bind "z" "say_team I need a medpack!; impulse 10"
This is what we had to start with, now lets turn it into a +, - alias.

alias +med "say_team I need a medpack!"
alias -med "impulse 10"
bind "z" "+med"

This means, that when you bind a key to +med, you press the key down (executing +), as soon as you release, a med call is sent to the commander. Understand now?

+ commands are executed by pushing the button down, - commands are executed by lifting the button up. Makes sense, right?


Advanced aliasing for + and - commands.
This is the part of scripting most commonly used. Remember the wait command? This is used alot here too. Without needing to explain, because all of that was down in the previous section, I'll get straight to the point. Your basic pistol script:

alias +pscript "+attack"
alias -pscript "-attack; wait; +attack; wait; -attack"
bind "mouse1" "+pscript"

Basically, you should already know what it does. I press mouse1, +pscript is executed (one bullet is fired, because +attack is all +pscript is), I release mouse1, -pscript is executed automatically (due to the Half-Life engine automatically executing + after -) shooting another bullet. (Recognized by the second "+attack; wait; -attack"). This script lets you shoot your pistol, two shots for every mouse click, but seeing as the pistol is capped at a certain rate of fire, you really cannot shoot as quickly as you would think you can, and it DEFINATELY isn't 2 times faster than normal shooting, because that is impossible without a speedhack. It just allows less clicking for the same, already achievable, speed.

Let's do another, your basic 3jump script:

alias jump "+jump;wait;-jump"
alias +ejump "jump;wait;jump;wait;+jump"
alias -ejump "-jump"
bind space "+ejump"

This script should easily be recognized as to what it does. First, I alias a command called "jump" which executes
"+jump; wait; -jump" (I did this so I don't have to type '+jump; wait; -jump' three times, I only have to type 'jump') I alias then, +ejump, which executes 3 +jump commands, and -ejump, which remember, is executed automatically after I release the key, kills all of the jump commands with -jump. I then bound it to spacebar.

Now, this script merely executes three jumps with every press of the space bar. No you WILL NOT jump three times. This seems to be everyone's expectations of the script, but that isn't what it does. Half-Life console commands are executed VERY quickly (depending on your FPS), and all this script does is help with timing ONE jump perfectly. A bunnyhop cannot be executed without perfect jump timing, this script just makes that timing easier.


Using the script
I know this whole time you've been wondering what to do with this commands, so I'm going to give you the most basic way to use a script.

Right click on your desktop, go to new, then text document. Open this document, and paste this script inside:

alias +pscript "+attack"
alias -pscript "-attack; wait; +attack; wait; -attack"
bind "mouse1" "+pscript"

Go to File, Save As, and where it says "Save as type" change it to say "All files". Name it "pscript.cfg" (without quotes) and save. Move pistol.cfg to your NS directory. Now executing the cfg can be done 2 ways. Every time you start NS, you can type into the console "exec pscript.cfg"
which will run all of the said commands in that configuration file, OR you can do this.

Create another config file, the same way you created this one, and name it autoexec.cfg. Inside autoexec.cfg put "exec pscript.cfg" and everytime you start NS, "exec pscript.cfg" will be ran. This is built into the Half-Life engine to automatically run autoexec.cfg upon game startup.

Attached to this post are both the autoexec.cfg and pscript.cfg, but I greatly suggest trying this no your own, and asking questions if it does not work.

Basically that's it. There is not much more involved in basic Half-Life scripting, so have a blast, and manipulate your game to your heart's desire, because it is allowed, accepted, and used by alot of NS's veterens, and pubbers alike.

---------------------------------


Attached as promised, the pistol script, and autoexec. Open with notepad after extracting to view.


Links:
<a href='http://www.csnation.net/view.php/csinfo/scripting/basic-bindlisting.csn' target='_blank'>http://www.csnation.net/view.php/csinfo/sc...bindlisting.csn</a>
This is a list of all keys bindable by the console, their ingame, and out of game names.

<a href='http://www.planethalflife.com/commands/commands/index.shtml' target='_blank'>http://www.planethalflife.com/commands/commands/index.shtml</a>
This is a link to the names of all Half-Life console commands. Remember some do not work in NS.

Comments

  • amnesiacamnesiac Join Date: 2004-11-03 Member: 32619Members
    Good read <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo--> very informative.
  • Bait_BoyBait_Boy Join Date: 2004-05-14 Member: 28672Members
    How do toggles work?
    is it possible to have a crouch toggle on one key, or do you have to bind a second key to do the -crouch?
  • MrRadicalEdMrRadicalEd Turrent Master Join Date: 2004-08-13 Member: 30601Members
    edited January 2005
    making a toggle script is very simple.

    a basic two way toggle consists of three aliases.

    First two aliases are the two items in the toggle(your on and off elements if you will). The third alias is the actual alias you bind a key to in order to use it.

    // Toggle items for a two way toggle
    alias item.1 "kill;alias toggle.items item.2"
    alias item.2 "retry;alias toggle.items item.1"
    // The recursive alias that does the toggling
    alias toggle.items "item.1"

    So inside the "item" aliases you have the command that you want to perform, and, in addition, an alias that serves as the toggle that points to the next "item"

    Then you write, again, the actual toggle alias to start the toggle and make it functional.
  • Bait_BoyBait_Boy Join Date: 2004-05-14 Member: 28672Members
  • kiddiegrinderkiddiegrinder Join Date: 2004-01-09 Member: 25181Members, Constellation
    ooooooooo very interesting <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin-fix.gif' border='0' style='vertical-align:middle' alt='biggrin-fix.gif' /><!--endemo-->
Sign In or Register to comment.