Help! Two quick questions for you .js masters.

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

Help! Two quick questions for you .js masters.

Post by stranf »

Ok, I have two, probably trivial questions, for you .js masters but if you could help me, it'd save me hours.


Question 1: I'm going to start magic weaopns, but not sure where to find an example of the syntax? Could someone whip me out a quick example I can use as a template.

For example, if I were to bind magic gauntlets of str to some leather gloves I'd envision the script as something like this:

Code: Select all

  OnEquip(myChar)
           {
             myChar.str = myChar.str + 5;
             myChar.TextMessage("hmm..I feel different..");
            }

            OnUnEquip(myChar)
            {
              myChar.str = myChar.str -5;
             }
Anyway, if you could show me how to properly code that, I'll get a batch of items up. Thanks in advance.


[/code]
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Post by stranf »

Question 2:

Ok, I'm working on my Loremaster script (who basically returns the name2 value of an item for quick, and cheap item identification), and I'm having a problem sending objects. Here is my code. (NOTE: portions are copied and appended from Xuri's speech_001.js so that the loremaster can have base conversation, that's why I'm starting at the end of the else if statements)

Code: Select all

   // The Loremaster conversation is appended here

               else if( Speech_Array[currObj].match( /\bjob\b/i ))
               {
               myNPC.TurnToward( myPlayer);
               myNPC.TextMessage( "I can indentify weapons and artifacts for thee.");
               return;
                }

               else if( Speech_Array[currObj].match( /\bID\b/i ) || Speech_Array[currObj].match( /\bIdentify\b/i ))
               {myNPC.TurnToward( myPlayer);
               myNPC.TextMessage ( "Let me see what thou hast.");
               ourObj = myPlayer.CustomTarget( 1, "Select an item to Identify" );

               var IdFee = 75;              //Amount of gold required to ID an object.
//               var mChar = socket.currentChar;                    //Sets up an object that contains all of the PC's data.
                 var mChar = myPlayer
                 var gold =  mChar.ResourceCount( 0xEED, 0 );        //Counts the gold carried by the PC.
                   if( gold >= IdFee )
                          {
                                myNPC.TextMessage ( "Debug: Does this work?");
                                    if( ourObj.isItem  )
                                              {
                                               myNPC ( "The true name of this " +ourObj.name+ " is " + itemObj.name2 + " .");  //ourObj.name is where the item type name is stored.  ourObj.name2 is where Uox3 stores unidentified or magic names.
                                              ourObj.name = ourObj.name2;                 //This sets the original name of the item to the magic name, so it is permenatnly Identified.  Theoretically it should work, but a bug in the latest Uox3 release prevents me from testing.
                                               mChar.UseResource( IdFee, 0xEED );         //takes the fee from the player.

      } 
   } 
                                                 else
                                                 myNPC.TextMessage( "Thou hast not enough funds.  My talents require "+IdFee+" gold ." );





               return;}





          		currObj++;
	}
}



function onCallback1(myPlayer, ourObj )                  //This is the Loremaster identify function.
{ 

  return ourObj;


Originally I placed all of the loremaster function in the OnCallback; however, this wouldn't give me access to myNPC (so I couldn't print any messages to the loremaster).

Basically if the script is run, it give me an "ourObj" is not defined error, inside the identification loop.

Somehow, as I understand it, I need to return the object's identification properties in OnCallBack1 back to the script so that I can identify the item.

Thanks again.
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

The first example looks basically right, with one minor change.

According to the JS Docs, onEquip() has two formal parameters. The first is the person equipping it, the second is the item equipped. That being the case, something like this would be correct.

Code: Select all

function onEquip( mChar, ourObj )
{
	DoStuff();
}
function( onUnEquip( mChar, ourObj )
{
	DoOtherStuff();
}
As for the second example, the problem is you are trying to get ourObj as a return value from the JS function. Unfortunately JS functions do not operate in this way (at least ours do not), thus you will need to go about it in a different way.

You have two options, you can use temporary storage on a players socket or a global variable. To create a global variable, declare it outside of a function with no var infront of it.

Code: Select all

globalObj = null;

function DoStuff( mChar )
{
	mChar.CustomTarget( 0, "Blah blah blah" );
	if( globalObj != null )
	{
		doOtherStuff();
	}
}

function onCallback0( socket, ourObj )
{
	globalObj = ourObj;
}
Scott
stranf
UOX3 Guru
Posts: 939
Joined: Wed Jan 04, 2006 3:59 pm
Has thanked: 0
Been thanked: 0

Post by stranf »

Thanks again. I'll get back to work and hopefully have some items to post by monday.
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 »

Alternative method:

Code: Select all

myNPC.TextMessage ( "Let me see what thou hast.");
myPlayer.socket.tempObj = myNPC;
myPlayer.CustomTarget( 1, "Select an item to Identify" );
and then, in the onCallback function:

Code: Select all

var myNPC = myPlayer.socket.tempObj;
<do stuff here>
-= Ho Eyo He Hum =-
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

Yes, that would be the other way to do it, but you've got it a bit backwards for what he wants, Xuri.

Should be setting the tmpObj in the callback and using it in the main function (after the custom target). :P
Scott
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 »

Well, if he does it the "backward" way, he can outsource all the stuff that should happen after the object is selected to a different function, splitting up the functions a bit instead of having everything in one huge function. =)
-= 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 »

Xuri...you think like me when I code.

I actually did it your way first. But with my limited .js knowlege I was getting MORE errors with stuff in the callback as opposed with stuff in the main function.

After 20-30 min. of trying to get it to work your way, I went to my posted method and the only problem I had was passing the object.

I think I'm going to re-code it that way, because it's simpler and I don't have to worry about global variables, etc. (It would also be easier for more skilled scripters to enhance/edit.)
Post Reply