Short solutions? No way, man.
#// POST_OFFICE.PY ///////////////
BOXSIZE=32; READSTATE=21; READBIT=(2**READSTATE);
#-----------------------------
class MAILBOI:
def __init__(self, owner):
self.inbox=[]; self.outbox=[]; self.owner=owner;
def send(self, other, subject, body=""):
if len(self.outbox) >= BOXSIZE: self.outbox=self.outbox[1:];
self.outbox.append((subject, body)); other.state|=READBIT;
other['mail'].inbox.append(self.outbox[-1]);
def read(self, n):
rt = self.inbox[-n:]; self.inbox=self.inbox[:-n]
if not len(self.inbox): self.owner.state&=~READBIT;
return rt;
So here’s a class, and what’s the deal with this?
- It sets a single bit of the statemask whenever you’re sent any messages.
- When that state is on, you process your inbox, and the state is deactivated automatically once the inbox is empty.
How do I use this?
#// INIT /////////////////////
from POST_OFFICE import MAILBOI;
from bge.logic import getCurrentController, getCurrentScene;
scene = getCurrentScene();
ob_list = [ob for ob in scene.objects if "mail" in ob];
for ob in ob_list: ob['mail']=MAILBOI(ob);
#// SEND /////////////////////
from bge.logic import getCurrentController, getCurrentScene;
scene = getCurrentScene();
own = getCurrentController().owner;
other = scene.objects["Cube"];
own['mail'].send(other, "Hello", body="");
#// READ /////////////////////
from bge.logic import getCurrentController;
own=getCurrentController().owner; rt=own['mail'].read(1);
for subject, body in rt: print(f"{subject}: {body}");
And here’s what the logic looks like:
The reciever will start at state 0, run state 0 and 21 simultaneously as the messages are being read, then go back to the original state once that is over.
And before Randy even thinks of pointing out I’m using always on pulse: this runs and kills itself.
Now, onto your actual question: how do I make an ifchain out of a list? Aaah, you people always ask the darndest things. Tell you what, here’s an oversimplification:
- Just make both the subject and body of the message strings
- Use them as condition:instruction.
What is the idea here? Like say, we have
subject="own['powerlevel'] >= 9000"
and
body=f"functionName(*{args}, **{kwargs})"
Now you can just go into read all like,
for subject, body in rt:
if eval(subject): eval(body);
But you KNOW this is terrible, right? It is, and it is python after all. But worse than python itself, this won’t exactly get cached into those pesky little bytecode dumps; so basically everytime IS the first time.
Which gets us to the next point in evolution, which would be generating python code from more abstract parameters, writting it to it’s own file and running that so a cache can be generated. It isn’t half as hard as it sounds. But all in due time.
Though if all you wanted is to check for property x and substract y from life then x is a string, that is subject, y is an int, that is body, and so it’s all like, freaking
for subject, body in rt:
if own[subject]: own["life"] -= body;
And I just lost about an hour elaborating a response to an issue that’s likely much more simpler than I’m giving it credit for, but hey. I felt like sharing MAILBOI
with the world.