String to list....

I searched google, but the answers are all non bge python,

I need to convert a string to a list

the string is a stored property

“(Thrust,Thrust2,Eye)”

to a list

(Thrust,Thrust2,Eye)

thanks

The reason is this has nothing to do with the BGE.

A) As your example is already Python syntax (but describes a set) you could use eval(). But this is not save as someone could enter bad Python code.

B) You could use a library that reads JSON. Be aware it expects [ ]rather than ( ) to describe a list (which is similar to Python).

C) You can parse the string by yourself with string operations such as strip(), split(), …

String processing method:


gameObject['my_string'] = '(Thrust,Thrust2,Eye)'


string = gameObject['my_string']

# Get everything between first '(' and last char ')'
stuff = string[1:len(string)-1]

# Split by comma
stringList = stuff.split(',')

# Optional:
# Trim whitespace
stringList = [s.strip() for s in stringList]

A more advanced method using the ast module:


import ast
ast.literal_eval("{'x':1, 'y':2}")
=> {'y': 2, 'x': 1}

Are you sending and receiving messages with logic bricks? it may be easier to just “get” another object and set its property. You can get objects in other scenes too by using the other scene.objects list. When you set a string property using python it retains its original structure which can be a list or a dictionary or even a class or an object or scene.

You should describe the overall problem you’re trying to solve, because these kinds of string conversions are usually unnecessary (avoid the XY problem).

@Monster

Unless objects referenced in that tuple are in scope, eval won’t work.

@Mahalin

The tuple is not composed of literals, so literal_eval wouldn’t work. Your other approach assumes that tuple elements are string literals, which seems unlikely.

Also, be aware that this:

stuff = string[1:len(string)-1]

Can (and should) be written like this:

stuff = string[1:-1]

@Smoking_mirror

It’s just a property, not a string property.

Anyway, you can get access to all active scenes like this:

scenes = {s.name for s in logic.getSceneList()}

And then, if you want to set properties on a specific object, in a specific scene:


overlay = scenes["overlay_scene_name"]
obj = overlay.objects["relevant_object_name"]
obj["prop_name"] = whatever

You are right the syntax with strings would be [“a”,“b”,“c”] which it is not. So option C) fits best to parse the string, I think.

But: What do you mean with “Thrust”. What object do you expect it to be? Du you mean the string “Thrust”. You you finally get [“Thrust”, … ]

Also, careful with stripping spaces after you’ve split the tuple /list. You’ll have empty string elements.

I actually solved my problem with

message------and---------set parent to assembler

list=[]
for object in assembler.children:
list+=[object]

however it would seem that
a string to list converter would be a nice tool to have embedded in bge.logic

Better: my_list.append(object.name)
Don’t use ‘list’ as a variable because this is also a function, f.e. list(object.worldPosition)
Why not use ‘append’?
If you only want to store the name of the object, you shouldn’t store (a reference to) KX_GameObject.

yeah, that is what I ended up doing,

list+=[object]

I should use append for managing lists, however this list only is manipulated one time,

generate list,

->compound assembly of objects,

also, what is the difference between append and list+=[data] ?

The append method adds the provided element to the end of the list.

list+=[data] creates a new list, puts data into it, and then appends elements from the new list to the existing list.

You can also use insert() to put things in the list at a particular point. Useful if you want to put something at the beginning of your list or in the middle somewhere.

Though, a word of warning, this can be an expensive operation as lists grow in length;
https://wiki.python.org/moin/TimeComplexity

Certain structures are optimised for certain tasks, deques being suited to outer insert/removal operations.