Jump to content
  • Advertisement
Sign in to follow this  
dzeligman

Unity simple component architecture questions

This topic is 3760 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

So I've spent most of today reading up the various theorycrafting on component-driven, data-driven systems and am curious about a simple implementation. I started reading : http://www.gamedev.net/community/forums/topic.asp?topic_id=463508. I also read about the more inheritance driven behavior imp : http://www.gamedev.net/community/forums/topic.asp?topic_id=492938&whichpage=1� There were a lot of ideas thrown around in both threads and its a lot to try to comprehend for someone with little architecture experience. The main issue that I'm really unsure about how to accomplish is bullet number 2 here : 1. Sometimes components in the same entity need to be able to access each other. 2. Sometimes components from different entities need access to each other. (I'll be doing this in C# but that doesnt really matter that much) Say I have an "alien" and a "player", both have either a weapon component or a shooting component such that they have shots fired. The corresponding component would have the list of shots and they're positions would need to communicate against the player's position which in my design I would probably keep at the top level for simplicity sake. So for the actual talking I assume the weapon component or projectile would need to register with a collision component of its parent. The collision component would then also need to know about the position of another object. That part I cant really wrap my head around right now.

Share this post


Link to post
Share on other sites
Advertisement
In my component system, my GameObject have the appropriate GetComponent methods, to get a reference to that component.

So, if my collision component needed to know of the position of another GameObject, I would do this:

GameObject other = GameObject.Find("Same of GO");
Transform otherTransform = other.GetComponent(typeof(Transform)) as Transform;
Vector3 otherPosition = otherTransform.position;

// Register the position with the collision component
Collision myCollision = self.GetComponent(typeof(Collision)) as Collision;
myCollision.DoSomethingWith(otherPosition);

Here's the GetComponent method:
public Component GetComponent(Type type)
{
foreach( Component component in components )
{
if( component.GetType() == type )
{
return component;
}
}

return null;
}


Share this post


Link to post
Share on other sites
Is this what you mean?


//bullet script
if(iHitSomething)
{
objectHit.gotHitBy(me);
}

//alienObject script
gotHitBy(someObject)
{
switch(someObject)
{
case PLAYERBULLET:
//explode
//player.addFrag();
break;
}
}

//playerObject script
gotHitBy(someObject)
{
switch(someObject)
{
case ALIENBULLET:
//explode
//lives-=[1];
break;
case POWERUP:
//make guns cool
break;
}
}


So basically you use one entity to call the method of another entity. Sometimes you need to pass in the calling entity to use in calculations, such as how much damage, or health is left, etc.


//enemy script
player.gotHitBy(me)

//player script
gotHitBy(anEnemy)
{
playerHealth-=anEnemy.getDamage();
}

Share this post


Link to post
Share on other sites
Hmm, on closer inspection of the articles you posted, it looks more like you are talking about an event driven system. I really like this, and they are very powerful indeed.

Rather than try to explain it all in a hurry, I actually wrote two articles about this system for the engine I am using:

(Advanced)Event Driven Programming
This just explains the basic idea.

Chopper
This one actually shows behaviors and such. I made these dwarves look for trees, then chop them down one by one. If they are chopping a tree, it removes it from a master list of trees to search for.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!