Multiple objects using one script.

Okay, so lets say my code looks something like this

import bge

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


def Grab():


    LMouse = own.sensors['LMouse']
    MouseOver = own.sensors['MouseOver']
    ray = own.sensors['rayCast']
    cursor = scene.objects['Cursor']
    setParent = own.actuators['setParent']
    delParent = own.actuators['delParent']
    endObj = own.actuators['endObject']
#grab and move
    if LMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    MouseOver.status == bge.logic.KX_INPUT_ACTIVE:
        cont.activate("setParent")
        own.worldPosition = cursor.worldPosition
#stack        
    if LMouse.status == bge.logic.KX_INPUT_JUST_RELEASED and \
    ray.positive and \
    ray.getHitObject['item'] == own['item']:
        ray.getHitObject['stackSize'] + own['stackSize']
        cont.activate("endObject")
#drop        
    if LMouse.status == bge.logic.KX_INPUT_JUST_RELEASED:
        cont.activate("delParent")

The code works fine when there is only one object attatched to it, but that wont work for me.
Right now im getting the error “cant activate actuators from non activate controller” which is fair, can I tell the script to only work on one object at a time? To put it simply, How can i make sure multiple objects can use the script. or how can i activate only the actuator of the object being grabbed

How can i make sure multiple objects can use the script.

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

put this inside grab()

import bge

def Grab():
    
    cont = bge.logic.getCurrentController()
    own = cont.owner
    scene = bge.logic.getCurrentScene()


    LMouse = own.sensors['LMouse']
    MouseOver = own.sensors['MouseOver']
    ray = own.sensors['rayCast']
    cursor = scene.objects['Cursor']
    setParent = own.actuators['setParent']
    delParent = own.actuators['delParent']
    endObj = own.actuators['endObject']
#grab and move
    if LMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    MouseOver.status == bge.logic.KX_INPUT_ACTIVE:
        cont.activate("setParent")
        own.worldPosition = cursor.worldPosition
#stack        
    if LMouse.status == bge.logic.KX_INPUT_JUST_RELEASED and \
    ray.positive and \
    ray.getHitObject['item'] == own['item']:
        ray.getHitObject['stackSize'] + own['stackSize']
        cont.activate("endObject")
#drop        
    if LMouse.status == bge.logic.KX_INPUT_JUST_RELEASED:
        cont.activate("delParent")

Thank you very much!

I often use loops. It reduces execution count of script. Like so:


def doSomething(cont):
    own = cont.owner # A signle object that you execute script onto.
    scene = own.scene # Scene where the object is.
    if not "_init" in own:
        own["execute_script_list"] = [for o in scene.objects if "doSomething" in o] # All the objects which has "doSomething" property gets attached to this script
        own["_init"] = True
    for ob in own["execute_script_list"]:
        ob.applyMovement([0.0, 0.0, 0.0], True) # Here you can do anything with all those objects, this is just dumb example.

This is effecient and easy to use method. You should also execute it just on a single empty or any else type of object:)