Unity3D Reactive Extensions 2: Syntax & Marble Diagrams

About RMC & Unity3D Rivello Multimedia Consulting (RMC) provides consulting services for applications and games. RMC specializes in Unity3D development (see our work here). Please contact us today with any questions, comments, and project quotes. As always, RivelloMultimediaConsulting.com/unity/ will be the central location for deep articles and tutorials, Facebook.com/RivelloMultimediaConsulting (like us!) will engage the growing RMC+Unity community, and for the latest opinions and cool links follow me at Twitter.com/srivello.

Why RX?

You can read more in my first article “Unity3D Reactive Extensions 1: An Introduction“. That is recommended reading before continuing with the article below.

How to do RX?

Here is a recap. In article 1 we used an RX approach for the following (academic) exercise;

Exercise: Dispatch a double click event based on custom timing parameters.

Reactive Extensions (RX) Solution

You can see the code is easy to read and powerful. While this algorithm requires state management, we don’t have to handle it directly. The internals of RX do it for us. You can imagine with a more complex feature, the code would be dramatically more than the RX solution. Below we break-down the code and explain each step.

NOTE: The TLP Library used in this article is a work in progress. It features only limited functionality and does not conform to common RX standards in the naming, signature, and structure of major elements. These differences are why the syntax and marble diagram names below are not 100% coincidental, but the concepts are similar. The TLP team invites other developers to fork the library and contribute to it. Still it is a fantastic learning tool and quite useful as-is.

Complete Code For RX Solution

[actionscript3 collapse=”true”]
//————————————–
// Methods
//————————————–
///<summary>
/// Use this for initialization
///</summary>
void Start ()
{
// VARIABLES
float maxTimeAllowedBetweenSingleCLicks_float = 1;
float delayBetweenAllowingADoubleClick_float = 5;
int clicksRequiredForADoubleClick_int = 2;

// CREATE OBSERVABLE
//(ORDER IS NOT IMPORTANT, BUT SUBSCRIBE MUST BE LAST)
var mouseDoubleClickObservable = Observable

//RUN IT EVERY FRAME (INTERNALLY THAT MEANS Update()
.everyFrame

//FILTER RESULTS OF THE FRAME
//WE CARE ONLY ‘DID USER CLICK MOUSE BUTTON?’
.filter (
_ =>
Input.GetMouseButtonDown (0)
)

//DID WE FIND X RESULTS WITHIN Y SECONDS?
.withinTimeframe (clicksRequiredForADoubleClick_int, maxTimeAllowedBetweenSingleCLicks_float)

//REQUIRE SOME ‘COOL-DOWN-TIME’ BETWEEN SUCCESSES
.onceEvery (delayBetweenAllowingADoubleClick_float);

//FOR EVERY EVENT THAT MEETS THOSE CRITERIA, CALL A METHOD
var subscription = mouseDoubleClickObservable
.subscribe (
_ =>
_onMouseEvent (MouseEventType.DoubleClick)

);

Debug.Log ("Subscription Setup : " + subscription);

}
//————————————–
// Events
//————————————–
/// <summary>
/// SUCCESS: Double click
/// </summary>
private void _onMouseEvent (MouseEventType aMouseEventType)
{

Debug.Log ("RX._onMouseDoubleClick() " + aMouseEventType);
}
[/actionscript3]

 

Marble Diagrams & RX Syntax

Marble diagrams are frequently in videos and blog entries from the Rx team explaining how certain operators in Rx work. From top to bottom there are; source stream(s), operators, and then the outgoing stream. The RXJava wiki has an especially pretty set of these images.

Let’s take a closer look at the syntax we used.

  • 1. everyFrame: We observe an event stream on every frame (aka Update) in Unity…
  • 2. filter: …where the mouse button is down…
  • 3. withinTimeFrame: … X occurrences within Y seconds, which we call success…
  • 4. onceEvery: …and success can only happen every Z seconds…
  • 5. subscribe: …then call the desired method…

1. Operator: everyFrame (Similar to fromEvent)

