tags:

views:

76

answers:

2

I am working on a text-based game in Python 3.1 that would use timing as it's major source of game play. In order to do this effectively (rather than check the time every mainloop, my current method, which can be inaccurate, and slow if multiple people are playing the game at once) I was thinking about using the Threading.Timer class. Is it a bad thing to have multiple timers going at the same time? if so, how many timers is recommended?

For example, the user inputs to start the game. every second after the game starts it decides whether or not something happens, so there's a Timer(1) for every user playing at the same time. If something happens, the player has a certain time to react to it, so a timer must be set for that. If the user reacts quickly enough, that timer needs to end and it will set a new timer depending on what's going to happen next, etc

A: 

It should be perfectly safe to have multiple timers going at the same time. Beware that it may not give much of a performance boost, as the CPython interpreter (the standard Python interpreter) uses a GIL (Global Interpreter Lock) which makes threading stuff a bit.... slow.

alpha123
+2  A: 

I think its a bad idea to use Timers in your case.

Using the delayed threads in python will result in more complex code, less accuracy, and quite possible worse performance. Basically, the rule is that if you think you need threads, you don't. Very few programs benefit from the use of threads.

I don't know what you are doing for input. You make reference to multiple players and I'm not sure whether thats on a single keyboard or perhaps networked. Regardless, your current strategy of a main loop may well be the best strategy. Although without seeing how your main loop operates its hard to say for certain.

Winston Ewert
its through a chat network, so the input is based on using commands in said chat eg. !game play. the mainloop is pretty simple, it checks if there are any packets in the socket, and then it checks for any user-added 'onloop' plugins, the plugin here being the timers for the game, I'm just worried about the queue-like way of executing things will slow down gameplay, which I am worried about becuase it is based on reaction time and lag is a major factor
Blazer
The queue like way is the way that pretty much all gui systems work. There is not really faster alternative that you can be using.
Winston Ewert
Lag may well be a problem. However, focusing on the speed of your app is like focusing on the few minutes it takes to leave your house because of the 2 month journey across the ocean. Unless you are doing things very wrong, your app speed is not going to make a noticeable difference.
Winston Ewert
I don't know that there is much you can do about lag here. Thats largely a function of the chat network you are using. The chat network is probably not designed for the kind of application you are imagining. It might work, but if not there is not much you can do about it.
Winston Ewert