Python message sensor errors

Hi all

I’ve been making a system that allows me to create combinations of objects from messages sent via a mouseclick. The system works great but generates Clist errors when more than one ‘bundle’ of objects is generated. To iron out some problems I made a .blend that focusses on this.

Here is the code:



import bge, flexicomp_rbc as rbc


#CREATE AND JOIN SHIP PARTS###########################

PpositionName = "positionName"

       
def Create():
    
    cont = bge.logic.getCurrentController()
    scene = bge.logic.getCurrentScene()
    objList = scene.objects
    owner = cont.owner

    sensor0 = cont.sensors["Main"]
    if not sensor0.positive:
        return
    MessageShipNameBody = sensor0.bodies
    MessageShipName = MessageShipNameBody[0]
    positionName = owner.get(PpositionName)
    playerShip = scene.addObject(MessageShipName, positionName)
    playerShip["Cindex"] = owner["cIndex"]
    playerShip["player"] = owner["player"] 
    sensor1 = cont.sensors["Left"]
    if not sensor1.positive:
        return
    MessageLeftBody = sensor1.bodies
    MessageLeft = MessageLeftBody[0]
    Left = [i for i in playerShip.children if "mountPointLeft" in i]
    payloadLeft = scene.addObject(MessageLeft, Left[0])
    const = rbc.SixDoF(playerShip, payloadLeft, (0.0, 0.0, 0.0))
    const.limit()
    const.limitRot()
    payloadLeft["const"] = const
    payloadLeft["Cindex"] = owner["cIndex"]
    payloadLeft["player"] = owner["player"]
    
#PART MANAGEMENT################################################

def End_constraint_custom():
    cont = bge.logic.getCurrentController()
    scene = bge.logic.getCurrentScene()
    owner = cont.owner
    owner["cIndex_value"] = owner.parent["cIndex"]
    MouseL = cont.sensors["Mouse"] 
    MouseM = cont.sensors["Mouse1"]    
    Message4 = cont.actuators["Message4"]    
    if not MouseL.positive:
        return
    if not MouseM.positive:
        return
    Message4.subject = str("end")
    Message4.body = str(owner["cIndex_value"])
    cont.activate(Message4)

#IN UI (SHIP)

def End(cont):
    owner = cont.owner
    sensor = cont.sensors["Message2"]  
    sensorBody = sensor.bodies 
    sensorBodyMessage = sensorBody[0]
    if sensorBodyMessage == str(owner["Cindex"]):
        owner.endObject()

    

#IN UI (PAYLOAD)

def End_constraint(cont):   
    owner = cont.owner
    const = owner.get("const", None)
    sensor = cont.sensors["Message1"]
    sensorBody = sensor.bodies
    sensorBodyMessage = sensorBody[0] 
    if sensorBodyMessage == str(owner["Cindex"]):
        if const and const.valid():
            const.end()
            owner.endObject()


Should I care about this if it works? If not, how do I go about stopping said errors? I am thinking about a try/except routine but I’m a little bit confused as to where the problem comes from.

Thanks for your time

Paul

Attachments

parent_testerB.blend (576 KB)

the error not happen more by adding this line , checking if in the list there some items


def End(cont):
    owner = cont.owner
    sensor = cont.sensors["Message2"]  
    sensorBody = sensor.bodies 
    if len(sensorBody)>0: #<<<< line add
        sensorBodyMessage = sensorBody[0]
        if sensorBodyMessage == str(owner["Cindex"]):
            owner.endObject()

the same can happen in the block below(line 77)

pretty common this error :slight_smile:

First simple change:



def Create(cont):
#remove:     cont = bge.logic.getCurrentController()

:wink:

As MarcoIT already wrote check if messages are present.

You can check if the sensor is positive then there is at least one message


if not sensor.positive:
  return

or check if bodies are present with MarcoIT’s method or with:


if not sensorBodies:
  return

I appreciate you name your variables pretty good. But there is still some room for critics ;):

  • in general the used identifiers (names) should represent the purpose

sensorBody = sensor.bodies

I would expect:


sensorBody = sensor.bodies[0]

I recommend to choose a consistent naming convention for variables, functions, classes. Currently you have a mix of Upper/lower capital words. E.g. MessageShipName and positionName

this is not a big deal but it can confuse the reader when your code gets large.

I hope it helps :slight_smile:

Many thanks to both of you! The solution is pretty simple, I should have seen it.

I kind of chopped this out of a much larger script and mashed bits together- but that cont = bge.blahetc is plain embarrassing:o I should learn to proof read my scripts more;)

Hopefully this system should be robust enough now for my game- I only need to change about 4 variables after duplicating each button (and no direct references to any object, which was a scripting goal of mine).