Basic Scripting Tutorial
From Dim3 Wiki
This is a basic scripting tutorial for anyone who wants to learn.
Contents |
[edit] Basics
This section teaches what the basic commands do.
if (question here) {
//do something
}
This means "If question is true then do something".
For example:
//This is a comment, it doesn't effect the script
var a=1; //this makes a new variable called a and sets it to 1
var b=1; //this makes a new variable called b and sets it to 1
if (a==b) { //if a=b then
a=3; //this makes a=3
} //closing bracket, end of what you do if a=b
Note that == checks for equality, but = sets the variable.
[edit] Strings, Integers, And Booleans
This section teaches what strings integers and booleans are
var a; //this could be a string integer or boolean var b=true; //this is a boolean, booleans are either true or false var c=1; //this is an integer, integers are numbers var d='You are currently on level one'; //this is a string, strings are text
[edit] Else and Simplifications
This section is somewhat advanced compared to the first section, read this after you understand the basics.
if (a==true) {
b=2000;
} else {
b=3000;
}
This says "If a is true make b 2000, otherwise set b to 3000".
if (count==true) {
otherVariable=2000;
}
//Well, this code can be simplified lots.
if (count) { //saying if(variable) { asks if that variable is true
otherVariable=2000;
}
//It can still be simplified, you can use
if (count) otherVariable=2000; //This does that in one line, use this when theres only only one command after the if and there is no else
//Not if (variable!=1) do something; //says "if the variable is NOT 1 then do something" //You can also use if (!variable) do something; //this says if the variable is false then do something
x=x+y//This can be simplified x+=y //This also applys to - * / and %
//You can also do this x=x++ //this sets y to x+1 //this applys to + and -
[edit] Functions and Calling them
This section is about functions and calling them.
function characterConstruct(obj)
{
//code goes inside functions, you make variables out of them but all other code should be inside functions
//functions are like sections, you can call them whenever you want.
otherFunction(obj); //this tells the script to go to the function called otherFunction.
//The "obj" written in the function name is the api to load, api will be discussed in the next section }
[edit] API
API stands for application programming interface, it's what does anything to the game
Api are things like
obj.motionVector.walkToPlayer
they do stuff to the game, anything, from moving models (obj.motionVector.go() to putting text on the screen (iface.text.set(Text Name,'text here')).
API can be found in the documentation, click on the scripting section then look at all the sections (obj, modle, iface). Api works like this:
section.subSection.command(parameters);
Example:
obj.
then scroll down to motionvector
obj.motionVector
(capitalazation counts) then find turnToObject
obj.motionVector.turnToObject
then check the parameters (not all API have parameters)
obj.motionVector.turnToObject(You would put an object ID here);
(Object IDs can be found using various APIs)
When naming a function, you put the section of the API you need to load in the name .
[edit] Other Stuff
Theres alot more you can learn Dim3 scripts always start at the function called "event".
//This is an example of a switch
switch (variableName) { //this starts a switch with the name "variableName"
case 1: //if variableName is 1
//do something
return;
case 2: //if variableName is 2
//do something
return;
case 3: //if variableName is 3
//do something
return;
}
There are other commands like if, (while, for) but if you feel you want to become more advanced google them. Although these commands are very useful, you probably won't need them until you're a more advanced scripter.
[edit] Applying this knowledge
I recommend starting out by modifying the demo scripts, then when you feel you get it starting your own scripts .
[edit] And
if (a && b) obj.motionVector.walkToPlayer(); //says if a is true AND b is true, walk to the player
Theres also an or,
if (a || b) do something; //This says "If a is true or b is true then do something"
[edit] Java and Javascript are 2 Different Things, dim3 Uses Javascript
For more info try the w3 schools site: http://www.w3schools.com/js/default.asp
