adding/removing strings to a list and reading the items in tihe list

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']

the idea is to get an active list of the resources youve put on into the grid, so whatever the list is equal to will show different crafting options.

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.

Sent from my SM-G920F using Tapatalk

Hello there, got some time to chip at this today

def crafting():    
    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    craftingSystem = scene.objects['craftingGUI']
    resourceList = []
    craftingSystem['resourceList'] = str(resourceList)
    
    if own.sensors['Ray'].status == bge.logic.KX_INPUT_ACTIVE:
        resourceList.append(own.sensors['Ray'].hitObject['resource'])
        
    print (resourceList)

No errors, but its not adding the value of resource to the list.

you need


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,

Wait, can a property automatically be used as a list or does it have to be defined somehow? i thought resourceList = [] was creating a list

Yes but if you dont store it, it dies at the end of the frame,

Using a property works, or a entry in a global dictionary

Also, setting it back to [] each frame will happen if you define the property each frame, hence the ‘init’

Thanks for the feedback!

     own['CurrentResources'].append(own.sensors['Ray'].hitObject['resource'])

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 :confused:

Check this out

Attachments

ResourceRay.blend (516 KB)

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)

Cheers! :slight_smile: