Skip to content

Interactive elements ‐ how to use them in script

Gombaris edited this page Nov 6, 2024 · 1 revision

You can find more about interactive elements in Vehicle Interactive elements setup (Vehicle IK) .

Interactive elements can have predefined values, which define their behaviour, so they can be used as button, slider, knob, lever or stick.

You can find these values in Scene editor -> Plugins -> Entity properties -> Bone attributes.

Properties labeled as "action" are the ones, that are important for us (for now), those are:

  • ot_knob_action_name - "action" used in action handler (if there is any)
  • ot_knob_action_velocity - "vel" in action handler "options"
  • ot_knob_action_acceleration - "acc" in action handler "options"
  • ot_knob_action_centering - "center" in action handler "options"
  • ot_knob_action_min_value - "minval" in action handler "options"
  • ot_knob_action_max_value - "maxval" in action handler "options"
  • ot_knob_action_positions - "positions" in action handler "options"
  • ot_knob_action_channel - "channels" in action handler "options"

In script

For using the elements with predefined values in script, you can use following action handlers:

  • register_handler("action", function) - this handler works with predefined parameters.
  • register_axis("action", { }, function) - when this handler has no "options" properties (just empty braces), it works the same as register_handler(), but you can also add new properties, or overwrite predefined properties by defining them here.

Examples:

this.register_handler("vehicle/controls/brake", function(v) { }          // works with predefined values (see image above)
this.register_axis("vehicle/controls/brake", {vel: 10}, function(v) { )  // also works with predefined values, except velocity, which is now set to 10

Note: be careful, when defining positions parameters, as it may significantly change the element functionality. For more info see Control elements .

For "action" is normally used format "cfg_file_name/group_name/action_name ", which works with interactive element only when the action (ot_knob_action_name) is already predefined (e.g. when an element with predefined action "vehicle/controls/brake" is interacted with, action handlers in our examples would be called).

In case, that the interactive element does not have an action, then automatic generated name format "knob_action_[name of bone]" is used.

this.register_handler("knob_action_brake_pedal", function(v) { }

Setting handler/knob value with functions

To set the value of interactive knob or any action handler, you can use following functions:

this.set_action_value(int action, float value, bool hold);
this.set_instant_action_value(int action, float value, bool notify);

set_action_value - used for setting the target value of the action handler and executing it's code
set_instant_action_value - used mainly for setting the target value of the action handler without executing it's code (when notify is false)

Parameters

  • action - action id (returned from functions register_handler, register_axis, etc., when defining them )
  • value - target value, to which the action should go (the value is not set instantly, the value of the action handler changes based on it's own velocity and acceleration, until it reaches the target value )
  • hold - used in set_action_value function, to determine, if the value should be set (if true, centering of the action is disabled)
  • notify - used in set_instant_action_value to determine, if the action should be invoked or not. If true, the code in the action handler is executed (same as using set_action_value), if false, the action handler's value is changed, without executing it's code (useful when working with interactive knobs, because changing the knob's action value means, that the knob (elements like switch, lever, etc.) in the model will move/rotate)

Note: These functions don't work on event handlers (actions defined using register_event and other event handlers).

For more information, see also Action handlers and Vehicle IK: Interactive elements.

Test

Clone this wiki locally