Ad

How To Add Slider In Tool Mode?

- 1 answer

I'm trying to add a slider in tool mode like this:

enter image description here

code:

tool
extends Position2D

export(float) var CustomSlider = 0.1 setget changeSlider_x

func changeSlider_x(changed_val):
    CustomSlider=changed_val
Ad

Answer

First of all, I want to point out that this variable:

export(float) var CustomSlider = 0.1

Is a variant. Even thought Godot shows it as a float on the inspector panel.

This is a float and Godot will also show it as float in the inspector panel:

export var CustomSlider := 0.1

Or if you prefer the type to be explicit, you can write it like this:

export var CustomSlider:float = 0.1

What you specify between parenthesis after export is to controls how it will be shown in the inspector panel.

I know the documentation is a little hard to find - because there are other things called "export" - but here it is: GDScript exports

Now, to get a (useful) slider, you have to either:

  • Specify a range of values:

    export(float, 0, 10) var CustomSlider:
    
  • Specify a range of values, with snapping to a step size:

    export(float, 0, 10, 0.1) var CustomSlider:
    

    The snapping will be to a multiple of the step size. That is, the snapping is not offset by the minimum and maximum.

  • Specify an exponential slider, with a maximum greater than zero (the minimum will be zero):

    export(float, EXP, 10) var CustomSlider:
    

    With this the slider is not linear. You will find that at the half point the value is the square root of the maximum, not the half. And a third of the way, you find the cubic root of the maximum, and so on. As a result, number are more tightly packed near the maximum (e.g. for a range from 0 to 100, the half point is 10, so the first half goes from 0 to 10 and the second half goes from 10 to 100).

  • Specify an exponential slider, with a minimum and a maximum:

    export(float, EXP, 10, 100) var CustomSlider:
    

    This is similar to the behavior above, except it is offset so the minimum is not zero, but the one you specified.

  • Specify an exponential slider, with a minimum, a maximum, and snapping to a step size:

    export(float, EXP, 10, 100, 2) var CustomSlider:
    

    This creates an exponential slider similar to the previous case. Except this one will snap the value, similar to the snapping described before.


Aside from using export, there is another way to have something show in the inspector panel: use _get_property_list (requires a tool script), which would give us more control (some options are only available this way). Furthermore, we could call property_list_changed_notify to tell Godot to check again what it should show in the inspector panel, and that would allow you to have it change dynamically.


By the way, you can write your variable name in lower caps and with _, and Godot will capitalize it and replace the _ with spaces when it shows it in the inspector panel.

Ad
source: stackoverflow.com
Ad