Event Handling for a game

Pages: 12
As most of the active members might have noticed by now, I'm trying to make a Zelda-ish game. I got to the point where I have a good map format, collision detection and I can walk around in an example map. Now how should I go about handling events? I have absolutely no idea how to do this. Should I make an event class, and let every Tile own an Event object? How do I handle the different parameters for different events?

Cheers,

Xander
"Event" is actually a very broad term. Do you mean an input event? A system message event? A game event (like entering combat)?

Each of these require different methods to handle them. In the case of a system message event this problem becomes platform specific.
If you are referring to the kind of events SFML has (you are using SFML, right?), which ones does a tile need to react to?

Similarly, if you mean "battle" events or something, does the tile really have to respond to that, either?


EDIT: Do you mean SFML events?
Last edited on
I mean a game event. For example, when he walks on a certain tile, it should load a new map.
Or he can no longer move because of a trap. That kind of stuff ;)
As for the trap bit, perhaps tile could be an abstract base class with the following pure virtual function:
virtual void OnPlayerEntered(const Player& player) const = 0;
It's arguments and return type might be slightly different depending on what you had planned, but this kind of thing could work.

Then you could derive a class called, e.g. PoisonTile which set the player's 'poisoned' flag.

Ultimately, it would be more easily extensible to do this with some kind of game script, but for now that's probably overkill.

EDIT: Of course, this approach could also provide a solution to your first question. The OnPlayerEntered function of a a door, for example, could invoke loading routines.
Last edited on
Hmm what's the meaning of virtual?
And my map is an 2d vector of Tiles, I can't use different objects...Or can I?

@Compgeek01: I have no idea what you're trying to say with that. Do you mean that I shouldn't load new maps and instead use one (huge) map?
I may have misunderstood you. Sorry, I do that sometimes.
what's the meaning of virtual

Have you read about inheritance yet? Polymorphism?

Basically a virtual function is one that you can override in a derived class, and then if you call that member function from a pointer/reference to the base class, but which contains a variables of the type of the derived class, then the correct function - of the derived class - will still get called.

I doubt that is a very well written explanation. You could read this site's classes tutorials for more info.
http://www.cplusplus.com/doc/tutorial/classes
http://www.cplusplus.com/doc/tutorial/classes2/

I can't use different objects...Or can I?

If you change it to a vector of pointers to tiles, then yes, it can (has) contains can contain instances of classes derived from tiles.

Do you mean that I shouldn't load new maps and instead use one (huge) map?

Computergeek can confirm if that's what he meant, but I'll just step in and say that (as we may have discussed on another thread), that may well be the easiest way to handle things as long as your map isn't too large.
Last edited on
LOL, I tried to use a view and everything is totally messed up right now...
It shows the wrong part of the map, randomly draws trees...damn.
And it was a clear explanation, actually!
I'll try to make it work. Without using views XD
Make sure the view is looking at the right place, but the errant trees, etc. are unlikely to be the view's fault, as it just positions the camera over a specific part of your map. Perhaps the tiles are uninitialized?

And it was a clear explanation, actually!

Thanks, but you should still probably look elsewhere for details. I doubt I can sufficiently explain in a few lines ones of the great tenets of object oriented programming ;) (Well technically I was explaining an implementation thereof...)

I'll try to make it work. Without using views

I know I previously half recommended against views, but on second thought they are probably good to use if possible. Otherwise you will have to offset all the tiles' positions by the camera position yourself.

Even if you do use them, however, you may still need to load a new map from time to time (i.e. not have it all in memory at once!).
Last edited on
Finally I got it working--WITH views! :D
I'll implement the new tile system tomorrow. Also getting time to make a map maker since just writing numbers to a .txt can get confusing at times :P
When it comes to map making, if you want a GUI, then note that you can integrate an SFML drawing area into a Qt/wxWidgets/WinAPI control.
I'll look into that integration with Qt!
Laurent has done a tutorial ;)
I'm having a problem when trying to switch over to a vector of pointers. I have a class "TileSet" which reads all possible tiles from a .txt file in a vector. Then when I build my map with regular objects (no pointers) I would use those to construct the necessary tiles. Thing is, it now shows almost everything black (because all of the same type point to the same piece in memory (int the TileSet class)). How do I prevent this?
Doing it with pointers, it shouldn't be too different - you just need to dynamically allocate memory for the tiles, and free the memory when you're done. If you don't want every tile pointer of a single type pointing to the same tile in memory, just allocate a different instance of the tile in each case.

I'd have to see your code to better understand your specific problem.
@x333: You could use references, so you don't have to worry about memory, then just have a managerial class to take care of resources. (that's what I'm doing, it's working great) then the ambiguity of pointers get's a little less confusing =]
you don't have to worry about memory

I did consider suggesting references, but you still have to allocate the memory etc, it just saves you a few deference operators, right? ;)
Ok this is the deal:

I have a class TileSet which stores all the tiles (which are read in from a file) in a map tiles<int, Tile*>. My class Map has a vector of class Layer. Layer reads in the map from a text file, for example:
1
2
3
1 1 1 1 0 0 2
1 0 0 0 0 0 2
1 0 0 0 0 0 3

Would make a map.

Every layer has a vector<vector<Tile> > For each number it reads, it retrieves the value of TileSet::tiles[number]. When I use pointers to Tile however, every tile shows black, except for every different tile last read in. so those would show:
1
2
3
1 1 1 1 0 0 2
1 0 0 0 0 0 2
1 0 0 0 0 0 3


How do I get around this?
Pages: 12