• Advertisement
Sign in to follow this  

Unity a problem about the design of components-based game object system

This topic is 2922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I searched some articles about components-based game object system and there have been some topics about it. I have also just read Jason's book, of which some chapters are talking about game objects. I was going to take a try to implement game system in this way but I met a small problem, hoping someone here could figure me out. My question is this. I name my game object a pure game object as what jason said in the book, which is only a handle/id that every component it has points to. So the component composes a single object id, which indicates whom it belongs to, and other component data. Many components managers maintain their own type of components. In a data-driven system, a type of game object could be written as a list of components. Problem arises here. Should I constrain that each game object can hold only one instance for every type of component? For example, can a game object have manys aniamtions? The obvious answer is yes. But it seems a little out of road of my initial design. In my first print, I think each components manager will have only one copy instance of a component for a single game object, meaning only the type matters. this way components manager only needs to index the game object handle, because there is no duplicate. But here, I need to remember the name of this type. This way, components lookup becomes not as simple as what I expected. I took a look at Unity3D tools. It allows each object only contain one instance of each component. But if I say, a soldier has 10 animations, it is not wrong. I stuck with this trade-off. I also think it might be a game object definition question. I am willing give the script ability to define a new type of game object. So, I am confused about name, type and value situations. Should I only specify the type of components, or along with a corresponding name to it? How to assign the default value for a component? or should I? Things are like you are defining a c++ class and you need to take a thoughtful consideration of how to organize your symbol table, which mades me mad... So, could anyone help me out? Thanks in advance hao

Share this post


Link to post
Share on other sites
Advertisement
hao,

Welcome to the world of compromises, trade offs and design iteration.

Don't think of an animation as a component. Instead, think of an AnimationCollection as a component that contains one or more animations.

Don't confuse the resource data with the component. Just because you have a Model component does not mean you have a Texture component for each texture that model uses.

On the flip side, don't paint yourself into the corner of having only one component of each type allowed. There will be cases when you need this. You can't know now what they are, but you will.

In our case it had to do with how prop states were created, or particles. There are always work-arounds, but if you design support from the start it should work.

In our engine you still find components by type, but we also support finding a range of types with an iterator so you can say GetComponents<Particle>( entityID ) and it will return an Iterator<Particle> which can can call ++ and IsDone() on.

best of luck with your design

S.

Share this post


Link to post
Share on other sites
S,

Your reply does really enlighten me.

I realize it is a tough question to line up between metaphors. Is a texture a component or just a resource aggregated into models or materials or such. What I feel a good way to answer is assume myself as a user of my code. Which way is the much more human-readable and understandable I feel like, I will then choose to.

I think AnimationCollection as a component is a good choice. First time I saw the name, Animation State Machine, I was really excited. To make the engine code easy to use and fast to produce, many facilities will be great helpful. I mean ASM could be done in many ways or even there is not one. It all depends on how you want your user use your code.

I know program to interface is a best practice, but when I do a real coding, I always lost myself into deeper details, which somehow lead me to somewhere I totally lose control...

The way of components lookup you mentioned looks tricky and decent. Yes you are right, don't assume anything when you design, just think of how to make it as easy to use as possible. Through the whole night thinking, I finally decided to allow multiple instances of single components, although coming from different ways of thoughts:P

Anyways, thank you again. This really helps me out of mad.

p.s. since resource data mentioned here, I am a little confused about between objects' serialization and resources' loading. I think the difference between is that objects could be copyable but resource not. In other words. objects could be modified and then deserialized, but resources are only data that are loaded from disk or somewhere and persistent all the time and also resources ane shared data over the game life. does this make any sense at all?

hao

Share this post


Link to post
Share on other sites
Quote:
Original post by wuh84

p.s. since resource data mentioned here, I am a little confused about between objects' serialization and resources' loading. I think the difference between is that objects could be copyable but resource not. In other words. objects could be modified and then deserialized, but resources are only data that are loaded from disk or somewhere and persistent all the time and also resources ane shared data over the game life. does this make any sense at all?

hao


In our engine we consider resources like any other constant value - immutable and unchanging. Our resources (models, textures, sounds, animations ) are shared among many instances of the same components in a read only fashion. Our rational was that they are authored in distinct tool sets and come to the game-entities as largely opaque collections of data. The designers and programmers rarely worry what is inside them, focusing exclusively on the game entities.

Share this post


Link to post
Share on other sites
Quote:
Original post by wuh84

I took a look at Unity3D tools. It allows each object only contain one instance of each component. But if I say, a soldier has 10 animations, it is not wrong. I stuck with this trade-off.


A soldier has animation component - which means it can be animated. Whether it is a single animation or multiple - it is still animation component.

Animation component has single method:
animate();

There is behavior component, which is a state machine, which has single method:
void setBehavior(string behavior);


When animation runs, it asks behavior component. If entity has no behavior, or currently selected behavior doesn't have an animation, or that animation cannot be applied to current model - then default animation is played. Otherwise, specific animation is played.


Everything besides interface is a matter of implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
There is behavior component, which is a state machine, which has single method:
void setBehavior(string behavior);


When animation runs, it asks behavior component. If entity has no behavior, or currently selected behavior doesn't have an animation, or that animation cannot be applied to current model - then default animation is played. Otherwise, specific animation is played.


Hello Autheus,

Please forgive my ignorance. I not quite understand the behavior component you mentioned.

What is the animation supposed to ask behavior for? which animation should be run? which property will be animated? Did you mean that animation component would be a collection of animations and how to apply the animated value into porperties of game objects is decided by behavior component, in which update should be overidden. Is that a case?

hao

Share this post


Link to post
Share on other sites
Quote:
Original post by Sphet
In our engine we consider resources like any other constant value - immutable and unchanging. Our resources (models, textures, sounds, animations ) are shared among many instances of the same components in a read only fashion. Our rational was that they are authored in distinct tool sets and come to the game-entities as largely opaque collections of data. The designers and programmers rarely worry what is inside them, focusing exclusively on the game entities.


Yeah! This is it!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
    • By Vu Chi Thien
      Hi fellow game devs,
      First, I would like to apologize for the wall of text.
      As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha). 
      Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
      - The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
      - When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
      - There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
      speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
      Also I have a few questions:
      - In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
      - Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
      - Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
      Please help me out with this. I haven't found any topic about this yet on gamedev, and this is my final piece of the puzzle. Thank you guys very very much.
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

  • Advertisement