Skip to content

Neat little script

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));

}

Comments

  • ForSeriousForSerious Member Posts: 446
    I have an idea.
    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.
  • TheTinmanTheTinman Member Posts: 72
    I thought I needed the oName lines so the name of the object could be changed also to spawn something in.
  • ForSeriousForSerious Member Posts: 446
    If you want to have the new object have a different name, you will need to set it after:
    object oNewObject = CreateObject(OBJECT_TYPE_CREATURE, oTAG, GetLocation(OBJECT_SELF));
    SetName(oNewObject , "New Name");
    
    Otherwise it will have the exact same name as the original object—because it's creating the object from the template defined in the toolset.

    CreateObject does have the option to change the tag if you enter a string for the last parameter.
  • ForSeriousForSerious Member Posts: 446
    Right now, it looks like your script is relying on not having objects of different types defined in the toolset.
    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.
  • TheTinmanTheTinman Member Posts: 72
    I deleted the oNAME lines and it worked just fine, but when i changed getgat to getresref it will not let me spawn in standard pallet objects anymore.
  • ForSeriousForSerious Member Posts: 446
    Humm okay. You can add some messages to see what resrefs aren't working.
    SendMessageToPC(GetFirstPC(), "ResRef is: " + oTAG);
    
    I did just read somewhere that standard items have the same tag as resref. So GetTag will work on them.
  • TheTinmanTheTinman Member Posts: 72
    Now its not working with the oName lines deleted. Tag change spawns objects, but name change does nothing now.
  • TheTinmanTheTinman Member Posts: 72
    Is there a way i can reset the variable on the pc when i exit the area so the script runs again when i come back?
  • ForSeriousForSerious Member Posts: 446
    The first thing that comes to mind is the OnExit Script for the area, but that creates a new problem of how to keep track of what variables on what object need to be reset.
    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.
  • ForSeriousForSerious Member Posts: 446
    TheTinman wrote: »
    Now its not working with the oName lines deleted. Tag change spawns objects, but name change does nothing now.
    Hold on a minute.
    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.)
  • TheTinmanTheTinman Member Posts: 72
    Im using it in the heartbeat of a placeable. Was it a standard creature?
  • ForSeriousForSerious Member Posts: 446
    Yes it was. They were both standard. The placeable though, when it made the other one, it made it in the exact same location, so the textures started looking buggy.

    What do you wish this script would do exactly? There might be another way to do it.
  • TheTinmanTheTinman Member Posts: 72
    I am using it mainly for populating my areas with monsters. I know there are great Spawning systems out there but this way is quicker. Just plop down a placeable and change its tag or name to the monster to spawn.
  • ForSeriousForSerious Member Posts: 446
    Oh okay. So your biggest problem will be finding the correct ResRef for each monster you want to spawn.
    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.
  • TheTinmanTheTinman Member Posts: 72
    The problem i am having is that i set it so it would fire only once if used in a heartbeat script. Otherwise it would fire every six seconds. I want its variables to reset when i leave the area so it will fire again when i enter again.
  • ForSeriousForSerious Member Posts: 446
    I have some ideas on that. Not sure if they will be too complicated or taxing.
    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.
  • TheTinmanTheTinman Member Posts: 72
    ForSerious wrote: »
    I have some ideas on that. Not sure if they will be too complicated or taxing.
    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));

    }

  • ForSeriousForSerious Member Posts: 446
    Nice work. Are you still putting this in the OnHeartbeat?
    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.
  • TheTinmanTheTinman Member Posts: 72
    I haven't thought much past what I've done so far. I have a respawn script on the creature so as long as a pc is in the area they will respawn. And a cleanup script so it destroys all creatures and body bags if a pc isn't there.
Sign In or Register to comment.