Spawn
This page uses Vector3, GameObject from Unity. The glossary says what each one is.
Spawn.Item drops new items into the world at a position, the way the game drops an item.
Spawn drops items into the world
Use Item to drop items into the world at a position.
public static class Spawn
{
public static bool Item(int itemId, int count, Vector3 position);
public static bool Item(int itemId, int count, Vector3 position, out GameObject spawned);
}
| Method | Returns | What it does |
|---|---|---|
Item(itemId, count, position) | bool | Drops the items. Returns true when they were placed. |
Item(itemId, count, position, out spawned) | bool | The same, and also gives you the first object it made. |
Things to know
Itemdropscountof an item atposition, and the player can pick them up. The game makes each one, starts its expiry timer, and registers it so it is saved. (it builds each stack the way the game’s drop does, withItemOperations.InstantiateItemandSetCollectibleItemValues, the steps ofSpawnItemToPosition; the game registers it inDestructibleList)- The
out GameObject spawnedversion also gives you the first object it made. It isnullwhen nothing was made. Its id for other APIs comes fromWorldItems.IdOf(see World items). - Spawned items use the game’s expiry, so they disappear unless the player picks them up.
WorldItems.SetExpiryOverridechanges the time for later default spawns (see World items, Expiry default). - Spawned items have no save id. A save id on a spawned item would stop the game from saving it: the game would look for a scene object that does not exist when the save loads.
Limits
countmust be 1 to 100 for each call, orItemthrows anArgumentException. The limit is for one call only. There is no limit on how many calls you make, and every call drops its items at the sameposition. The items are split into stacks of at most the item’s stack size. (the stack size isItem.Stackable)Itemreturnsfalsewhen it can’t place the item. That is on the main menu, while a save is loading or being written, for an unknown item id, and when the game’s own call fails (the error goes to the log).- The coins (item ids 0 and 1) are refused. Use
Walletfor money. - The position is used as given. The game does not move the item down to the ground, so pass a point on the floor.
- The game’s option for items with no timer (
noExpirationTimer) is not available here. - You can’t spawn arbitrary prefabs. There is no
Spawn.Prefab, because the game has no list of names for general prefabs. The game finds item prefabs by item title, and only for items. (item prefabs are loaded by title throughResources(Item.ItemAppearance.GetPrefabByTitle))