Hello there.
I’ve been working on a small mesh replacement system for my game, and i’ve come into the need to detect messages with python. However i’ve been getting some errors with the current system i’m using.
Here’s a small portion of the code i’m using:
from bge import logic
cont = bge.logic.getCurrentController()
Message = cont.sensors['messageDetect']
mouseClick = cont.sensors['mouseClick']
own = cont.owner
objType = own["selectedBuilding"]
placerObject = [obj for obj in bge.logic.getCurrentScene().objects if obj.name == "placer"][0]
#Gets the messages.
for rcv in range(Message.frameMessageCount):
if MessageSubject[rcv] == "createTurret_Gun":
buildingMesh = 1
placeBuilding = True
elif MessageSubject[rcv] == "createTurret_Missile":
buildingMesh = 2
placeBuilding = True
However when i run the script i get the "Name error: name “MessageSubject” is not defined " error.
So my question is, am i even using the right code for message detecting? (Still pretty much a noob on python, and i’m trying to learn by getting into action :P)
You are making a for loop that has rcv from 0 to 1 less than the number of messages received by the sensor presently.
But MessageSubject isn’t anything. That’s why you get an error.
What you could do is iterate through each message received, and do something based on the value of its subject.
for subject in Message.subjects():
if subject == 'createTurret_Gun':
buildingMesh = 1
placeBuilding = True
elif subject == 'createTurret_Missile':
buildingMesh = 2
placeBuilding = True
For example, if this sensor received 3 messages with subjects “hello”, “createTurret_Gun”, “createTurret_Gun” it would set buildingMeh to 1, placeBuilding to True.
But, a problem I can see is if both of the expected messages were sent last tic. That is, if ‘createTurret_Missile’ and ‘createTurret_Gun’ were sent last tic, then the ordering of the messages matters, which you don’t want. One of the 2 objects will be created, but it is semi-random which one gets created.
Also, if for example createTurret_Gun was sent as a message 20 times, it would still only be created once, which is also confusing.
TBH, it doesn’t make sense to me to recreate the message system in Python since the message system is a higher-level abstraction suited for logic bricks. If you’re going to create a message system at the Python level, might as well do a more suitable object-oriented model. You could store a deque as a game property or as an attribute through mutation.
Message passing is also another way of dealing with concurrency, but that’s a whole other thing.
Yeah just found out that haha. As i said, i’m still pretty much a noob on Programming terms, but still, that’s how i started using Blender, just with logic bricks
Either way, thanks for all the help. This will sure as hell come handy in the future