CreateObject()
Create an object of a specified type at a given location
object CreateObject();
Description
This function allows the creation of specific objects at any location within a module.
Using this function it is possible to spawn items, creatures, placeables and stores on the occurance of certain events.
In older patches, providing an invalid sTemplate, a badger would be created when trying to make a creature, and a Miscellaneous Small item when trying to make an item. Confirmed as of patch 1.30, this is no longer the case - an invalid sTemplate will simply not create anything!
Remarks
Only the following constants are valid for the nObjectType parameter:
- OBJECT_TYPE_ITEM
- OBJECT_TYPE_CREATURE
- OBJECT_TYPE_PLACEABLE
- OBJECT_TYPE_STORE
- OBJECT_TYPE_WAYPOINT (as of patch 1.30)
If sNewTag is specified (other than the default ""), the created object will be given that new tag.
Be careful with 'sTemplate', or you may find yourself with a lot more Badgers than you want. A ResRef is a unique key into the palette of objects that comes with the engine and your module. If you attempt to look up the ResRef of a standard placeable or creature, it's natural to right-click the object in the palette and look for a 'Properties' option to examine. It isn't there, and the next-best-thing is very tempting - 'Edit Copy'. Sure enough, 'Edit Copy' gives you the appropriate object properties dialogue, and there's a blueprint ResRef, for example "SKELETON001".
Unfortunately, the Toolset has done what it said it would - shown you a copy of the blueprint, not a copy of an object created using blueprint, as you might expect. So your ResRef is in fact a toolset-generated reference string for a new blueprint - one which promptly disappears when you cancel the properties screen to go back to the Script Editor. To make matters that little bit more confusing, by default the Tag of an object created from a standard blueprint is the same as the blueprint ResRef, which might lead you to mistake a Tag for a ResRef.
The easiest way to find the ResRef for a resource is to click on the "View" menu and select the "Show Preview Window" menu item. This will cause a preview window to appear which details not only the ResRef, but the Name, Tag, and Comments for a particular resource (when available). You can view properties by clicking on an already existing object or choosing an object from the various palettes within the toolset.
Known Bugs
As of patch 1.30, bUseAppearAnimation finally works, though there’s been reports that some of the appear animations are a little weird.
According to Bioware, there was a problem creating waypoints with mappins. This is not mentioned in the release notes for patch 1.31. The problem still persists in 1.61.
Creating a creature during the OnEnter event without first checking to see what's actually entering the area causes NWN to enter an infinite loop of creating creatures (as each creature created causes the OnEnter event to fire its attached script again). This can be avoided by testing if GetIsPC(GetEnteringObject()) is true (if its true, then create the creature(s); if its false then a creature that was created fired the event).
Version
1.31
Example
// The following script will create a chicken next to waypoint. // NOTE: The following script requires the use of a Waypoint with // the tag Waypoint1 in the module. // DO NOT PLACE THIS CODE WITHIN A SCRIPT USED FOR // AN AREA'S OnEnter EVENT! (see "Known Bugs" above) int nObjectType = OBJECT_TYPE_CREATURE; string strTemplate = "nw_chicken"; // Standard chicken location locLocation= GetLocation(GetObjectByTag("Waypoint1")); // Find the location of the waypoint int bUseAppearAnimation = TRUE; CreateObject(nObjectType, strTemplate, locLocation, bUseAppearAnimation);
See Also
functions: | ActionCreate | CopyObject | CreateObjectVoid |
author: Dave Withnall, editor: Grimlar, additional contributor(s): Jit Fong Oon, Jazael, Stephen Fritz
Send comments on this topic.