Hello there! Im basically trying to make it so that when any crafting-inventory slotse “ray” is positive it will take the value of ray.hitObjects[‘resource’] and add it to a either another property or use a list. the ray reads items dropped in the crafting window and only checks for the property resource.
scene = bge.logic.getCurrentScene() cont = bge.logic.getCurrentController()
own = cont.owner
craftingSystem = scene.objects['craftingGUI']
if own.sensors['Ray'].status == bge.logic.KX_INPUT_ACTIVE:
own.hitObject['resource'] + craftingSystem['resourceList']
If both of those properties are lists, you can add them, but you’re not currently assigning the result of the addition. You should use += to assign and add, or use the extend method of a list.
if 'resourceList' not in own:
own['resourceList']=[]
other wise your blanking the list you make each frame it looks like
ok so.
def crafting():
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
if 'CurrentResources' not in own:
own['CurrentResources']=[]
#Best to store stuff rather than sorting through all objects all the time
own['craftingSystem']= scene.objects['craftingGUI']
# gather info
if own.sensors['Ray'].status == bge.logic.KX_INPUT_ACTIVE:
own['CurrentResources'].append(own.sensors['Ray'].hitObject['resource'])
print (own['CurrentResources'])
#send out the GUI info
own['craftingSystem']['resourceList'] =str(own['CurrentResources'])
you may want to add a mouse button etc, sometimes a timer is nice to using a sensor to prevent double executions,
this doesnt seem to work for me.
the idea is a 3x3 crafting grid, each little cell has a ray checking for items with ‘resource’ resource is a string in certain items like ‘wood’ ‘stone’ etc, so it should be when the cell is holding an item it adds the value of that items resource to the craftingGUI’s property resourceList
it looks okay in the console but the property doesnt get updated
You’re assigning the string representation of a list before you assign any items to that list. This means it will always be an empty string. Furthermore, you would probably want to retrieve the previous value of the property to update it.
I’d suggest a python tutorial series.
def crafting(cont):
own = cont.owner
scene = own.scene
craftingSystem = scene.objects['craftingGUI']
try:
resourceList = craftingSystem['resourceList']
except KeyError:
craftingSystem['resourceList'] = resourceList = []
ray_sensor = own.sensors['Ray']
if ray_sensor.positive:
hit_object = own.sensors['Ray'].hitObject
if 'resource' in hit_object:
resourceList.append(hit_object['resource'])
print (resourceList)