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.
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:
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.)
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 }
Check for existence of property, and whether said property has a value set:
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!");
To reset the decaytimer on an item manually myItem.decaytime = 0;
To remove an item from a container: myItem.container = null;
(need to teleport item to desired location after)
To move an item from the ground and into a container: myItem.container = containerObj;
To move an item into a character's backpack: myItem.container = myChar.pack;
To equip an item on a character (check for conflicting item in layer first): myItem.container = myChar;
myItem.layer = <layerID>;)
To temporarily store an object in a player-character's socket (say, from one function to another, where it's not possible to include the object in the function-call): mySocket.tempObj = myObject;
or myChar.socket.tempObj = myObject;
Other temporary storage variables available on sockets: mySocket.tempInt - Temporary variable to store larger numbers (supports negatives). mySocket.xText - Temporary variable to store strings of text. mySocket.clickX - Temporary variable to store small numbers (supports negatives). mySocket.clickY - Temporary variable to store small numbers (supports negatives). mySocket.clickZ - Temporary variables to store very small numbers (-128 to 128).
function onUseChecked( pUser, iUsed ) { // Finding items within a radius of 10 tiles from pUser // Replace with AreaCharacterFunction to find characters. // Socket can be a player socket or NULL (if script runs for item/npc) // var numberOfCharsFound = AreaCharacterFunction( "findNearbyChars", pUser, 10, pUser.socket ); var numberOfItemsFound = AreaItemFunction("findNearbyItems", pUser,10, pUser.socket); pUser.TextMessage("I found "+numberOfItemsFound+" items nearby!"); returnfalse; }
var iMoreZ = iUsed.morez; var iMoreZPart1 =( iMoreZ>>24); var iMoreZPart2 =( iMoreZ>>16); var iMoreZPart3 =( iMoreZ>>8); var iMoreZPart4 =( iMoreZ%256);
In this case the container is trapped if iMoreZPart1 equals 1, while iMoreZPart2 contains the damage dealt by the trap if sprung!
Getting coordinates of target location
Coordinates of the last targeted location can be gotten via Socket properties, for instance in the onCallback#() functions. One thing to note is that there is some difference in how this behaves for older clients (which don't by default include the height of targeted tiles for the Z value) vs newer clients (which do). This means, for older client versions, the height of the tile must be added on manually via the GetTileHeight() function.
Also note that this extra step is only needed when targeting items (static or dynamic), and is not needed when a map-tile has been targeted.
var x = socket.GetWord(11); var y = socket.GetWord(13); var z = socket.GetSByte(16);
// If connected with a client lower than v7.0.9, manually add height of targeted tile if( socket.clientMajorVer<=7&& socket.clientSubVer<9) z += GetTileHeight( socket.GetWord(17));
// Display a 50% filled progress bar using two tiled gumps with different colors myGump.AddTiledGump(100,20,200,12,5055); myGump.AddTiledGump(100,20,100,12,9271);
// Display a 50% filled progress bar using HTML background colors myGump.AddHTMLGump(100,20,100,20,0,0,"<BODYBGCOLOR=black></BODYBGCOLOR>"); myGump.AddHTMLGump(100,20,50,20,0,0,"<BODYBGCOLOR=green></BODYBGCOLOR>");