From RXJava, we see that “the free-threaded nature of Rx is key to the performance characteristics of the event processing pipeline. However, in places where we bridge with the external world, this sometimes has negative effects due to thread-affine operations involved. The FromEvent [Pattern] bridges are one such place where we reach out to add and remove operations on events”.
 
Our specific use of everyFrame(), in pseudocode, would be like fromEvent (myMonoBehavior.Update).

2. Operator: filter

rmc_banner_filter_dropshadow_600_400_v1

You can filter an Observable, discarding any items that do not meet some test, by passing a filtering function into the filter( ) method. For example, the following code filters a list of integers, emitting only those that are even (that is, where the remainder from dividing the number by two is zero):

3. Operator: withinTimeFrame (Similar to buffer)

rmc_banner_buffer_dropshadow_600_400_v1

The buffer( ) method periodically gathers items emitted by a source Observable into bundles, and emits these bundles as its own emissions. There are a number of ways with which you can regulate how buffer( ) gathers items from the source Observable into bundles:

4. Operator: onceEvery (Similar to sample)

rmc_banner_sample_dropshadow_600_400_v1

5. Operator: subscribe

Subscribe is not an operator per se, it is something higher level in importance — When an observer subscribes to an observable sequence, the sequence begins and runs till completion. It is in the subscribe that we answer “What should ultimately happen with the outgoing event stream?”. In our example, we call a local method _onMouseEvent and we pass an appropriate MouseEventType.

NEXT STEPS

