Advertisement

Which does what?

Started by August 04, 2017 07:07 AM
4 comments, last by NDIR 7 years, 3 months ago

Hey all, i have the following -code/system design- question.

Basically i have a tile map where you have a per-tile control. Each tile for example has a texture and some properties. When i click on the tile the tile changes texture and / or properties. And the question is; who handles the changes in the tile?

Do i call:

1. tile->update()

2. tile_map->update(tile)

3 tile_map_manager->update(map, tile)

Is there a common accepted solution to who handles modifications we apply to our entity?

Currently i'm using the 1. option and i was wondering is this is a good approach.

Thanks!

 

It depends on the semantics of the change. Think semantics first, then think about the best code approach to represent those semantics.

If I had a tile map editor where I was changing the tiles to create a map, there would probably be a call like this:


tilemap->ChangeTiletype(x, y, newTileType);

Each Map has a collection of Tiles, each Tile knows which TileType it is, and each TileType contains a reference to the texture or whatever other visual representation it has. I wouldn't expect to have Tiles knowing about textures.

Advertisement

I didn't yet read it, but probably you can find a good answer here -> http://gameprogrammingpatterns.com/ under Update Methods is the first place I would look for.

And if you don't find that, at least you still find something useful to learn I bet :P

14 hours ago, NDIR said:

Each tile for example has a texture and some properties. When i click on the tile the tile changes texture and / or properties. And the question is; who handles the changes in the tile?

1. tile->update()

2. tile_map->update(tile)

3 tile_map_manager->update(map, tile)

That somewhat depends on what system you are calling it from and what you are trying to do.

The first one works if you already happen to know which tile you are updating.  You'll probably want to word it in a command form as a verb, such as tile->ApplyTextures().  Since it is at the tile level, it should be an operation that applies to ALL tiles, not just the ones you care about.

The second one works if you already happen to know which map you are working on. Again, it should be an operation that applies to ALL tile maps, not just the one you happen to be working on.

The third one makes the least sense to me, since both systems should already know.

If you really are trying to make these "update" functions to be called regularly, beware of the cost of this type of operation. Each function call has a small amount of overhead. It isn't bad if you're calling ten or twenty update functions. But if you're calling hundreds or thousands of them every frame then the overhead rapidly accumulates. 

 

I may want to go with another system entirely.  

If only specific tile systems are involved, I may have the systems register a callback to be called with an event.  Any tile or map can register for the events, and they can do whatever they want when the event triggers.

A variation on that is to have a general event system that anybody can listen to. Events are simply numbers, and the callbacks are an array of functions to be called when the event is broadcast. Any system that wants to can add or remove themselves as listeners by being added to the array for the event.  

If processing batches, I'd want an form that handles all the tiles as a batch instead of calls on the individual tiles.

Thanks for the info! I'll stick with my current approach (with some modifications.)

This topic is closed to new replies.

Advertisement