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
0
Question by Shot511 · May 23, 2014 at 03:09 PM · c#tiledelegateselect

Using C# delegates and events to detect similar tiles.

Hello,

How to use delegates and events to select tiles of the same color? For example, I have a grid similar to this:

Sample Grid

And I would like to know, how to use delegates and events to select the same tiles in one group (the same tiles contact each other - see the red tiles in bottom center of the image).

Could you give me a sample code/pseudocode or idea how to do this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · May 23, 2014 at 03:20 PM

Delegate and event are no algorithm they are tools/features from the language, a delegate won't tell you if you have aligned tiles. You need to define first the algorithm and then you can use a delegate or an event to call that method.

Delegates and events have many tutorials all over the net since they are not Unity related features.

Now for your algorithm, you need a search principle. First you check the first tile and look for all adjacent tiles, if one matches color, you add it to a queue AND a list. Then you start the same process with the queue until the queue is empty making sure you mark the tiles as visited so that you don't process the same ones over and over again.

When the queue is empty, you check the count of the list and if it is greater or equal 3 you have your tiles in the list for candidate of destruction. Repeat that process until you have done the whole grid and the list is empty.

so:

  1. while grid is not entirely done take first and then next

  2. Check adjacents

  3. if same color add to queue and list

  4. while queue is not empty back to 2

  5. When queue is empty check list count

  6. if greater or equal to 3 destroy all content of list

  7. if grid is not entirely done back to 1

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
avatar image
0

Answer by Tarlius · May 23, 2014 at 03:48 PM

Maybe I misunderstood the question... But you could do it with something like this I guess...

 public event System.Action<Block> OnSelected;
 public event System.Action<Block> OnDestroyed;
 
 OnNeighbourDestroyed(Block b) {
     OnSelected -= OnNeighbourSelected;
     OnDestroyed -= OnNeighbourDestroyed;
 }

 void OnNeighbourSelected(Block b) {
     if(this.isSelected) return; // Don't re-trigger own event for recursive pain
     if(b.color = color) OnSelected();
 }

 void Init(Block upB, Block downB, leftB, Block rightB) {
     RegisterBlockEvents(upB);
     RegisterBlockEvents(downB);
     RegisterBlockEvents(leftB);
     RegisterBlockEvents(rightB);
 }
 void RegisterBlockEvents(Block b) {
     OnSelected += OnNeighbourSelected;
     OnDestroyed += OnNeighbourDestroyed;
 }

However, with this kind of approach you will have lots of "fun" making sure all the events point to the right blocks all the time... It would probably be more simple to have a class controlling the board that has access to the colors that can search from a given starting point, for example:

 int[,] blockColors
 List FindSameColor(Vector2 start) {
     points = new List
     color = blockColors[start]
     return Check(color, start, points
 }
 List Check(color, currentPoint, matchedPoints) {
     if(matchedPoints.Contains(currentPoint) return

     matchedPoints.Add(currentPoint);

     if(blockUp.color == color) Check(color, blockUp, matchedPoints)
     if(blockDown.color == color) Check(color, blockUp, matchedPoints)
     if(blockLeft.color == color) Check(color, blockUp, matchedPoints)
     if(blockRight.color == color) Check(color, blockUp, matchedPoints)
 }
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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make different Rule Tiles interact with each other? 0 Answers

C# array equal to another array minus one entry. 1 Answer

How to unsubscribe from InputSystem event properly 0 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