#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2

    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.
    Code:
    class item:
    	    name=str()
    	    cost=int()
    	    weight=int()
    	    nodes=[]
    	 
    	    def __repr__(self):
    	        return self.name
    	 
    	    def len(self):
    	        return len(self.nodes)
    	 
    	    def pop(n):
    	        return self.nodes.pop(n)
    	 
    	    def append(i):
    	        self.nodes.append(i)
    	 
    	class node_attack:
    	    skname=str()
    	    dmg=int()
    	    acc=float()
    	 
    	class node_container:
    	    ctyp=int()
    	    size=int()
    	 
    	class node_material:
    	    matname=str()
    	    mat=int()
    	    vol=int()
    	 
    	class node_feature:
    	    ftyp=int()
    	    detail=str()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Would a dictionary of items work better?
    Code:
    inventory = dict(torch=item(args),backpack=item(args))


    A list doesn't make much sense to me, although it works too.
    Code:
    class common_item:
    
        def __init__(self,name,cost,weight):
            (self.name,self.cost,self.weight,) = (name,cost,weight,)
    
        def __str__(self):
            return self.name
    
        __repr__ = __str__  # poor choice for repr
    
        def __int__(self):
            return self.weight
    
    class item(common_item):
    
        def __init__(self,name,cost,weight,**kwargs):
            common_item.__init__(self,name,cost,weight)
            self.__dict__.update(kwargs)
    
    inventory = [item('torch',0,3,state='unlit'),
                 item('backpack',0,1,capacity=12)]
    
    print('inventory: %s' % ', '.join(str(item) for item in inventory))
    print('Weight: %d\n' % sum(int(item) for item in inventory))
    Otherwise, I don't comprehend your post well.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo