+1 vote

I have a scene with grid snapping turned on and configured. I would like to be able to get the grid offset and grid step from a script. Basically, I want the information in this form picture the configure snap form for the scene editor snap settings

Is it possible?

in Engine by (42 points)

2 Answers

0 votes

I have an unrelated code that might help:

# ON MAIN MAP
extends TileMap
tool

func set_cell_size(v2):
    # CONFIGURE SNAP > GRID STEP
    #   SET TO CELL_SIZE: VECTOR 2
    CanvasItemEditor.snap_step = v2

    # SET CELL SIZE
    cell_size = v2
    pass

So on this script, I made it such that when I set the cell size of a TileMap, the snap is set accordingly.

For your case, you can use "CanvasItemEditor.snap_step" to get the step. Not sure about the rest, though; godot docs has not cover the CanvasItemEditor class yet.

by (60 points)

I take back my solution-I thought I got it when I mistook the grid as the snap guide. For now, I'm still looking to get the snap settings by code. I'm currently looking at plugins.

0 votes

It's probably exposed later: https://github.com/godotengine/godot/issues/11180
However, you can hack it by setting it as if a user did it:

Step 1: Find some important nodes: SnapDialog and nodes of SpinBox class inside it.
- To find them, first get get_base_control() on an EditorPlugin class and then get all nodes in it. (I have an extension just to make the EditorPlugin available: when the plugin loads up it just attaches itself to a singleton tool script: _tool.editor_plugin).
- For more help see the attached pic or the link below.

Step 2: Modify the UI settings by script (as if user setting values) and then click the ok.

var snap_grid_boxes := []  # The SpinBox objects inside SnapDialog.
func update_snap_grid_size( new_size : Vector2 ):
    # Set the values to the UI controls.
    snap_grid_boxes[2].set_value(new_size.x)
    snap_grid_boxes[3].set_value(new_size.y)
    # Accept. (No need to popup.)
    _tool.ed_snap_grid_popup.get_ok().emit_signal("pressed")

Helper funcs

Many thanks to https://godotengine.org/qa/18051/tool-script-in-3-0

UPDATE: Oops, the question was about GETTING. Just use get_value()instead.

by (38 points)
edited by

Why does snap_spinbox[2].get_value() and snap_spinbox[3].get_value() both return 0.01 when they, as snap step values, should return an integer like 64? Is it because I called it all the way from the wrong function (_enter_tree()), or am I still missing another step, even after snap_dialog.get_ok().emit_signal("pressed")?

[UPDATE 1] The snap step value will return the intended values after I open the "Configure Snap..." window. Anyone know how to do that via script?

[UPDATE 2] In the end, I stopped getting value from the snapbox. Instead, I created a ConfigFile that stores the snap settings, which would be applied to the snapbox upon _enter_tree().

[UPDATE 3] I've tried virtually opening the snap dialog, both by snap_dialog.popup_centered() and through its parent PopupMenu.

When I tried the former, the value are all defaults in the variable sense, not snap settings sense. This mean all entries are 0s, with few exceptions being 0.01 due to their range settings. After a few more testing, I've learnt that the snap settings only load after clicking Snapping Options (3 vertical dots icon) > "Configure Snap..."

Now, for the latter, I have to find a PopupMenu that has 7 items total, and with the last one with the text of "Configure Snap...". This is the code I used to find it:

for c in children:
    if c is PopupMenu:
        if c.get_item_count() == 7:
            if c.get_item_text(6) == "Configure Snap...":
                c.get_parent().emit_signal("pressed")
                c.emit_signal("index_pressed", 6)
                break

However, calling its parent MenuButton to emit_signal("pressed") does not caused it to drop down the menu. Neither does calling emit_signal("index_pressed", 6) from the PopupMenu causes the SnapDialog to pop up.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.