Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 1st, 2013, 12:48 PM
Mr909 Mr909 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 32 Mr909 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 20 sec
Reputation Power: 1
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()

Reply With Quote
  #2  
Old January 1st, 2013, 03:23 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Customizing calls, aliasing, and functions on container object

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap