Jump to content
  • Advertisement
Sign in to follow this  
dzeligman

Unity simple component architecture questions

This topic is 3584 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
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Aryndon
      Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
      We are building in Unity and are currently looking animators and artists.
      What we are looking for
      -Someone who is okay with split revenue/profits when finished
      -Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
      -Someone who wants to work with people that are passionate about this project
      If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
    • By Aggrojag
      Hello!
      I'm working on a game that is a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
      This project is exercise in polish, this means some experimentation will be needed along with some reworking of assets as they're provided.
      This will be a revshare model.
      First, I'm looking for a 2D sprite artist, not pixelated, that can compliment the style of the attached images, and be similar to the temporary character.
      We are looking to bring on a SFX designer at this time. Full list of required SFX will be available upon request, as well as a build with all elements related to sound implemented in some form (many SFXs pulled from the web for now). Will likely require some field recording, and some pretty strange SFX for when things get weird. I imagine a good portion of these will be quite fun to create.
      Lastly, I'm looking for a male voice actor, English should be your primary language. There will be at minimum two characters that will need to be brought to life through vocals. The first voice is similar to Marvin from Hitchhiker's Guide to the Galaxy. A reference for the second voice would be a mix of Ren (Ren & Stimpy), and Android 21 (DragonBallFighterZ). Due to formatting, I'm not including YouTube links in the post, sorry.
       
      WIP Scene with our custom shaders attached (platforms are lazily placed, as this was an asset test):

      A scene with dynamic lighting and temp character:

       
      Unshaded asset:

      If you made it to the bottom, thank you, and I look forward to hearing from you.
    • By SickTwistGames
      Ok, firstly, Hi.
       
      This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.
       
      My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
      So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
      It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.
       
      Thanks
    • By ethancodes
      I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
    • By Pixeye
      I wrote an extension for unity inspector that allows to group/fold variables. 
      Available on github  , cheers!

       
  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!