Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by NeverBeenToSchool · Mar 27, 2015 at 05:07 PM · componentdesignarchitecture

Callbacks in component based design

I am trying to use component based design in Unity3d v5. If I have 2 separate C# scripts attached to enemy ships like below:

  1. Script1.shipDead() - Remove game object and show explosion animation

  2. Script2.showPoints() - Destroyed ship shows points ex: +100 (later this will fade away)

How can I perform showPoints() function after shipDead() function has been performed, as a callback?

I can't put both actions in the same function, as some enemies will not have points. Some enemies will leave power ups/ small enemies/ bombs after being destroyed.

And I don't want to duplicated the same code in 4 functions.

Like :

  1. function 1: destroy ship (nothing more)

  2. function 2: destroy ship + show points

  3. function 3: destroy ship + spawn small enemies

  4. function 4: destroy ship + show points + spawn bomb

(As you can see above, there are many different possibilities / combinations I can come up with)

I need to separate functions and isolate them to do individual tasks. As shipDead() will not do anything related to showPoints() or showPowerUp() functions.

My question is how can I perform functions sequentially? One after another, like a callback function?

Like this:

  1. run Script1.DestroyShip() first;

  2. after that run Script2.ShowPoints();

  3. and finally run Script3.SpawnBomb();

Please provide a solution that will not violate the following:

  1. Components needs to be reusable (I can drag and drop the scrip in to other enemies/ships)

  2. Code shouldn't be duplicated

  3. Loosely coupled (Lesser dependancies)

Please help me on this, Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by wcorwin · Mar 27, 2015 at 08:29 PM

You likely want to create a C# event delegate.

Try adding code like this in Script1:

 public event Action OnShipDestroyed = delegate {};

This will let you reference that event from other objects and add callbacks. The Action type is a Delegate which represents a callback with no arguments. If you want to assign callbacks and pass arguments to them, use Action or Action etc. These generics represent the argument types you're sending, so Action is passing an int value, Action is passing an int and a string, etc.

With a reference to your Script1 object, assign a delegate as such

 script1Object.OnShipDestroyed += MyCallback
 
 private void MyCallback()
 {
     //do work
 }
 
 If you were using arguments, the callback signature would differ, e.g. 
 
 private void MyCallback(int myInt, string myStr)
 
 

The += signifies we are adding to the delegate callbacks. To clean up a delegate after the object no longer needs it use the -= operator.

Events are very powerful! Try searching around for more information on events and delegates.

Seeing as you've mentioned some of my favorite coding principles (loose coupling, callbacks, DRY) I feel obligated to link my personal framework. This isn't an official unity product, it's something I work on in my personal time. It's an inversion of control framework built on similar principles. I think you might find it very interesting! StrangeIoC

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Skills system - class design 0 Answers

Confirm Instantiation or Object Confirm it's Instance 2 Answers

Ability System with Modular Targeting: having trouble finding a good way to map between input components and effect components 0 Answers

Game architecture best practices 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges