Godot: Is there any way I can invoke a method in my Script from the editor? (for testing purposes) - godot

I come from Unity, and there you can use ContextMenu method attribute. It will add a button in the editor you can click and the method in your script will be invoked.
This is very helpful for testing/debugging purposes. When you are testing a functionality and you want an easy way to trigger it.
Is there something similar in Godot, or any workaround I can use?
(Godot 3.5 here)

There are multiple ways to run code in the editor.
In fact, Godot games and the Godot editor are built on the same core. One way to say is that Godot is build on Godot… But a more accurate way to say it is that your games are Godot without the editor, plus whatever you built on top.
As a consequence, you have a lot of freedom when extending the Godot editor.
For starters you will be making a tool script. To do that you the tool keyword on the top of the script file. This allows the script to run in the editor.
Warning: Remember that in Godot the Game is not running inside the Editor. Anything that your script moves while running in the Editor would be a modification to the project, for good or ill. And it does not come with build-in undo functionality. It is possible to add undo functionality (with the UndoRedo class), but that is also something you would have to program.
By the way, you might want to know if your code is running on the editor or not. For that, you can check Engine.editor_hint which will be true in the editor.
Read more on the article Running code in the editor.
Since the tool script modify the project. What I present below is more often used to setup parts of the scene or to automate parts of the development workflow. Not for testing features. However since the linked documentation about ContextMenu mentions that it is useful for…
automatically setting up Scene data from the script
I believe what present below is not out of place.
With that said, some modifications of the editor are harder than others. I believe you don't really want to go into the trouble of adding a button to the editor (which is perfectly possible) or an option to the menus (which is also possible, but not everywhere, at least not easily). Instead, I'll stay with the easy options for this answer:
You can make an EditorScript. That is a script that extends the EditorScript class and overrides the _run method. For example:
tool
extends EditorScript
func _run():
print("Hello from the Godot Editor!")
To execute it, have it open in the Script Editor, and to the File menu, and select Run. You can also use secondary click on the script on the "Scripts Panel (on the left of the Script Editor) and select Run in the contextual menu.
The drawback is that is script does not work in the game. It is only for the editor.
Although Godot 3.x does not have official (there is a plugin) support for inspector actions (it might land in Godot 4), we can workaround that. What we will do is export a bool property, and handle (with a setter, which we specify with setget) what happens when you set it. Like this:
tool
extends Node # or whatever
export var do_something:bool setget on_do_something
func on_do_something(_mod_value:bool) -> void:
# do whatever you want
pass
The property should show up as a checkbox in the Inspector panel when the node is selected. And clicking the checkbox will trigger the setter method on_do_something… Which will do whatever you want it to do. Notice also that I'm discarding the value that Godot is trying to set to the property (_mod_value) so it will remain false.
This pattern has got popularity among Godot developers.
If you want to add elements to the Godot UI you would have to make an EditorPlugin (see the Editor Plugins section in the documentation).
Alright but, since the tool script could cause modifications to the project, which might be a problem for testing… What do we do for testing?
Well, I will remind you that you can tell Godot to execute specific scenes (it does not have to run the main scene), and that can another way to test your code.
Furthermore, when your game is running you can go to the Scene panel and select the Remote tab to see the Nodes that exist in the game. Which will allow you to select them, which shows their properties in the Inspector, which would be able to modify (having an effect in real time on the executing game).
… And thus something similar to what I described above about using a setter would work. Except it does not need the tool keyword since it won't be running in the editor:
# No tool
extends Node # or whatever
export var do_something:bool setget on_do_something
func on_do_something(_mod_value:bool) -> void:
# do whatever you want
pass
By the way, in the inspector, when the game is executing and you have the relevant Node selected, you will see your property twice. The first one will trigger the setter, while the second one bypasses it. So pay attention which one you are using.
You might also be interested in the "Project Camera Override" feature, which allows you to freely move the game camera from the editor. You access the feature via the top bar in the editor.
You will also find that it is possible to modify Resources in the editor and see the effect in real time while the game is running. And a Script is a Resource… However pause the game from the Debugger panel (or use a breakpoint) and make sure the script you want to modify is not being executed before you modify it.

Related

how to disable tool mode entry box widget?

