missmatch between Events and Update()

Hey,

i have two classes, the first one updates “properties” and sends an Event with the new Data. The second class reacts on the event and does something with the “properties”-Values.

the first class looks like this:

 for(int i = 0; i < availableGestures.Count; i++) {
            gesture = availableGestures[i];

            properties.title = gesture.title

                try {
                    newPropertiesAvailable(properties);
                } catch {
                    //Debug.LogWarning("No listener attached to the PropertiesHandler!");
                }
            }

Here is the second class:

private String title;

 public void Enter() {
            Generator.newPropertiesAvailable += new Generator.PropertiesHandler(updateProperties);
        
        }

 public void Update() {

     if (title.Equals("Step")) {  //This is never true and i dont know why :(
         Debug.Log("AAAA");         
     }
            
 }

 private void updateProperties(GestureProperties properties) {
            
        title = properties.title;

        if (title.Equals("Step")) {  //This is true
              Debug.Log("BBBB");         
          }
  
    }

i dont understand why in updateProperties(…) the title is “Step” and in Update() the title isnt “Step” :frowning:
in Update() the title has always the value of my last value in the first clas in the for loop.

in my first clas e.g. the for loop contains [0] = “step” and [1] = “bla”. then in the second class in Update() my title is always “bla” and in the updateProperties(…) methode it is first “step” and then “bla”.

Thanks for your help!

I believe you have a Race Condition.

Your first class generates events for ALL of the elements of availableGestures (ALL OF THEM). They are all handled immediately by your second class in the callback/event handler. The LAST ITEM PROCESSED is “bla”.

When the Update loop gets called the last item is still “bla” so the Debug.Log statement isn’t executed.

To confirm this…make “steps” the last element of your avilable Gestures

yes u are right.
when i make “steps” the last element of my available gestures my Debug.Log shows me “Steps”. But the i cant get the gesture “bla”. But i need both Gestures.

it looks like:
frame begins
for loop element [0] = step
for loop element [1] = bla
frame ends
show properties

I think what you want to do is Queue up a list of guestures to process in the Update() function. IF that is the case then use a Queue or list of some kind of collection to store them for processing in the Update() function.

But what I don’t know is: Why don’t you just do whatever you want to do in the Event handler instead of handling it in the Update() function.

If you MUST handle all Guestures that occured in Update() (as opposed to in the handler) then:

  1. Make the ‘title’ variable a List or Queue of some type → " titleQueue;"
  2. In the event handler do something like–> “titleQueue.add(properties.title)”
  3. In your Update() function something like–> “foreach (title in titleQueue) { dostuff }”

But again…if you understand what I was saying about a race condition…then you have enough knowledge to make it behave the way you want.

thats how i do it right now. But i dont like this solution because i have to loop every frame through an array. And i dont know the size of the array. It could be 2 or it could be 20. I tought there is maybe a better/faster solution.

i will go a little bit more into detail, so you can understand why i get stuck here.

The Background: I am using a Kinect to record different gestures. then i use these gestures to interact with gameobjects, e.g. “wave” changes a color of an object, “step” moves an object …
i am also using playmaker, because i have designer here who dont know how to use C#/JavaScript.

I have done a Class Generator.cs which checks every frame what Gesture was detected. When a gesture was detected i save several infos like position, title, etc. Because it is possible that i detect more then one gesture in one frame i have a List with gestures. In my Generator.cs i have to loop through all Gestures and save the information of a Gesture in GestureProperties.cs. Everytime i saved the properties of one gesture (during a loop) i fire an event.

In Playmaker i have a class Controller.cs which get the properties from the Event. This class takes the information from the recived properties and saves them in attributes. All the other Classes in Playmaker e.g. Move, Rotate use these attributes.

But because in Generator.cs an Event can be fired more then one time during a frame the Controller.cs override the attributes so at the end of a frame i see only the information of the last fired event.

Here is a picture, hope that helps to understand where my problem is :slight_smile:
http://www.luna-arts.de/others/misc/cs.png

im not really sure if there is another solution… there must be something like " send event, wait until the information was used, send next event". However the solution with the list you mentioned works, but i hoped there is a better solution.

PS: I like your idea to do everything in the Event handler. Maybe i can delete the Controller.cs and use instead direct the Event from Generator.cs in my Move, Rotate, etc Classes. I will try that! Thanks alot !