January 1st, 2013, 01:48 PM
Customizing calls, aliasing, and functions on container object
So, I'm trying to develop an RPG inventory array for asymmetric objects, that is to say, each object will have attributes that are vastly different from other objects in the same inventory.
In an attempt to solve this, I gave my item class only four characteristics, the name, cost, and weight (common to all items) and then a list of "nodes" defining additional object attributes. The thing is, due to the lack of internal order or count on the nodes being relevant (I don't want/need to know the individual node position) I was trying to simplify node modification along these guidelines:
Objects are subject to an upper limit of 1 node_container. I want to modify ctyp and size by using (item).size instead of (item).nodes[x].size, where x is the position of the node.
For attack and materials, I want to modify only the first instance of a node_attack. So, (item).dmg would actually modify (item).nodes[x].dmg, where x is the first instance of an attack node. In theory, it would be great if I could have a way to choose which attack to modify by modifying the call, but that's totally beyond me.
I'm also trying to modify the sort calls to sort the node list according to type, but I don't know how to determine which objects are in the list.
I was also wondering if I could use the set attribute call to modify the attribute in a freshly created node upon detection that the node currently doesn't exist, i.e. setting dmg would automatically add a node_attack to the item's node container. I started figuring out the get attribute calls, but I kept getting all sorts of crazy syntax errors I'd never even heard before.
I've currently tried this several different times, and the documentation on objects provides little to no help without examples as to how to actually implement it. I've been running in circles and Googling the heck out of it to no avail.
January 1st, 2013, 04:23 PM
Would a dictionary of items work better?
inventory = dict(torch=item(args),backpack=item(args))
A list doesn't make much sense to me, although it works too.
Otherwise, I don't comprehend your post well.
(self.name,self.cost,self.weight,) = (name,cost,weight,)
__repr__ = __str__ # poor choice for repr
inventory = [item('torch',0,3,state='unlit'),
print('inventory: %s' % ', '.join(str(item) for item in inventory))
print('Weight: %d\n' % sum(int(item) for item in inventory))
[/code] are essential for python code and Makefiles!