OO Design, events, etc. - Solving a problem

4 posts

Flag Post

Ok, I’m probably just missing out on some obvious programming method, but I’ve been stuck on this for a bit and I need to ask for some help.

Basically, I’m creating a puzzle game. I have a class that I’ve defined that is the puzzle itself. From my main game, I can call initPuzzle(args) and have a puzzle start up.

My problem is figuring out how to either return success/failure from the puzzle object or in some other way notify the main game that the puzzle has completed.

The initPuzzle function simply sets up the puzzle with appropriate parameters – it is a void function and I don’t think there’s any way to return from it since I have other functions checking the puzzle constantly to see if it has been completed or not.

For this particular issue, I want to be able to call as series of puzzles in a row and time the performance across all of them. This requires my main game to know when the puzzle has finished so we can create a new one.

Does any of this make sense? Am I missing something obvious? Thanks in advance for the help!

 
Flag Post

There’s a few ways to go about it.

When you set up your puzzle, you can pass it a reference to the main game. Then when its finished, it calls some method in main game to let it know the puzzle is finished. Clean and easy.

or, just pass it a reference to a function instead, that it can call when it’s finished. More versatile, but you need to be careful about scope.

or, what I like to do is set up a static reference to the main game, so the puzzle, or anything else, can access methods of the main game at any time. I guess it’s pretty bad style really, but it’s convenient.

 
Flag Post

Hmmm…function references sound good to me. I’m gonna try those – thanks! :-)

 
Flag Post

you should really use events for this…
as2 or 3?