Quick-Reference for JavaScripting in UOX3
Posted: Mon Jun 14, 2004 12:31 am
This post is merely meant as a quick-reference for UOX3-related JavaScripting. I've posted a few basic ways to achieve specific things through UOX3's JavaScript-engine, and will try to update this file with more tidbits and tricks as time goes.
First, a few links that might be interesting to potential/current JavaScripters:
A re-introduction to JavaScript
UOX3's JavaScripting Documentation
Declaring a variable:
Declaring a global variable:
NOTE: Declaring a variable outside functions in a script will make it a global variable for the entire script, and for all instances of that script
To declare a global variable from within a function, simply use:
Declaring a case insensitive string-variable:
Perform a search in a string:
Check which type of variable:
Convert the variable myVar from one type to another:
Convert variables between uppercase/lowercase:
Checking the length of a string:
Do something until condition is met:
Optional method of multiple If/then:
Extract part of a string only:
Round numbers off:
Check if a value is NOT a number:
Convert all of the elements of an Array to one joined String:
Split the elements of a string into an array:
Catching errors in your scripts
Check for existence of property, and whether said property has a value set:
Check for existence of property, but ignore value
First, a few links that might be interesting to potential/current JavaScripters:
A re-introduction to JavaScript
UOX3's JavaScripting Documentation
Declaring a variable:
var myVar = 3;
or
var myVar = "This is a string";
or
var myVar = "This is a string";
NOTE: Declaring a variable outside functions in a script will make it a global variable for the entire script, and for all instances of that script
To declare a global variable from within a function, simply use:
newVar = 5;
newVar = "This is a string";
newVar = "This is a string";
var myVar = /follow/i;
if( mySpeech.match( follow ) )
pSock.SysMessage( "The variable myVar is a "+typeof( myVar )+" variable." );
myString = myInt.toString(); //convert to string
myHex = myInt.toString( 16 ); //convert to hex-decimal
myInt = parseInt( myString ); //convert to int
myHex = myInt.toString( 16 ); //convert to hex-decimal
myInt = parseInt( myString ); //convert to int
myVar = toUpperCase( myVar );
myVar = toLowerCase( myVar );
myVar = toLowerCase( myVar );
pSock.SysMessage( "The string is "+myVar.length+" characters long." );
do {expr} while (!cond)
switch( value/variable )
{
case value1:
blah;
break;
case value2:
bleh;
break;
default:
bluh;
break;
}
{
case value1:
blah;
break;
case value2:
bleh;
break;
default:
bluh;
break;
}
var myVar = "This is a string variable.";
pUser.SysMessage( myVar.slice(5,17) );
//Produces "is a string" as the system message
pUser.SysMessage( myVar.slice(5,17) );
//Produces "is a string" as the system message
// Round to whole number:
var pi = "3.1415926535";
pi = Math.round(pi);
// Round to tenths:
var pi = "3.1415926535";
pi = Math.round(pi*10)/10;
// Round to hundredths
var pi = "3.1415926535";
pi = Math.round(pi*100)/100;
var pi = "3.1415926535";
pi = Math.round(pi);
// Round to tenths:
var pi = "3.1415926535";
pi = Math.round(pi*10)/10;
// Round to hundredths
var pi = "3.1415926535";
pi = Math.round(pi*100)/100;
if( isNaNo( myVar ) )
pUser.SysMessage( "myVar does NOT contain a number value! Woho!" );
or
if( myVar.NaN )
pUser.SysMessage( "myVar is NOT a number! Whee!" );
pUser.SysMessage( "myVar does NOT contain a number value! Woho!" );
or
if( myVar.NaN )
pUser.SysMessage( "myVar is NOT a number! Whee!" );
var myArray = new Array();
myArray[0] = "This";
myArray[1] = "is";
myArray[2] = "a";
myArray[3] = "long";
myArray[4] = "string";
myArray[5] = "in";
myArray[6] = "many";
myArray[7] = "parts.";
myVar = myArray.join( " ");
pUser.SysMessage( myVar );
myArray[0] = "This";
myArray[1] = "is";
myArray[2] = "a";
myArray[3] = "long";
myArray[4] = "string";
myArray[5] = "in";
myArray[6] = "many";
myArray[7] = "parts.";
myVar = myArray.join( " ");
pUser.SysMessage( myVar );
var myString = "Kal Vas Flam";
var SpellWrd_Array = myString.split(" "); //the contents of " " determines where to split the string. In this example a space was used.)
var SpellWrd_Array = myString.split(" "); //the contents of " " determines where to split the string. In this example a space was used.)
try
{
//Put the script code you want to test for errors here
bogusObject.x++;
}
catch( exception )
{
//Handle the error, display error message, etc
socket.SysMessage( exception );
}
finally
{
//Any code here will run whether or not an exception is thrown
}
{
//Put the script code you want to test for errors here
bogusObject.x++;
}
catch( exception )
{
//Handle the error, display error message, etc
socket.SysMessage( exception );
}
finally
{
//Any code here will run whether or not an exception is thrown
}
if( pUser.hasOwnProperty( "target" ))
pUser.TextMessage( "Target property found AND has a value!" );
else
pUser.TextMessage( "Target property either NOT found, or has no value!" );
pUser.TextMessage( "Target property found AND has a value!" );
else
pUser.TextMessage( "Target property either NOT found, or has no value!" );
if( "target" in pUser )
pUser.TextMessage( "Target property found!" );
else
pUser.TextMessage( "Target property NOT found!" );
pUser.TextMessage( "Target property found!" );
else
pUser.TextMessage( "Target property NOT found!" );