<aside> 💡
the tricky part is to loop the equipment table incrementally across, adding to equip table those one after another. otherwise multiple equipment changes are skipped.
</aside>
local self = require('openmw.self')
local types = require('openmw.types')
local function changecloth(event)
types.Actor.setEquipment(self,event.equips)
end
return {
eventHandlers = { changecloth = changecloth }
}
clothes to move the long logic into it.local world = require('openmw.world')
local types = require('openmw.types')
local core = require('openmw.core')
local I = require('openmw.interfaces')
local function clothes(actor, type, slot, equ)
local cloth = types.Clothing.records -- the clothing records in the world
local clothing = {}
for slot, item in pairs(cloth) do -- for all clothes
if cloth[slot].type == type then -- if the cloth is shirt
table.insert(clothing, item.id)-- construct the table from bottoms up
end
end
local rand = math.random(#clothing) -- generates the random number from the amount of cloth records
local clothingmodel = types.Clothing.record(clothing[rand]).model -- get the random shirt model
local clothingTemplate = types.Clothing.record(clothing[rand]) -- gets the base template
-- change / add the properties here
local clothingTable = {name = "better cloth" , template = clothingTemplate, model = clothingmodel }
-- object creation shuffle
local recordDraft = types.Clothing.createRecordDraft(clothingTable)
local newRecord = world.createRecord(recordDraft)
local clothrecord = world.createObject(newRecord.id, 1)
-- moves into actors inventory
clothrecord:moveInto(types.Actor.inventory(actor)) -- moves the cloth to inventory
-- sets the item to equipment table
equ[slot] = clothrecord.recordId
return equ -- function result, table
end
local function onActivate(object, actor)
local actives = world.activeActors
for a, _ in pairs(actives) do -- for every nearby actor
if actives[a].type == types.NPC then
-- the chosen equipments shuffle
local y = clothes(actives[a], types.Clothing.TYPE.Shirt, types.Actor.EQUIPMENT_SLOT.Shirt, types.Actor.getEquipment(actives[a]) )
local b = clothes(actives[a], types.Clothing.TYPE.Pants, types.Actor.EQUIPMENT_SLOT.Pants, y)
local c = clothes(actives[a], types.Clothing.TYPE.Shoes, types.Actor.EQUIPMENT_SLOT.Boots, b)
-- send the result to local
actives[a]:sendEvent("changecloth", { equips = c } ) -- setequipment works only on local
end
end
end
return { engineHandlers = { onActivate = onActivate } }