This class holds the persistent data for a dropped item. I put it at the end of the item.py:
class ItemDropped(Persistent):
item = ForeignKey("Item")
zone = ForeignKey("Zone")
position = StringCol()
rotation = StringCol()
def _init(self,*args,**kw):
Persistent._init(self, *args, **kw)
def destroySelf(self):
Persistent.destroySelf(self)
To create a database table for this persistent class, add this line to Genesis.py:
DBDict.createRows('ItemDropped')
and import the ItemDropped by adding the name in the import statement near ItemProto and all the others. ItemDropped has to be added to tables in def CreateTables().
While testing I found that table empty from time to time and then I found WorldUpdate copying some data over from the previous one to a new copy of world.db. So that needed some changes as well.
All the dropped items should appear again when the zone is loaded. First there has to be a definition to reach the table in zone.py Zone:
droppedItems = MultipleJoin('ItemDropped')
This referenced table is handled in ZoneInstance.start():
try:
print "Items in this zone:"
for i in self.zone.droppedItems:
print i
itemInst = ItemInstance(i.item)
itemInst.droppedItem = i
newpos = (i.position + " " + i.rotation).split()
self.respawnItem(i, itemInst, newpos)
except:
print_exc()
The respawnedItem function works similar to the spawnItem() but does not create another ItemDropped:
# callback for spawnItem. item is the ItemInstance, simItem is the SimItem, dItem is the ItemDropped
def respawnedItem(self, simItem, dItem, item):
self.itemLookup[simItem] = item
# spawn an item using ItemDropped
def respawnItem(self, dItem, item, transform):
self.simAvatar.spawnItem(item.name, transform).addCallback(self.respawnedItem, dItem, item)
