Questions about Dictionaries

I was just wondering how to use dictionaries in python for weapons, and there stats. I have no idea how to use them but I believe they will be able to help me along my way. :slight_smile:

Dictionaries certainly are useful, especially for inventories. Classes may also be of some interest for you, though they’re more complex data structure.s.

This may help (although some of the syntax may be of the 2.49 era:)

Classes are as well an interest to me, If someone doesn’t mind giving me a few pointers or posting a link to something that is helpful, like @Rubbernuke has, this was very helpful Thank you.

A list is a way of storing information in an ordered way.A dictionary does not have order, but rather you use keywords to find things.
Let’s say I want a shopping list, I can make one like:

shoppingList = {"Lollies":3.2, "Chocolate":2.5, "Bread":3.5}

(you can tell I’m hungry…)
And then to get them you use the keyword:

price = shoppinglist["Lollies"]

Will return 3.2

In the game engine these have a lot of use, they can be used for inventories (the object : the number of that object you have), or some other uses:
They can be used to link keys and actions. If you’re using the method you can link events like “WKEY” with a function “moveForwards”
They can be used for keymappers (“forwards”:“WKEY”)

You can have lists as part of the dictionary too:

listDict = {"shoppingList": ["Lollies", "Chocolate", "Bread"], "todoList":["wake up", "eat", "brush teeth"]}

then you can get the list with

todoList = listDict["todoList"]

I hope that helped

You should watch my video tutorials.

Relevant links can be found in my signature.

Thanks Everybody This is very helpful:)

Classes are a bit more structured than dictionaries. Like dictionaries and their values, they can have any data type as a property, but they also are accessed differently. In addition, each class-based object is unique, as opposed to a dictionary. There’s a quick write up on my site here. It doesn’t cover everything, but it covers some things.

Thanks SolarLune That gives me a pretty good idea on how to use classes:) I think I will be good to go for a while now;)

Yes, thanks for that too, SolarLune- definitely food for thought.

Difference between dictionary and classes:

  • dictionaries are container for data (as lists, tuples, sets etc)
  • classes are container for data and code

Btw: dictionaries are class instances

That’s true, but dictionaries aren’t unique objects, right? I mean, if you create two dictionaries with the same contents, they will equate, despite that they are two separate objects, right?

Oh yes, each dictionary is an on instance (object).

The difference between a class and an instance is:

  • a class is a construction plan
  • the instance is the object build from this construction plan

The difference between a type and a class:

  • a type describes data
  • a class describes data and code

That means a variable (with a type) can’t process itself as it is pure data. The processing must be performed at other places. The code is separated from the data. The code has no relationship to the data. The data must be explicit provided to the code (usually as parameter of a function)

An instance (of a class) can process it’s own data and explicit provided data. It is not even necessary that an instance provides access to all it’s data (data encapsulation).

For example:
A dictionary can manage any kind of object.
The objects stored in the dictionary do not need to know the details of the dictionary. They do not even need to know it exist. This removes any dependency from the stored object to the dictionary.

Solarlune, you’re correct. I beleive this behaviour is a default override or of the like. Monster is referring to the class an instance is constructed from.
Dicts, however, must compare items i guess.

How would I change one of the dicts item values? :slight_smile: I’ve looked in the api but I doesn’t seem to say :frowning:

you can simply equalise them:

dict_ = {“lollies”: 5.0, “bread”:2.0, “meat”:1.0}

now set he value:

dict_[“lollies”] = 10.0

for example…

As sevi writes simply set another value with the same key.

You might have notified that accessing properties is a dictionary access as well (since 2.49).
See the Python docs for details on dictionaries

should you be able to use these dictionaries in in another scene or do you have to save them and then import them into the script that is activated in another scene via logic.loadGlobalDict() :slight_smile:

you need to storeit eitherin the globalDictionary, or store it in a file file and then import it.
It is also possible to create a python file, write your dictionary and import it.
This is very useful, while you dont have to write any classes to import.

Dictionaries and all other objects reside in memory. As long as there is at least one reference to them Python will keep them alive.

What you need is a reference. You can place references in Modules, game objects, properties even in other dicts (containers) etc. .

myDict = {} # creates a dict; keeps the reference at the module

def useDict(cont):
   myDict["myKey"] = 100  # use the dict

def readDict(cont):
  value = myDict["myKey"]

def createReferences(cont):
  obj = cont.owner
  obj["myDict"] = myDict # stores a reference to the dict in a property
  bge.logic.globalDict["myDict"] = myDict # stores another reference to the dict in another dictionary (of another module)
  bge.logic.myDict = myDict # stores another reference to the dict in a dictionary of another module

def useReferences(cont):
  value =  myDict["myKey"]

  obj = cont.owner
  value = obj["myDict]["myKey"] # if this game object called createReferences() before

  value = bge.logic.globalDict["myDict"]
  value = bge.logic.myDict["myKey"]

  #these are always true after calling createReferences() 
  myDict is obj["myDict]
  myDict is bge.logic.globalDict["myDict"]
  myDict is bge.logic.myDict

I hope it is not to confusing.