[Closed] registerTimeCallback and variable updates
I need to know the speed of a moving object; I did it by editing the text of a label inside a rollout.
To update it in real time, I added a registerTimeCallback at the end of the script.
The problem is that when I change the framerate by a spinner in the same rollout, the formula doesnt use the real distance traveled by the object,continuing with the previous value.
I have just read this part of the help file:
Note that it is the function value that is being registered, not the function name or global variable. This means that redefining the same-named function after registering it does not change the callback. You either need to unregister, re-define the function and then register it again, or make the registered function an intermediary which calls another function,
For Example:
fn time_cb = print currentTime
fn tcb = time_cb()
registerTimeCallback tcb
In this case, the registered callback function, [b]tcb[/b], calls the real call back, [b]time_cb[/b], (by referencing the global variable it is defined in), meaning you can redefine [b]time_cb()[/b] as often as you need and the callback will always invoke the latest definition. This is a useful technique to employ while you are developing and debugging a callback."
But even with this "intermediate calling" step the problem persist:
)
local ctdMaster = getNodeByName "CTDmaster"
global callSpeedRealTimed, speedRealTime
...
function speedRealTime obj = (
if obj != undefined do (
speedM = (distance (at time (currenttime + 0.5f) obj.pos) (at time (currenttime - 0.5f) obj.pos)) * frameRate
ctdSceneConfiguration.speedMtsS.text = speedM as string
...
)
)
function callSpeedRealTimed = speedRealTime ctdMaster
rollout ctdSceneConfiguration "Scene:Configuration" (
label speedMtsS "00.0" width:26 height:16 across:4 align:#left offset:[6,0]
...
spinner animationFrameRate "FPS: " range:[0,1000,frameRate] type:#integer fieldWidth:40 align:#right
)
...
unregisterTimeCallback callSpeedRealTimed
registerTimeCallback callSpeedRealTimed
)
When you change the spinner, the current time does not change. The Time Callback reacts to the scene time changing, so you have to trigger it inside the spinner’s handler, for example by adding the rather hacky ‘sliderTime += 0’ statement which does not change the time, but causes a time update nevertheless.
Sorry, I forgot part of the code:
rollout ctdSceneConfiguration "Scene:Configuration" (
label speedMtsS "00.0" width:26 height:16 across:4 align:#left offset:[6,0]
...
spinner animationFrameRate "FPS: " range:[0,1000,frameRate] type:#integer fieldWidth:40 align:#right
spinner deltaVmtsSeg "" height:16 range:[0, 1e+006, 0.0] across:3 type:#float fieldWidth:32 align:#left --offset:[-4,0]
spinner interval_spn "" width:60 height:16 range:[0.01, 30, 0.5] type:#float fieldWidth:36 align:#right offset:[6,0]
...
spinner speedDir "Orient/Yaxis [degrees] " height:16 range:[-180.0, 180.0, 180.0] align:#right type:#float fieldWidth:36
...
on animationFrameRate entered do (
frameRate = animationFrameRate.value
if ctdMaster != undefined and IsValidObj ctdMaster do animateMasterObj deltaVmtsSeg.value interval_spn.value speedDir.value
)
)
...
unregisterTimeCallback callSpeedRealTimed
registerTimeCallback callSpeedRealTimed
)
Where “animateMasterObj” is the function which calculates the keyframes value and point3 position of ctdmaster in space.
The problem persist even when dragging the time slider.
The framerate inside the function is right, the error is with the distance travel by ctdmaster.
I’ve tried unregistering and re-registering the callback inside the “on animationFrameRate entered do (” to update the ctdmaster variable…
thank you Bobo