|
| 1 | +# Create action scripts |
| 2 | + |
| 3 | +!!! Note |
| 4 | + Reading [Action Scripts Guide](action_scripts.md) is recommended before creating actions scripts. |
| 5 | + |
| 6 | +## Regular action scripts |
| 7 | + |
| 8 | +There is some different types of action scripts based on their base classes, `ActionScript` class designed for regular |
| 9 | +action scripts and can handle a lot of tasks. |
| 10 | + |
| 11 | +To add new action script you should make sure there is an item in menus for that action script, you can use `data/main_ui.ini` |
| 12 | +to add new options (or menus), to understand about structure of this file you can see [here](https://text-forge.github.io/docs/data_driven_ui/#menus). |
| 13 | +After select your option create a script in `action_scripts/` directory with same name (but in snake_case), it this case |
| 14 | +we will create **Copy Path** action script that will copy file path of opened file in clipboard, so we will create |
| 15 | +`action_scripts/copy_path.gd` and use this script as start point: |
| 16 | + |
| 17 | +```gdscript |
| 18 | +extends ActionScript |
| 19 | +``` |
| 20 | + |
| 21 | +Just that! Run project and see option is enabled. But there is no action for now, so we have 3 steps to complete our |
| 22 | +action script: |
| 23 | + |
| 24 | +### Add initializing |
| 25 | + |
| 26 | +We needn't any special initializing for this action script, all we need is disabling it when there is no opened file, |
| 27 | +there is ready-made feature for this: |
| 28 | + |
| 29 | +```gdscript |
| 30 | +extends ActionScript |
| 31 | +
|
| 32 | +func _initialize() -> void: |
| 33 | + requires_file = true |
| 34 | + requires_saved_file = true |
| 35 | +``` |
| 36 | + |
| 37 | +With this function `ActionScript` class will disable option when there isn't saved file, it means when you create a new |
| 38 | +file this option will keep disabled until that file saved, because before that there is no file path. Otherwise, (for |
| 39 | +example when user uses Open) option will be enabled. |
| 40 | + |
| 41 | +### Add main action |
| 42 | + |
| 43 | +We have initialized action script, but there is no action, so let's add it. To do this we use `_run_action()` function, |
| 44 | +this function will be called when user clicks in option in menus, presses shortcut (we will add shortcut later) or uses |
| 45 | +command palette. So we can complete our action script with this function: |
| 46 | + |
| 47 | +```gdscript |
| 48 | +extends ActionScript |
| 49 | +
|
| 50 | +func _initialize() -> void: |
| 51 | + requires_file = true |
| 52 | + requires_saved_file = true |
| 53 | +
|
| 54 | +func _run_action() -> void: |
| 55 | + DisplayServer.clipboard_set(Global.get_file_path()) |
| 56 | +``` |
| 57 | + |
| 58 | +Almost done! We have completed action script, and you can try it now in witch way you want. |
| 59 | + |
| 60 | +### Add shortcut |
| 61 | + |
| 62 | +There is an easy way for adding shortcuts to action scripts, so let's see how we can do it. To have shortcut we need a |
| 63 | +`Shortcut` resource stored in `shortcuts/` directory, so go to Godot's FileSystem dock and use RMB on `res://shortcuts/`, |
| 64 | +then click on Create New > Resource... and create a `Shortcut` resource and save it as `copy_path.tres`. Shortcut will |
| 65 | +be opened in Inspector, click on `Events` and add new element, then create a new `InputEventKey` in that element. Use |
| 66 | +`Configure` button and set your shortcut, then run project and try it. You can open command palette (Ctrl+P) and type `copy path` |
| 67 | +to see shortcut. |
0 commit comments