Beauty of Mathematical Curiousity

Once there was a girl who thought math was pretty, and wanted to program her own games. This is her story.

Unfortunatly for her, one of her setbacks was impatience.

In this story, I lay the groundwork for a brighter tomorrow. I have prepared a basic home for my thoughts, and filled it with bright, cheery colors which hopefully don't look too awful. I have neglected spell checking, but everything looks okay. If I've misspelled something please correct me. I don't approve of careless spelling errors any more than lazy capitalization and punctuation.

I've decided that this really bothers me. After all, what if you want to know the first Fibonacci number after 15000? For such a simple program, the user shouldn't have to work so hard to find the answer. Moreover, I have no idea what kind of implications this might have for some beast of a program (such as a gamem). Luckily, being forced away from your project for many hours (by going to work, where you can't actively write code other than on paper) is a great place to figure out what you want to fix, and think about ways to do so. It's the forced patience for my unpatient self. It was thus on paper that I solved this issue.

I'm not convinced that I've found the best solution. Merely have I found a solution. That said, I feel good enough about it over all. Since so few changes (a few lines) are needed, I'm not going to bother posting the full code this time.

Simply put, I want most of my events to sync to the time (to ticks, that is). This is because I crave predictability. I crave full control. This way, I can completely control the speed everything runs at which, while pointless now, will have great use in a game. After all, who wants their graphics and AI to run faster than the fingers of the human at the controls? That makes for a lousy game. I also like keeping the ticks throttled (currently using 30 FPS) to prevent eating the CPU. So, after some reflection, I decided that Quit Events are the only ones (for now anyway) that should be exceptions to the rule.

A Solution

I added a very simple feature to Event. In addition to a name, event stores a "sync" value, which is set to True. I used self.sync, although I'm considering changing and setting it in the class body, rather than init, so that it functions as Event.sync, a static. At first, I assumed I'd want to be able to set any single event to not sync to Tick Events, but now I'm not so sure that will be true as of my next post (where I'll be using subclasses to Event, instead of Event directly).

This attribute of Event will be completely ignored unless someone updates EventManager to deal with it. I suppose, as the ringleader here, it should be me. All we'll change is the post method. Leave the first part of the if clause alone - if the non-sync event is a tick, there is no change in behavior. Perhaps if we wanted to be fancy (and if we could find a reason to bother), a Tick Event that does not sync could be posted to listeners, but not trigger the other stored events to be posted. Since I can't imagine the need for this, I won't do it. Anyway, all I've changed is the else statement. Instead of just adding the event to the list, I do this:

        if event.hasAttribute('tick'):
            ... # no change from before
        else:
            if event.sync:
               self.events.append(event)
            else:
                for listener in self.listeners:
                    listener.notify(event) # post it right away if sync is set to False

This way, we skip adding it to the list and just notify all the listeners. Super! Now run the program. Nothing is different. It will still update the model after the quit event is generated. The reason for this is beyond simple, but it's the sort of simple that's easy to overlook. We've not yet told any events not to sync. In QuitController, just set event.sync = False before posting the event. Try the program now. See? It works. All the problems of the world solved, or at least most of them.

Labels: , , , ,

0 comments.
Leave one.
Comments

Post a Comment