Re: Learning about callbaks
Alan Gauld <alan.gauld <at> btinternet.com>
2008-01-01 14:17:17 GMT
"Michael Bernhard Arp Sørensen" <michaelarpsorensen <at> stevnstrup.dk>
> I have a game class and a menu class. When the user chooses
> "quit" in the menu, I want the menu object to call a method that
> executes a quit_program() from the game class.
should do it.
Except in your code bekow you don't define a quit_program method
in the Game class.
> Obviously, menu is an object within the game object.
Not obviously, but it's certainly an option. You could have
kept menu as a separate object if that made more sense
to you. But having a menu contained by Game is also fine.
def __init__(self, queue, game):
self.loop = 1
self.queue = queue
self.game = game
tstr = raw_input("Input string: ")
print "Input: ", tstr
if tstr == "q":
def quitProgram(self, game, quit_callback):
self.loop = 0
game.loop = 0
I'm not sure I really understand what this class is modelling.
What kind of an object is UserInput?
Does it represent a single command or is it representing an
action - getting input from the user? In which case its a
very abstract kind of object.
If this (as I think) is the "menu" that you refer to above
then I'd expect it to be responsible for displaying a menu
and obtaing a selection, it could then dispatch a message
to the associated operatrion (a callback).
However this class has a queue and game parameter that
are assigned to local attribiutes but then never used...
The main() method displays the prompt and then
calls the quit method with no arguments.
The quit method tries to use a call back function but the
call back is never passed to it.
In fact in this case you don't even need a callback
since the UserInput object has an attribute pointing
at the game object so you can call game methods
I'm also not sure what the threading stuff is needed
for either. Try to simplify the example by cutting out
all the redundant stuff and not using callbacks
initially, just call the game methods via the game
Then once it works modify it to use callback style.
Also, in the code below you are using loop as a boolean
so it would be better to assign True/False rather than 1/0
self.loop = 1
self.queue = Queue.Queue()
def startUI(self, tid):
ui = UserInput(self.queue, self)
self.loop = 0
data = self.queue.get(block = False)
g = Game()
> Allthough I might be wrong on that point.
> I'm desperate.
I think you need to strip back and simplify, it looks like
you may have been reading too many different resources
and incorporated some ieas without really undertansding
what they do and why.
Author of the Learn to Program web site
Tutor maillist - Tutor <at> python.org