Accessing items in a list

Is it possible to have a property for an object that is an indexed list, and then access it by changing the index value? For example, say I create a property for a cube, a string property, but I want it to be a list. Let’s call it “fruit” and fruit[0]=“apple”, fruit[1]=“orange”, fruit[2]=“peach” etc. Now I want to access one of these from the list by changing the value in the [] (subscript?) so if I have another property, integer named “index” for example, if I set index=2, then I could get “peach” as the property “fruit” etc. So if “index”=1, then fruit[index] would be set to “orange”. I know you can do lists with python but I was wondering if a simple list can work in the game engine.

Yes. Lists work in the game engine.

OK, but would you happen to know the correct format? I tried adding a list property to an object, but there didn’t seem to be a way to reference it. I thought I might use another property as the index, but I couldn’t see a way to enter the string property as a list, just a single item. I want to do this with logic bricks, not a script.

Logic bricks are very limited, sooner or later you’re going to want to break out python.

My idea is if you set one property to an object then you could use that name as a reference to store items, array or classes, depending on your fancy.

iamthwee, so are you saying I can’t do what I want just using logic bricks? My understanding is that python uses “lists” in the way that other language use arrays, but the net result is pretty much the same. I tried naming a property (using logic bricks) as something like “word[]”, and a second property called “idx”, and then I changed various values of idx and tried to access one ‘word’ from a list using something like “word[idx]”, but it didn’t work.

I think you can use expression controller to do that

Create 2 properties: fruit as string. nr as integer number

Construct this logic
always( pulse on ) ----- expression: nr == 1 ------assign property fruit to “banana”

From the same always sensor do other nextexpressions for any nr value and assign it to the string value with any fruit

“I know you can do lists with python but I was wondering if a simple list can work in the game engine.”

They work in the game engine via python. Use an always sensor noodled to a python controller.

Always (no pulse) ---------python

Insert a script in the python controller that looks something like this:


from bge import logic

cont = logic.getCurrentController()
own = cont.owner

own['fruits'] = ['apple', 'banana', 'pear', 'peach']

The first three lines of code are pretty standard to any BGE python script. Importing the logic from bge, finding our current Controller (the python controller logic brick), and that controller’s owner (whatever object you have this brick assigned to). The last line defines the property of ‘own’, called ‘fruits’. We then define that property as a list of objects, the same way you do with any python list. You don’t need to add a property called ‘fruits’ to your object beforehand; python will add that property if it doesn’t find it.

If you want to access things on the list, you must do so with python also. Let’s say clicking the left mouse button prints the contents of fruits[0]. Add this code under the code above:


def Click(cont):
    own = cont.owner
    print(own['fruits'][0])

Now hook your Mouse sensor (or whatever you’re using) to another python controller, but set this one to Module mode (rather than Script mode). Let’s say you’ve named your script ‘fruit_list.py’. In the python controller, put the following into the field there: fruit_list.Click
This will execute that Click() method above, and pass ‘cont’ (or logic.getCurrentController(), ) as the first argument.

RockyMadio, your way works fine, although I need a lot of logic bricks if the list gets too long. Thanks.
Nines, I understand what you’re saying. Thank you. I’m not quite ready to tackle python just yet, because I have so much more to learn about Blender in general, but I will save your text and try it when I am ready, maybe in a few weeks.

Do you want to enter a collection of values via GUI (e.g. entering “10.0, 3, abc”] or do you want to keep the reference to a list in a property?

Monster, the items in the list would be part of the game when it starts. But I’d want to be able to pick from that list based on a value during the game. So I would setup the list as part of the game, like fruit[n] where fruit[0]=“apple”, fruit[1]=“cherry”, fruit[2]=“orange” etc. Then during the game, the value of n would determine a value to assign to a text object. I don’t know much about python so I was trying to use only the logic bricks.

1 Like