[SOLVED] Understanding Controllers / Should I use Controllers?

If I have something like this:

[java]

class MyControls

{

MyControls(String a, String b, String c) // custom contructor

{ time = a;

speed = b;

id = c;

}

MyControls { //empty } // regular contructor

update(float tpf) { // do something using a, b , c that are passed from Reader }

}

class Reader extends MyControls

{

// bla bla bla where we give values to a, b , and c

spatial.addControl( new MyControls(a,b,c));

}

[/java]

I do not want to create an instance of MyControls everytime I need to add the controls. If I create the instance at the beginning of the Reader class , it will not pass the updated variables (a,b, and c) to MyControls (and will get a Null Pointer or something). I need to pass these variables to MyControls without having to create a new instance of it every time.

In Java, I could simply do MyControls.someFunction( a, b, c) but it’s a bit trickier here in Jme3 with the addControl function.

you have something wrong in your code.

  1. Why reader extends MyControls ? Remove inheritance.
  2. why MyControl has no setTime , setSpped, setId ?
  3. read beginner java tutorials how inheritance is used.

    [java]

    class InheritanceSucks extends MyControls

    {

    public InheritanceSucks(String a, String b, String c)

    { super(a,b,c);

    }

    }

    [/java]

Tralala this is not my actual code, just trying to explain the idea I have. As far as the inheritance , I put it just to show that I can inherit in case it needed to be done.





Wezrule, trying option 2, spatial.addControl(control) is called at the end of class Reader as the values are passed. I don’t really need to use a certain function from the MyControls rather than just trigger it and pass it a certain event. Thats why i was initializing the class and passing the args to its constructor (something that I know is not right).





so plain simple, I am trying to pass the EVENT from Reader to MyControl and don’t need to use a function from MyControls.

Maybe you should show us a more concrete example because this is a little confusing.



If all you want to do is pass an event to an existing control then just grab it and call your own handleEvent() method on it or something. Sorry, that’s the best I can do given the information provided.



Tell us more about what you are actually trying to do instead of overly generalizing.

After just a quick read, you could do:



animator.cube.getControl( SpatialController.class ).callSomeMethodOnYourClass(event , time_read, type, id, speed, x, y, z);

but thats the thing, and I apologize in advance if thats something silly, but what function should I be calling to trigger the controller. All I am trying to do is pass these variables (event, time_read, etc ) to SpatialController and start it once

so I need to be calling it at the beginning of the Reader class and then “somehow” pass those variables without re-instantiating it





and if I instantiate the controller class from the beginning the parameters are still null and do not contain values

And I’m not understanding what the issue is.



Somewhere you early on someSpatial.addControl( new SpaialControl() );



then later when you need to give it new data you call a method of your choosing:

someSpatial.getControl( SpatialControl.class ).yourMethodToGiveItNewData( stuff to give to your control );



I’m just going from your big bold comment in the code… and that’s how you would change the control without reinitializing it. I haven’t looked into what your control is actually doing.



Are you trying to do a sequence of events at certain times or something? I’m not really clear on what SpatialController is trying to do exactly.

for #2 - I really think it’s what i told you before about rot_start and rot_end continuously changing. Even for the moveSpatial() your doing it wrong, but the only reason its working is because your end Vector never changes so it will eventually reach there, while in your rotation function, your end Vector is based on the start vector of the spatial each frame so in effect is never reachable, which is probably why your getting those weird effects. You just need to initialize them once and then interpolate between them

1 Like

I have a file that contains a list of things (trace file) that my Reader class reads it, parses it, and animates it accordingly. Thats why when the Reader class reads a line that says move spatial x y z I call the controller to trigger the moveSpatial function in it and moves that cube spatial to location x y z





from previous examples, I learned that to call the controller I use something like



spatial.addControl( myController); which I did. However the question is that the functions inside myController (or in my ex SpatialController) need args that I need to pass.



makes more sense?

Wezrule, I see what u mean. I fixed it by initializing them in setSpatial (if spatial != null)



However the issue is that now the events are mixed. So while the cube is moving it is doing the rotation (I need them in separate steps) I think thi sis related to my quetions #1



PS: I will go ahead and post ur answers for others on my other post to have it as solved

SpatialController a = (SpatialController) animator.cube.getControl( SpatialController.class );

a.x = blabla;

garnaout said:
I have a file that contains a list of things (trace file) that my Reader class reads it, parses it, and animates it accordingly. Thats why when the Reader class reads a line that says move spatial x y z I call the controller to trigger the moveSpatial function in it and moves that cube spatial to location x y z


from previous examples, I learned that to call the controller I use something like

spatial.addControl( myController); which I did. However the question is that the functions inside myController (or in my ex SpatialController) need args that I need to pass.

makes more sense?


Why do you want to add the control again and again? It's a little like opening a new window every time you want to update a visual. And at any rate, your control will only be able to be associated to one spatial at a time but adding it more than once means it will be called a bunch of times.

If all you want to do is execute a sequence of animaion events then add one control and then add events to it. Keep track of the order of the events and execute them one at a time for however long they need to be run.

pspeed thank you, It seems that I misunderstood how to use controllers in the right way.

Do you have any example I could follow to do what you suggested? I went through the forums and the tutorials on jme and couldn’t get the details needed.

If all you want to do is execute a sequence of animaion events then add one control and then add events to it. Keep track of the order of the events and execute them one at a time for however long they need to be run.


That is exactly what I am trying to do. Should I add the control when the spatial is created? If so, having done so, how can I trigger the controller at every specific event.

Notice how my update() works in controller

Can you give me more details maybe a quick example?


thank you

tralala I am not trying to get the actual variable , I am instead trying to pass (more like set)



so forgetting about all the code I already wrote, and assuming that I have a controller already added to a spatial. When I am reading a certain trace file and reached a line where I have to move that spatial (that is done in class reader, other than the controller class), how would I pass the variable values (event name, type, speed, etc) from reader to the update() in class SpatialController (the actual controller) to do that move event?

we have already answered that question.



update has arguments ( SpatialController this, float tpf ).

You can store the data on SpatialController this by calling its set methods.

SpatialController a = (SpatialController) animator.cube.getControl( SpatialController.class );

a.setX();

when update is called it will have the new data.

but if I use this like you’re saying it will only read the last time for ex 6.55 which is a rotate and skip the previous ones.



I need something like pspeed stated: read line by line, and execute command by command where other commands have to wait for it. With the current design and your solution what happens is that the reader reads the whole file at once and starts from the latest time read which is only the last event

  1. why did you use MyControl class ? for better marketing ? use classic java.
  2. use a static method that does the thing you want. gg now it works.
garnaout said:
pspeed thank you, It seems that I misunderstood how to use controllers in the right way.
Do you have any example I could follow to do what you suggested? I went through the forums and the tutorials on jme and couldn't get the details needed.


That is exactly what I am trying to do. Should I add the control when the spatial is created? If so, having done so, how can I trigger the controller at every specific event.

Notice how my update() works in controller

Can you give me more details maybe a quick example?


thank you


Just wondering... are you trying to animate just one object or many objects? Do you read all of the events ahead of time from a file? Or are you expecting to get the events read as you are supposed to be doing them?

While I've never used it, you may want to take a look at the jme cinematic package. It looks like it does a lot of what you seem to be trying to do and can manage multiple objects at a time, etc.. I don't know if there are any tutorials for using it or not. It combines app states and controls to animate a bunch of objects in various ways. At the very least, it may give you some ideas... but I think it's supposed to do exactly the type of thing you are trying to do.

I am reading a trace file and need to animate it. For now I am trying to do one object, but eventually I need to create “many - talking about hundreds”.

The file is ready and I am just reading it - it is static and will not change (but could be a long trace).



I will look into it thanks.



Any advice on this for the time being?

garnaout said:
2- Also now setSpatial is not accessed during this process (I assume it should only be accessed at the controller's initialization) and I was passing the args for the rotation there, so is there another function other that the setSpatial() and update() I could use to pass the args?


You should never call setSpatial() yourself and only under very odd circumstances should you call update(). These are called by JME to help setup your control.

The basic idea (and what the cinematic package seems to do) would be to have your control keep a list of events with information about how long they should run. In your update() you would keep executing an event until it's time is up and then remove it from the list and start executing the next event. update() is called once per frame.