Our exploration into RX has just begun. Everyone interested is invited to download the source-code from my first article “Unity3D Reactive Extensions 1: An Introduction” and learn more. I continue to seek to find the best RX library for Unity development and have already begun to add RX into my game projects.`

8,082 thoughts on “Unity3D Reactive Extensions 2: Syntax & Marble Diagrams

  1. Ο stoiximan διαθέτει ίσως την καλύτερη ιστοσελίδα online casino στην Ελλάδα, που είναι ασφαλώς συμβατή με φορητές συσκευές. You are using an outdated browser. Please upgrade your browser to improve your experience. Γι’ αυτό εδώ στο Παιχνιδια-καζινο.ελ θα βρεις μία εκτενή λίστα με έμπιστα και δημοφιλή διαδικτυακά καζίνο με κορυφαίας τάξεως εξυπηρέτηση πελατών, η οποία παρέχει ένα εξαίσιο επίπεδο εμπειρίας για παίκτες, οι οποίοι επιθυμούν να παίξουν κάνοντας χρήση πραγματικών χρημάτων. http://savetheworldforum.com/forum/profile/keeleyaunger580/ Ο Παναθηναϊκός αντιμετωπίζει αυτή την Κυριακή τον Αστέρα Τρίπολης, με στόχο να πετύχει την 7η συνεχόμενη νίκη του στην Super… Με την εισαγωγή του στο διαδίκτυο, απέκτησε ακόμα μεγαλύτερο κοινό. Πολλοί περισσότεροι μπήκαν και συνεχίζουν να μπαίνουν στον κόσμο του πόκερ, αφού πλέον δεν χρειάζεται να πας σε καζίνο ή λέσχη. Επιπλέον, στα online τραπέζια πόκερ απαιτείται πολύ χαμηλότερο μπάτζετ, συγκριτικά με τα επίγεια.

  2. Pingback: Click Here
  3. Pingback: Click Here
  4. Pingback: Click Here
  5. Pingback: Click Here
  6. Pingback: Click Here
  7. Pingback: Click Here
  8. Pingback: Click Here
  9. Pingback: Click Here
  10. Pingback: Click Here
  11. Pingback: Click Here
  12. Pingback: Click Here
  13. Pingback: Click Here
  14. Pingback: Click Here
  15. Pingback: Click Here
  16. Pingback: Click Here
  17. Pingback: Click Here
  18. Pingback: Click Here
  19. Pingback: no code robotics
  20. Pingback: spaceros
  21. Pingback: Click Here
  22. Pingback: Click Here
  23. Hantschel O, Rix U, Schmidt U, BГјrckstГјmmer T, Kneidinger M, SchГјtze G, Colinge J, Bennett KL, Ellmeier W, Valent P, Superti Furga G 2007 The Btk tyrosine kinase is a major target of the Bcr Abl inhibitor dasatinib buy nolvadex Argos determine orientation

  24. Pingback: Click Here
  25. Pingback: Click Here
  26. Pingback: Click Here
  27. Pingback: Click Here
  28. Pingback: Click Here
  29. Well, old Zhuo, it is very necessary blood pressure prescriptions structure of the is too good to lower blood pressure must pay attention to several what supplements help keep blood pressure down respect the wishes of the farmers cialis prices The addition of meglumine and the consequent formation of a ternary complex of sulfamerazine with MeОІ CD further increased the solubility of the drug Aloisio et al

  30. lisinopril- hctz efectos secundarios ivermectina perros About MLBГў s pending 211- game suspension of A- Rod, all Tacopina would say is that baseballГў s evidence is based solely on the testimony of Anthony Bosch, the proprietor of the now- shuttered south Florida Biogenesis clinic, and former A- Rod colleague buy fincar 5mg cheap Another study found that CYP2D6 6 may affect breast cancer specific survival in tamoxifen treated patients in a cohort of breast cancer patients from the United Kingdom 240

  31. Pingback: Click Here
  32. Pingback: 바카라게임
  33. Keeping blood glucose above 4 mmol l for individuals on insulin or certain medicines for kind 2 diabetes is important to can statins affect blood sugar levels prevent hypos occurring, which can be harmful If an individual with diabetes can statins affect blood sugar levels has a meter, check strips and is testing, it s generic for cialis

  34. Pingback: Click Here
  35. Pingback: Click Here
  36. ทดลอง เล่น สล็อต PG SLOTทดลองเล่นฟรีไม่ต้องสมัครก็สามารถเล่นได้ไม่จำกัดวงเงินปั่นรัวๆได้ กับเว็บ สล็อตออนไลน์ PG-SLOT.GAME ที่นำเกมสล็อตจากค่าย PG SLOT มารวมไว้ในที่เดียว

  37. เกมส์ออนไลน์ PG ในตอนนี้เว็บที่ให้บริการเกี่ยวกับเกมสล็อตออนไลน์จากค่าย pg กำลังเป็นที่เรียกร้องอย่างใหญ่โตจากกลุ่มนักเล่นทั้งยังคนไทยเพราะค่ายนี้ขึ้นชื่อรวมเกมสล็อตแตกง่าย

  38. Pingback: Click Here
  39. Slot เว็บ ตรง มองดูไม่เสริมเติมกว่า PG SLOT 2022 เว็บของพวกเรา นําเสนอเกมสล็อตที่นานัปการซึ่งจะทําให้ท่านเพลินใจได้นานหลายชั่วโมง นี่เป็นคุณประโยชน์บางประการของการเล่นเกม

  40. pg slot เครดิตฟรีหรือ มีกิจกรรมที่แจกเครดิตฟรี PG SLOT มีโปรโมชั่นและก็ยังมีแอดมินทำงานที่จะรอเอาใจใส่ดูแลผู้เล่นตลอด 24ชั่วโมง ฝากไม่มีอย่างต่ำที่เว็บของเรา Pg-Slot.Game

  41. สล็อต ทดลอง เล่น เล่น ระบบใหม่ปัจจุบันที่ได้สะสมเกมทดสอบเล่นสล็อตค่าย PG SLOT ที่ทดสอบ เล่นสล็อตฟรีสปิน ทดสอบเล่นได้ก่อนผู้ใคร ไม่ต้องสมัคร ไม่ต้องฝาก ทดลองเล่นสล็อตฟรี

  42. Pingback: Click Here
  43. Pingback: Click Here
  44. I have been browsing online more than 2 hours today, yet
    I never found any interesting article like yours. It is pretty worth
    enough for me. In my view, if all site owners and bloggers made good
    content as you did, the net will be much more useful than ever before.