adding attributes

Hi there,
I am designing a chess game, and I am wondering if it is possible to do this…

Lets say there is an attribute called Movement, and inside of the attribute Movement, there are a bunch of more attributes that all point to the main attribute, Movement. For example…

GameLogic.Movement.PieceMoved = ‘PAWN’
GameLogic.Movement.PieceTaken = ‘KNIGHT’

is this possible? I see there is a function called hasAttr, but I could not find a function for addAttr. Is there a such thing?

You would need to use a dictionary for that. Using your example, you would declare it like this:

GameLogic.Movement = {}  # declare the empty dictionary
# add empty entries to the dictionary
GameLogic.Movement['PieceMoved'] = None
GameLogic.Movement['PieceTaken'] = None
# include any other values you need here.

Run a script to set up these values once at the start of the game. You could use a Delay sensor to do this.

In the course of the game, when you need to access or change the values, you would refer to them the same way they were declared:

# an example of changing the values
GameLogic.Movement['PieceMoved'] = "Pawn"
GameLogic.Movement['PieceTaken'] = "Knight"

# an example of testing the value for a condition
if GameLogic.Movement['PieceMoved'] == "Pawn":
     print "Pawn moved."

Thank You so much Blendenzo. One more question though. Where can I find more about these so called empty dictionaries? Would that be under python or would that be under Blender python?

Should read the official python tutorial, very useful and easy to follow :slight_smile:

If you want attribution access, you could also make a class:

class Movement:
    PieceMoved = None
    PieceTaken = None

GameLogic.Movement = Movement()

GameLogic.Movement.PieceMoved = "Knight"