I’m can’t be bothered doing a long intro so I’ll cut it
short. This will hopefully show you how to add scripting to your programs, what
kind of programs you can add scripting to, and some possible uses of scripting.
The structured programming model is one which should all be
familiar with. Even in Event-Driven languages like VB, we still use A LOT of
Structured programming is the type that goes like this:
INPUT ======> PROCESS ======> OUTPUT
The input is what the user does. Whether they type a certain
thing, click a button, move the mouse…whatever
The output is what gets displayed on screen to your users.
The process is the part we are interested in, this is where
most of your programming effort is focused. This is the part where you make
decisions, call functions, carry out certain tasks, and produce a result (not
always the case though) which will be carried on to the output part.
But, imagine if your users could control certain parts of your
Lets say you are writing a chatter bot…it has to be able to
be added to without re-compiling. You could use databases of phrases, or text
files or whatever. However by themselves, they are very limited in producing
the user’s desired results.
So you search around a bit and you think “Hey, seems that
what my program needs is its own scripting language” but who wants to spend
weeks trying to write a good scripting engine? I’ve tried and believe me, there
is no more extensive usage of the “split” “join” “mid” “left” “right” “instr”
“instrrev” and “len” functions but to name a few. Trust me here when I say even
the best written code is NOT pretty! And it’s a nightmare to debug. “Well
that’s that option out of the window. Back to text files?” I hear you ask. And
I say “NEI my hard-coding amigos! There is a better way!”.
We aren’t going to create our own scripting language; we are
going to use “VBScript” or “Jscript”. If you have the MSScript control that is included
with Visual Basic Enterprise or Professional Edition (I think) then you are in
luck! The code is simple.
I’m going to use a real life example just to demonstrate the
use of this brilliant control.
Now I go on IRC a lot and I like to amuse my fellow VB
coders in the channels or even help then or provide useful information with my
bots. For those of you who don’t know what they are, they are programs that
automatically connect to a chat room and run without any user intervention. My
bot replies to commands typed by users.
Not a single one of my commands are hard-coded into VB.
Anybody who has a compiled version can write their own commands and customize
them as they please, using a choice of languages! Here’s a simple one…a file
Function Say (Data)
Obviously my say command is a lot more complex that that, it
trims off spaces, checks for names, can add time and date and…. it does a lot
ok but moving on. This takes us back to the very beginning; you remember the
Input – Process – Output part? Well, we just wrote a function that takes input,
jiggles it about and does what it wants with it, then produces an output.
We take commands from the user, send them to this “say”
script, and return the output of the function back to the user. I have many
other larger and more complex scripts for my bot, each dealing with its own
error handling, string handling, checking and everything. Some are over 100
That is just one extremely useful…eh…use. But there are
hundreds more; you just got to use your imagination. They are extremely useful
in any AI programs I can tell you that though!
Now, how to actually implement this? Well first you add the
Script control by right clicking somewhere on your toolbox and clicking
Components. Now find it and add it and draw a copy on your form, mine is called
First thing we have to do is add our code….
Script.AddCode “Function Say(Data)” & vbcrlf & “Say
= Trim(Data)” vbcrlf & “End
Then we can keep adding functions by using the “AddCode”
method. Note: the previous code is not overwritten.
We could easily add the code from files (They don’t have to
be named .vbs or .js or whatever, they can be .dat, .txt .script, whatever you
So how do we input/output data from the function…like so:
Output = Script.Run(ProcedureName,Param1,Param2, Param3,…..)
You can add as many or as little parameters as your function
requires after the “FunctionName” parameter. It is a ParamArray. Example:
And that works nicely for my IRC bots ;)
Also a good function to know is the EVAL method. I use it to
impress my buds on IRC. Just imagine:
“Hey bot, whats sin(1 + 4) * cos(-sin(cos(100)) + 100 /
1000) – 10^2”
“That’s easy, the answer is…<ANSWER HERE>”
Only it gives the answer of course :-P
It looks quite impressive. But it can be used in programs
like calculators, or graph plotters (I have an example of one of those here on
It takes a string expression and attempts to solve it
basically. Just experiment to see what it can and can’t do!
Ok, I hope this is a clear tutorial on how to add scripting
to your programs. If you read it at all, please feel free to leave a comment J
I like to feel appreciated. Hehe
Later all. And for those who I talk to on IRC (V*r*sF*e*,
D*A*K and all the others, you know who you are, but others don’t. Nice privacy
thing there J)
I’ll have my new APIBot up soon!