Thursday, May 8, 2008

Scripting: The Little Things

This is just a placeholder post with a quick apology regarding my May 4 post on NPC managing conversations. There was a potential problem in the script as I originally posted it, in that it was possible for the trigger to be incorrectly destroyed! It is now fixed, so please grab it again if you copied it before. My sincere apologies for not noticing earlier!

Today I have three small utility scripts that are handy in various situations. The first two are OnEnter scripts for triggers that you can use to fire off a single line of speech. The first is to get an NPC to speak a line when the PC steps on the trigger. Of course, you could use a speak trigger to do this, but this way there's no need to create a conversation file with a single line, and the trigger destroys itself upon having been fired.

// tr_en_npc1liner
/*
Description:
Makes a specified npc/object speak a line ONCE for each that PC passes over the trigger

Local Trigger Variables:
talkString - the string the chosen NPC should say
npcTag - the exact tag of the NPC to speak the line
*/
//AmstradHero - 01/04/08
void main()
{
object oPC = GetEnteringObject();
if(!GetIsPC(oPC))
return;
//The following has to be done in two lines, otherwise the string is not retrieved properly
string talk = GetLocalString(OBJECT_SELF, "talkString");
AssignCommand(GetObjectByTag(GetLocalString(OBJECT_SELF, "npcTag")), SpeakString(talk));
DestroyObject(OBJECT_SELF);
}

The second is very similar, except displays floating text above the PC instead of having an NPC speak the line. This is handy for where you want to have the PC "hear" or "notice" something as opposed to having an NPC say something.

// tr_en_pc1liner
/*
Description:
Makes each PC that passes over the trigger say the specified line once

Local Trigger Variables:
talkString - the string the chosen NPC should say
*/
//AmstradHero - 01/04/08
void main()
{
object oPC = GetEnteringObject();
if(!GetIsPC(oPC))
return;

FloatingTextStringOnCreature(GetLocalString(OBJECT_SELF, "talkString"), oPC);
DestroyObject(OBJECT_SELF);
}

The last script is designed for use in conversations. It allows you to change the first name, last name, or both names of an object with a specific tag. I feel this is a nice touch that allows you to add names dynamically to NPCs, rather than just having the PC magically know the names of everyone they meet automatically.

// ga_set_object_name
/*
Changes the first and last names of object oTag to the two supplied strings
If no object tag is specified, the change will be applied to OBJECT_SELF

AmstradHero - 08/04/08
*/
void main(string oTag, string sFirstName, string sLastName)
{
object oTarg;
if (oTag == "")
{
oTarg = OBJECT_SELF;
}
else
{
oTarg = GetObjectByTag(oTag);
}

if (sFirstName != "")
{
SetFirstName( oTarg, sFirstName);
}
if (sLastName != "")
{
SetLastName( oTarg, sLastName);
}
}

While these scripts may not look like much, they're just small enhancements to a modding toolkit that helps to improve productivity and to add that special extra touch.

No comments: