Neat little script
TheTinman
Member Posts: 74
A neat little script i made that could have multiple uses. Im not a great scripter so any advice would be welcome.
// Neat little script that could have many uses.
// Set up so objects can be spawned by a name change or a tag change or both.
// It can be put in multiple script places. Set it up to fire once in case it was used in a heartbeat script.
// If used with a trigger the spawn point is the start point of the drawn trigger.
void main()
{
// The PC.
object oPC;
// Only fire once per PC.
if ( GetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC)) )
return;
SetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC), TRUE);
// Tag of object the script is attached to.
string oTAG = GetTag(OBJECT_SELF);
// Name of object the script is attached to.
string oNAME = GetName(OBJECT_SELF);
// Change objects tag to a standard pallet creatures tag. Change objects tag with the ResRef of a custom creature.
CreateObject(OBJECT_TYPE_CREATURE, oTAG, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard placeable. Change objects tag to the ResRef of a custom placeable.
CreateObject(OBJECT_TYPE_PLACEABLE, oTAG, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard item. Change objects tag to the ResRef of a custom item.
CreateObject(OBJECT_TYPE_ITEM, oTAG, GetLocation(OBJECT_SELF));
// Change objects name to a standard pallet creatures tag. Change objects name with the ResRef if a custom creature.
CreateObject(OBJECT_TYPE_CREATURE, oNAME, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard placeable. Change objects tag to the ResRef of a custom placeable.
CreateObject(OBJECT_TYPE_PLACEABLE, oNAME, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard item. Change objects tag to the ResRef of a custom item.
CreateObject(OBJECT_TYPE_ITEM, oNAME, GetLocation(OBJECT_SELF));
}
// Neat little script that could have many uses.
// Set up so objects can be spawned by a name change or a tag change or both.
// It can be put in multiple script places. Set it up to fire once in case it was used in a heartbeat script.
// If used with a trigger the spawn point is the start point of the drawn trigger.
void main()
{
// The PC.
object oPC;
// Only fire once per PC.
if ( GetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC)) )
return;
SetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC), TRUE);
// Tag of object the script is attached to.
string oTAG = GetTag(OBJECT_SELF);
// Name of object the script is attached to.
string oNAME = GetName(OBJECT_SELF);
// Change objects tag to a standard pallet creatures tag. Change objects tag with the ResRef of a custom creature.
CreateObject(OBJECT_TYPE_CREATURE, oTAG, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard placeable. Change objects tag to the ResRef of a custom placeable.
CreateObject(OBJECT_TYPE_PLACEABLE, oTAG, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard item. Change objects tag to the ResRef of a custom item.
CreateObject(OBJECT_TYPE_ITEM, oTAG, GetLocation(OBJECT_SELF));
// Change objects name to a standard pallet creatures tag. Change objects name with the ResRef if a custom creature.
CreateObject(OBJECT_TYPE_CREATURE, oNAME, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard placeable. Change objects tag to the ResRef of a custom placeable.
CreateObject(OBJECT_TYPE_PLACEABLE, oNAME, GetLocation(OBJECT_SELF));
// Change objects tag to the tag of a standard item. Change objects tag to the ResRef of a custom item.
CreateObject(OBJECT_TYPE_ITEM, oNAME, GetLocation(OBJECT_SELF));
}
0
Comments
Because CreateObject uses only the res ref (Resource Reference) to create an object, you should change GetTag to GetResRef.
You don't need any of the lines that use oNAME because they are redundant.
CreateObject does have the option to change the tag if you enter a string for the last parameter.
If there is an object with a resref of 'cool' that is an item but also another with the same resref that's a placeable, this script will create them both in the same spot, if it is called on either.
The easy way to do it would be a timer. Just delay setting the variable back to false for how ever long you think is long enough.
How exactly are you using this script? I just tried a dubbed-down version using GetResRef on a placeable in the OnUsed slot, and it worked. Then I did a creature with it in the OnDeath slot and it worked. (Of course it only worked once because I didn't replace the default OnDeath script so the new creature that spawned just died like normal.)
What do you wish this script would do exactly? There might be another way to do it.
The toolset doesn't show you correctly because when you try to edit a monster, you can only edit copy. That makes a copy with a different ResRef and shows you that.
What I suggest is filling an area with all the creatures you want the ResRefs for.
Make a while loop using GetFirstObjectInArea and GetNextObjectInArea. If the object is a creature, have it WriteTimestampedLogEntry of GetResRef.
Once that script runs, you can go grab your log file in ~\Documents\Neverwinter Nights\logs\nwclientLog1.txt. It will have all those ResRefs for easy copy pasting.
Remove the do once logic from this script.
Then create either an OnHeartbeat or OnEnter OnExit scripts for the area.
The OnEnter/OnExit approach would be like: OnEnter check if DoOnce is already true, if not set it to true and execute the spawning script on all the placeables. OnExit check if there are still any PCs left in the area, if not set DoOnce back to false.
For your placeables, I know you are using the tag, but you can define local variables in the toolset. So you can make a custom placeable and use its tag to get all of the instances of it in an area, then check the local variable on each instance and use that to create the creature from.
I am curious why normal encounters are not sufficient.
I just thought it would be neat if i could spawn creatures anywhere a placeable is. Thanks for your advice. I reworked the script to only spawn in creatures. ( Didnt want placeables to respawn when the variable was changed. )
//On Area Exit
void main()
{
object oPC;
object oArea = OBJECT_SELF;
object oObject = GetFirstObjectInArea(oArea);
while(GetIsObjectValid(oObject))
{
if(GetTag(oObject) == "Spawner")
{
SetLocalInt(oObject, "DO_ONCE__" + ObjectToString(oPC), FALSE);
}
oObject = GetNextObjectInArea(oArea);
}
}
// If used with a trigger the spawn point is the start point of the drawn trigger.
// The tag of the object this script goes on must be "Spawner"
void main()
{
// The PC.
object oPC;
// Only fire once per PC.
if ( GetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC)) )
return;
SetLocalInt(OBJECT_SELF, "DO_ONCE__" + ObjectToString(oPC), TRUE);
// Name of object the script is attached to.
string oNAME = GetName(OBJECT_SELF);
// Change objects name to a standard pallet creatures tag. Change objects name with the ResRef if a custom creature.
CreateObject( OBJECT_TYPE_CREATURE, oNAME, GetLocation(OBJECT_SELF));
}
Note that oPC never gets defined in either of the scripts. So it should work as intended for one player, but if the idea is to spawn two creatures for two players, three for three and so on, you'll need another loop to find all the valid PCs in the area.