Event queues

The brain thing is really getting out of control.

Yesterday I ran into this problem:

Fatal error: Maximum function nesting level of '100' reached, aborting!

What caused it was my dumbness. Basically, I was firing the Bite neuron which called the Bite method in the snail class. Then I was checking if the object the snail was biting was an item or another snail. If it was another snail, it called BiteSnail() in the snail class. Here is where it got screwy. I wanted to have the bite do the following:

The problem though was that I was creating a new snail brain, calling all the same functions that were already running on the current snail. The input checks for the two snails were overlapping and either I had too many recursive function calls or I had an infinite loop somewhere (infinite snail-biting? A snail ouroboros?) because the normal solution - increasing xdebug.max_nesting_level was not working. I decided to implement a queue system instead of having a snail check all inputs from another snail that’s checking all inputs.

So now I’ve got this possibly even dodgier thing going where I have a queue in the event model. The event, in this case the CheckIdleSnailActions event that checks inputs for the snails and makes them do stuff, finishes running. If a snail bites another snail during this time we add another checkAllInputs action to the event’s queue. When the event does the thing it’s supposed to do the first time around and gets to the end, it loops through its queue and runs everything in there as well.

Here’s what we push to the queue (we use the brain of the snail being bitten, with the extra input already saved to that brain’s tactile sensor)

$toPush = [
            'object' => $object->brain,
            'function' => 'checkAllInputs'
array_push($event->queue, $toPush);


And then, at the end of the event’s runEvent method we do this:

    if ($eventFinished) {
        Log::info ('event finished');
        if (count($this->queue) > 0) {
            foreach ($this->queue as $event) {
                Log::info('about to call ' . $event['function']);
                call_user_func_array(array($event['object'],$event['function']), func_get_args());


One crappy thing (of many) is that currently I don’t have a good place to save the event, so I end up passing it from runEvent to the snail controller that loops through the snails and checks their inputs, to the snail brains that get created. It’s kind of a mess.

I think this weekend I’m going to stop working on new brain stuff and just focus on refactoring all of this.

comments powered by Disqus