Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The question is about how to handle data about entities, but not particularly useful to the entity itself. An example of such data may be the external script that controls the entity's behavior or the path to the animation file — such data is gathered when loading the entity and is no longer useful until you save the game; it certainly doesn't belong in the entity object. Any tips on how to handle metadata?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Put the data in an external file that gets loaded by the object factory. Both XML and JSON are very useful for this purpose:

<entity id="anEntity">
  <script>script.lua</script>
  <image>image.png</image>
</entity>

Then when you save the game just save which entity it was (ie. the ID). When loading the saved game refer back to the entities data file:

<game>
  <entity>anEntity</entity>
  etc.
</game>

ADDITION: Y'know, I think you may be overcomplicating things based on "it certainly doesn't belong in the entity object". If there is some information about the entity that your application needs to continually refer to then stick that information in the entity and provide a getter; if not, then don't.

If you really need/want it, provide one level of indirection; the EntityFactory class has information about what script to use when making intances of Entity. That is how I would design things for the use case I'm guessing you're getting at, since all Entity instances would reference the same script, but really it depends on your exact use case.

share|improve this answer
    
I already have my external data in external files. My question was about how to organize the data inside the application such that classes don't get cluttered with information they don't particularly need. –  Paul Manta Sep 14 '11 at 15:31
    
Yes, I'm the same person. –  Paul Manta Sep 14 '11 at 15:32
    
If they don't need the information, then don't give it to them. I'm not clear on what the problem is here. –  jhocking Sep 14 '11 at 15:32
add comment

such data is gathered when loading the entity and is no longer useful until you save the game; it certainly doesn't belong in the entity object. Any tips on how to handle metadata?

This statement actually gives me pause, and makes me think you may have a very unusual state serialization system. But, that said...

The question is about how to handle data about entities, but not particularly useful to the entity itself. An example of such data may be the external script that controls the entity's behavior or the path to the animation file

If you have a generic key/value dictionary type of storage for the data, you could simply hang a reference to that data off the entity object itself. It's fine that the entity knows that it has metadata, it's generally only problematic if the entity gets to know all about specific kinds of metadata.

(Of course that only matters if you've decided to let the specific metadata live above the level of abstraction of the entity itself, which it sounds like you have.)

Another option is to store the metadata dictionary elsewhere, perhaps in an interface whose job is to recover the metadata for a given entity given its entity ID or whatever.

Neither option is objectively better than the other in general; the latter tends to allow you to restrict what other systems have access to the metadata, which can be nice for enforcing your abstraction boundaries. The former tends to be easier to use. Which will be most ideal will depend more on your specific needs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.