I’m currently trying to properly learn python (one can only be a copy-paste peasant for so long) and I’m trying to assign a property from a list.
I’ve managed to use the choice …thingy… To make lists, and can properly set a variable via python, but for some reason, when I define the list, and use the variable I defined, but it never updates the actual property.
I think it’s just a syntax error somewhere, or my format is wrong, because all the tutorials I’m watching make it so simple.
I’ll include a blend to try and save someone time.
Thanks for your time so far.test script.blend (466.9 KB)
After adding a Text object to the scene, I’ve made the following changes to the script, showing the given list as a string property, retrieving and converting this string back to list and setting the Text object’s text to a random name in that list. I hope it helps (file below).
import bge
from random import choice
# Example namelist below
nameslist = ['name1', 'name2', 'name3', 'name4', 'name5']
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
# Get Text object from the list of objects in the scene
textobj = own.scene.objects["Text"]
# Run when the sensor sends a positive pulse
if sens.positive:
# Convert the list to string, then store it in the prop
own["myName"] = str(nameslist)
# Retrieve the string and convert it back to list
retrievedlist = eval(own["myName"])
# Set text to the a random item on the evaluated list
textobj.text = choice(retrievedlist)
main()
Remember that properties added through the Blender interface can store only integers, floats, booleans and strings, so storing the list only work when converting it to string first. You can also store the list without converting it to string, but you’ll not be able to preview it on the debug properties, and you cannot create it through the Blender interface.
# Store the list as a new property
own["myListProp"] = nameslist
# Get a random name from the previously stored property
textobj.text = choice(own["myListProp"])
import bge
from random import choice
#Example namelist below
namelist = "name1,name2,name3,name4,name5"
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
if sens.positive:
own["myName"] = namelist
else:
own["myName"] = "failed_to_name"
# turn comma seperated stringlist to list
nlist = own["myName"].split(",")
# prints a random word from the list to console.
print(choice(nlist))
main()
here is an example if you want to store more complex data.
import bge
import json
from random import choice
#Example data below
data = {
"names" : ["name1","name2","name3","name4","name5"],
"loot_table" : ["loot1","loot2","loot3"]
}
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
if sens.positive:
own["myName"] = json.dumps(data)
else:
own["myName"] = "failed_to_name"
datafromprop = json.loads(own["myName"])
# prints a random data from the list to console.
print(choice(datafromprop["names"]), choice(datafromprop["loot_table"]))
main()
So, I tried using the first corrected version of the code you provided, (which I am quite grateful for you taking the time to make,) but it applies the entire name-list to the property, instead of a single name from the list.
Also, in what scenario would one want to print a name from a list to the system console? It’s not like you can use that data, right?
To make myself more clear, I’m trying to apply a name, from a list to a property owned by an object, so I can apply that string to a universal text object that appears when you talk to NPCs. This text object must change each time you talk to different NPCs, and the generated names need to be the same each time you talk to NPC-Bob, then go back and talk to the generated NPC again.
I have all of this done; I just can’t figure out how to randomize the NPCs name in a way that I can print it to the Text object the same each time, until said object’s name is re-randomized.
That’s why I was going to try and store it as a variable owned by an object.