Teleporting to an item. Possible?

Need help with your JScripts? Got questions concerning the DFNs? Come forward, step inside :)
Post Reply
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Teleporting to an item. Possible?

Post by stranf »

Basically when a PC double clicks on a rope or a moongate I want the .js script to search out an object (preferably by name, as it would be the most unique), get it's X, Y, Z coordinates and teleport the PC to that location.


I have a few quest ideas that involve some long and dangerous voyages at sea. The size of the UO ships is the greatest challenge to the atmoshpere of these quests. So, just out of site of green acres, I built a small cargo hold for a large dragon ship. Complete with a few cannons. This hold is surrounded by water, so it does appear that the PCs are simply inside the hold of their ship.

Teleporting from the ship to the hold is easy enough, the hold doesn't move. Simply add an object such as the hatch, add the script to it, and when the PCs click on it, it sends them to the hold. Easy as pie.

The trick is leaving the hold, especially if the ship is moving. As I understand movement in UO, a person has to be on the ship in order for it to move. So basically, an option would be to have the navigator remain on deck and steer the ship while the passangers can be in the hold and craft, practice combat, fish, etc without the necessary lag of ship travel. Then by simply clicking on an object in the hold (like the top of a staircase), the .js script would get the x-y-z value of the ship and teleport them to the ship.

I am not sure how this can be done.
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

Off the top of my head:

Code: Select all

var teleChar;

function teleportTo( toTele )
{
	teleChar = toTele;
	IterateOver( "ITEM" );
	teleChar = null;
}

function onIterate( pItem )
{
	if( pItem )
	{
		if( pItem.isItem && pItem.name == "whatever" )
		{
			teleChar.Teleport( pItem );
			return false;
		}
	}
	return true;
}
Or something like that.
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Post by stranf »

Works like a charm Giwo, thanks!!!

One thought though, is there anyway to store a pointer to this object, perhaphs in the global.js?

When the PC clicks on the steps in the hold to teleport back to the ship, UOx3 freezes for about 3-5 seconds while it searches for the object. In all honesty, I'm quite pleased the lag is only that long, considering the object count on my shard.

It would seem faster if I could store a pointer or some other similar method to access that particular object as soon as the stairs are clicked.
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

The other option is to make two items that are bound to eachother using the SetTag() method. You can store the serial for each object on its partner, then grab that when it's doubleclicked.

so something like this:

Code: Select all

function bind( item1, item2 )
{
	item1.SetTag( "hold", true );
	item1.SetTag( "holdSer1", item2.GetSerial(1) ); 
	item1.SetTag( "holdSer2", item2.GetSerial(2) ); 
	item1.SetTag( "holdSer3", item2.GetSerial(3) ); 
	item1.SetTag( "holdSer4", item2.GetSerial(4) ); 
		
	item2.SetTag( "hold", true );
	item2.SetTag( "holdSer1", item1.GetSerial(1) ); 
	item2.SetTag( "holdSer2", item1.GetSerial(2) ); 
	item2.SetTag( "holdSer3", item1.GetSerial(3) ); 
	item2.SetTag( "holdSer4", item1.GetSerial(4) ); 
}

function onUse( myChar, iUsed )
{
	if( iUsed.GetTag( "hold" ) )
	{
		var myHold = CalcItemFromSer( iUsed.GetTag( "holdSer1"), iUsed.GetTag( "holdSer2"), iUsed.GetTag( "holdSer3"),  iUsed.GetTag( "holdSer4"));
		if( myHold && myHold.isItem )
		{
			myChar.Teleport( myHold );
		}
	}
}
PS I borrowed the serial code from Xuri, as I couldn't recall what the syntax was in JS. :P
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Post by stranf »

Thanks Giwo.

I removed some debugging code from your first suggestion, and it was the debugging code that caused the 5 second delay. The original script iterates through all the objects in UOx3 and finds my teleport object in under a second. I was clearly impressed.

I believe I will stick with the first code for now and if a delay shows up, attempt to script the second.

Thanks for your help!
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

What debugging code was that, stranf?
-= Ho Eyo He Hum =-
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Post by stranf »

I had some errors to begin with, so I applied some basic "I am at the outer loop" "I am iterating inside the inner loop" text statements for every iteration.

I assumed that a text statment would add negligable time to a loop, after all in basic c++ code it is negligable. I forgot to account that an item search is done by iterating within UOx3 while outputting a text stream forces Uox3 to send a packet to the client everytime it iterates. I'm assuming this was the source of my lag. :D .

Interestingly enough, my PC only said "Iterating" 3 times, said it very quickly BEFORE the client pasued for 5 seconds. It never said "Spam Filter: You must wait awhile before talking (or whatever it usually says when your PC talks a lot)". Either it only took 3 iterations to find my object (which I doubt), or the packets send to the client were "lost" (most likely).

All in all, I am very pleased with this script. If I can figure out how to link to some picture I'll post some screenshots of what I did.
Post Reply