Lua API for creating new objects
There are a bunch of considerations to keep in mind:
- In multiplayer, creating an object is fundamentally an asynchronous operation. At minimum, the object's refId should be assigned by the server to ensure uniqueness.
- Objects might have parameters other than record id, such as condition of weapons, or souls in soul gems. There needs to be a way to set those values at the same time as the object is created, to avoid multiplying cross-script delays.
- In Morrowind (and OpenMW), objects can exist either in a cell, or in an inventory (an actor's or a container's). We should structure the API in such a way that the logic of object creation is separate from the place where it gets created, to avoid duplicating functionality.
- Currently there is no final decision on how objects with attached scripts should behave when they move from a cell into an inventory. Identical objects stack, so there are multiple reasonable ways of handling identical objects with some of them having attached scripts.
A vague draft of how an API addressing all of these might look:
local core = require('openmw.core')
local types = require('openmw.types')
-- creates a temporary object, which doesn't exist in the world yet
-- the object won't be synchronized or even assigned a final refId
-- until it is actually added to the game world
local newObject = core.createObject('myWeaponId')
-- make the item broken, still no sync of any kind
types.Weapon.stats.dynamic.condition(newObject).current = 1
-- drop the object in a cell, actually gets synchronized to other scripts at this point
newObject:teleport('Balmora', util.vector3(1, 2, 3))