Inventory Systems (part 1 and 2)

Getting MouseOverAny.hitObject and storing a game object as a property as well as debuging*

Mic dies near the end of the first video** (my speakers were off :P)

Converting game object to data(A list of object properties) and then moving the item in the world, or into and back out of a slot*

import bge



def main():


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

    ## Define Sensors ##
    LeftMouse = cont.sensors['MouseLeftClick']
    MOA = cont.sensors['MouseOverAny']
    TimeZero = cont.sensors['TimeZero']
    


    # Mouse is over a valid target and mouse is clicked and Time is zero
    if TimeZero.positive and LeftMouse.positive and MOA.positive:

        # if the data is a string in this case it is empty
        if type(own['Target']) is str:
            

            #Grab item from world and convert to data place in 'Hand'
            if 'Item' in MOA.hitObject:

                props = MOA.hitObject.getPropertyNames()
                StoreList =[]
                for property in props:
                    StoreList.append([property,MOA.hitObject[property]])
                own['Read']=str(StoreList)    
                own['Target']=StoreList
                MOA.hitObject.endObject()
                own['Timer']=30
            
            
            # Grab item from slot and place into 'Hand'
            if 'Slot' in MOA.hitObject:

                if MOA.hitObject['Slot']!="Empty":

                    own['Target']=MOA.hitObject['Data']
                    MOA.hitObject['Slot']="Empty"
                    own['Timer']=30
                    
                    
                    
        else:
            

            # if the property target is a list and your aiming the mouse at a object 
            if type(own['Target']) is list and LeftMouse.positive and MOA.positive:
          
                #Place object in Slot
                if 'Slot' in MOA.hitObject:

                    print('hitSlot')
                    if MOA.hitObject['Slot']=="Empty":

                        for Property in own['Target']:
                            if Property[0] =="Item":
                                
                                MOA.hitObject['Slot']=Property[1]
                                MOA.hitObject['Data']=own['Target']
                                own['Read']="Empty"
                                own['Target']="Empty"
                    
                    
                    
                else:    
                    #Place Object in world
                    End = MOA.hitPosition-own.worldPosition
                    Dist = End.magnitude
                    End = own.worldPosition+(End*(Dist+.5))
                    Ray = own.rayCast(End,own.worldPosition,0,'',0,0,0)
                    bge.render.drawLine(own.worldPosition,End,(1,0,0))
                    if Ray[0]:
                        Scale = 0
                        Item = 0
                        bge.render.drawLine(own.worldPosition,Ray[1],(1,0,1))
                        for property in own['Target']:
                            if property[0]=="Item":
                                Item =property[1]
                            
                            if property[0]=="Scale":
                                Scale = property[1]    
                                
                                
                        if Scale!=0 and Item!=0:    
                            Added = own.scene.addObject(Item,own,0)
                            Added.worldPosition = MOA.hitPosition
                            
                            Added.worldPosition = Ray[1]+(Ray[2]*Scale)
                            own['Target'] = "Empty"
                            own['Timer']=30    
        


main()



MIT 0 licence
it’s nice to let me know you use it though :smiley:

Attachments

ConvertToDataAndPassInAndOutOfSlot.blend (496 KB)