I'm trying to lock an entry widget in tool mode like this:
tool
extends Node
export(bool) var locked=false
export(String) var entry="" setget set_entry
func set_entry(new_val):
if!(locked):
entry=new_val
In theory, this should prevent any changes made to the entry widget
as It would rapidly change back to the previous value hence giving the illusion of being disabled
but in reality, you can type freely and after you select another node and then reselect the original node then the value returns to what it was before locking
How do I disable it completely? (perhaps using _set()?)
Edit
This is the problem I'm facing
To disable it completely, I think you need an Inspector Plugin (I talked a little about creating one elsewhere). You could make one that handles the property you are interested in and displays its value, but does not allow to edit it, or only allows to edit it conditionally.
However, for a quick and dirty approach you can do is tell the inspector that the properties changed:
func set_entry(new_val):
if!(locked):
entry=new_val
property_list_changed_notify()
This will cause the inspector to re-read the values of the properties. It can be a bit annoying, depending on the case. By the way, it seems to be ignored in sub-resources, which I find frustrating.
By the way, Godot 4 has PROPERTY_USAGE_READ_ONLY.
Addendum: We can be a little more aggressive than property_list_changed_notify, by using EditorInterface.inspect_object (we can get an instance of EditorInterface from an instance of EditorPlugin). Godot would reload the inspector (which also results in losing the keyboard focus).

Switching panels in Adobe After Effects using Extendscript

In After Effects I have written a panel that (among other things) switches between different compositions using Extendscript.
I use the myComp.openInViewer() function to switch between compositions.
A side effect of this function is that the viewer becomes active. This means that you first have to click on my custom panel before the user can click on the next action. This is a bit of a hassle, because the point of my script is to quickly compare compositions and a few extra click defeat the whole purpose.
Is there a way to put the focus back on my panel via a Extendscript command (or something else)?
I use After Effects CC 2014 on a pc. The panel is written in html5 and thus uses a lot of embedded JavaScripts. Through these scripts I call the .jsx-files.
Hope there is a solution.
Thanks,
Geert

PyQt: Simulate real mouse click on "desktop"?

If there's any way to simulate a real mouse click (press + release) at the absolute position of current desktop with PyQt, without other extenal library like PyUserInput?
I search around and just found this and this. But If I don't misunderstand, they seem to send their click event to Qt application it self, instead of the desktop?
Use PyQt's QTest, together with unittest or such. See also for example http://www.voom.net/pyqt-qtest-example.
If this is not for unit testing, look at sendEvent and postEvent (See http://doc.qt.digia.com/qq/qq11-events.html#syntheticevents). There are some limitations to Qt's mechanism for generating "artificial" events but based on what you describe, it is likely to work. If you have tried those and it doesn't work, please post the code you tried.

Alternatives to Struts-Menu for Menus in Struts 2.X Application

Per user feedback, I am opening a new question for this topic.
So I am currently using Struts-Menu to handle my menu needs for my Struts 2 J2EE application. It is not necessarily a package I wish to work with I have found by playing around with it. So what are some alternatives to this package? I immediately flocked to Struts-Menu because I saw a fair amount of web search traffic pointing to it, including those who use Struts2. What I am worried about is difficulty in the future of making it work with other packages, given its 2007 last update and the extra tap dance I had do to make it work with my configuration. It seems too fragile at this point for my taste.
I have several different menus in my app, but the one I am specifically addressing at the present is like this ... The top level menu drops down upon mouse hover over it. The submenus expand horizontally upon mouse hover. Exactly one menu item can be selected as no radio buttons or check boxes are contained in the menu. This particular menu does not require db access to populate its children. It works sort of like Velocity CoolMenus4 from the Struts-Menu demos.
I've never used struts-menu, but it looks like overkill to me.
I would recommend that you locate a menu that you like and then write a tag file to handle outputting it in your view layer. To me, that's a lot easier than using a framework or library just to output a menu. Plus, its specific to the actual menu you want to use. Your tag can handle doing security checks to ensure that the user only sees what they have permission to access, etc.

Embed Qt window into firefox, via plugin, on Linux

So this is a trivial example of what I'm trying to accomplish:
Using QX11EmbedContainer, and QX11EmbedWidget, I can create two separate apps that embed the widget from one into the window of the other (container <- widget). I would like to do the same in Firefox, and embed the widget into a plugin.
So I created a basic plugin that just contains the QX11EmbedContainer (starting from the 'trivial' example of qtbrowserplugin), and just for testing purposes, another generic widget.
The result is that the generic widget shows inside the plugin just fine, but the container widget 'pops' out to a separate window.
This occurs under Firefox, Chrome, and even Konqueror.
So the question is - Is it possible to embed external Qt widgets into a plugin?
The answer is yes, but the Qt code is buggy, which is why I'm not including it- you wouldn't want it anyway. Managed (with the help of Trolltech engineers) to insert a vanilla QTextEdit widget. Upon attempting to enter text, the plugin would crash, often taking Firefox with it. So clearly, embedding Qt objects directly into a plugin is a no-go.
I also tried launching it separately with mozplugger, but that had it's own issues - mainly focus was not tracked correctly, and would often be lost, requiring you to restart the plugin to get it back.

Resources