Advertisement
Drakim

Merging Data and System

Feb 25th, 2012
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. I was struck by the fact that I could invoke my Timer class either as a Event or as a Data. Aren't these things supposed to be different?
  2.  
  3. So I did an experiment where instead of having:
  4. TimerData implements ComponentData
  5. TimerSystem implements ComponentSystem
  6.  
  7. I swaped it into:
  8. Timer implements ComponentData, implements ComponentSystem
  9.  
  10. And it works just fine!! You can access the Timer as either a event or as a piece of data. There are still big differences depending on how you access it, making it good to keep both types. But in this case there is no real advantage in keeping it two separate objects. After all, even if an outside component needs to access TimerData, it can still do so. The TimerSystem parts will be invisible, and vice versa in the opposite situation.
  11.  
  12. Let me show you two different ways we could structure the workings of our Timer component. These are the differences I've pinned down in my head.
  13.  
  14. DATA
  15. entity.getData('timer',TimerComponent).useTimerCustomMethod(data);
  16.  
  17. EVENT
  18. entity.dispatchEvent('TimerCustomMethod',data)
  19.  
  20. Despite this doing the same thing, there is a huge difference. First of all is that the top example with DATA is a singleton, It's saved under the hash string 'timer', if we saved another Timer under 'timer' it would overwrite the old one. The second example with EVENT is not a singleton, there could be several Timer classes that detects that 'TimerCustomMethod' event being dispatched.
  21.  
  22. DATA
  23. var x = entity.getData('position',PositionComponent).x;
  24.  
  25. EVENT
  26. <impossible>
  27.  
  28. Events cannot return information! It wouldn't even make sense, if you have several events hooked on, which one should be the one returning data? all of them? Events is always a one way street.
  29.  
  30.  
  31. DATA
  32. entity.getData('healthdata',HealthData).heal(50);
  33.  
  34. EVENT
  35. entity.dispatch('heal',{amount:50});
  36.  
  37.  
  38. The DATA example is strictly typed, while the EVENT example is more dynamic, with a silent failure. If you have a healing spell that can target ANY entity, you WANT it to silently fail if the entity doesn't have a health property. In addition, more than one thing can snap up the EVENT on the way and even modify it. If the entity has an ArmorOfHealing that doubles healing effects, then that would be very hard to implement on the DATA way, we'd have to hardcode a check, while EVENT it would be a breeze.
  39.  
  40.  
  41.  
  42. So, I hope you can see that even though these two things can be treated as one, they still differ so much in functionality that we need both. However, we don't actually need to treat them as two diffrent interfaces anymore, that's why I'm removing DataComponent and SystemComponent and replacing it with just Component. If you give it to an entity as Data, that works, and if you give it to an entity as an Event, that works.
  43.  
  44. Often you DON'T want the data and event to be merged. For example, the PositionData and the MovementSystem are very often related, but we don't actually want them to be one and the same. Because that that would mean that we'd be forcing an object which only has an position and can never move (say a button) to carry the deadweight of the movement code. We should still strive to be modular.
  45.  
  46. It's easy solved by simply making a component called Position and using it only as data, and a component called Movement and using it only as events. Just because we can merge related things doesn't mean we HAVE to do it.
  47.  
  48. It does makes sense in some places though, like the Timer class. TimerData and TimerSystem does not need to be two separate things, there is no situation where you want one but not the other.
  49.  
  50. Btw, I've made a "component" folder now and started filling it, to give me more structure. Things are going along nicely.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement