Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_connection_count function to GraphEdit #99564

Merged

Conversation

JekSun97
Copy link
Contributor

@JekSun97 JekSun97 commented Nov 22, 2024

This is my second attempt at this PR, the first one had problems with Git.

Closes: godotengine/godot-proposals#10942

Adds a method to GraphEdit to get the number of connections to a given node and port get_connection_count(to_node,to_port).
With this we can easily limit the number of connections using the condition:

if get_connection_count(to_node,to_port)<1:
	connect_node(from_node,from_port,to_node,to_port)

Test project:
graphedit_test_func.zip

@JekSun97 JekSun97 requested review from a team as code owners November 22, 2024 23:07
@JekSun97 JekSun97 changed the title Add get_connetion_count function to GraphEdit Add get_connection_count function to GraphEdit Nov 22, 2024
@JekSun97

This comment was marked as outdated.

@KoBeWi
Copy link
Member

KoBeWi commented Nov 23, 2024

You forgot to bind the method.

@KoBeWi KoBeWi added this to the 4.x milestone Nov 23, 2024
@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from ed0ca8c to 8b3d8bc Compare November 23, 2024 01:56
@JekSun97
Copy link
Contributor Author

You forgot to bind the method.

Thanks for the comment, I was a bit hasty, fixed

@JekSun97
Copy link
Contributor Author

I generated documentation with --doctool but the function disappeared from GraphEdit.xml, what could be the reason?

@AThousandShips
Copy link
Member

Did you build the editor before running doctool?

@JekSun97
Copy link
Contributor Author

JekSun97 commented Nov 27, 2024

Did you build the editor before running doctool?

Yes, I compiled godot.windows.editor.x86_64.exe in the bin folder, and ran it as bin\godot.windows.editor.x86_64.exe --doctool

@AThousandShips
Copy link
Member

Then did it vanish or did it get moved to the correct position?

@JekSun97

This comment was marked as resolved.

@AThousandShips
Copy link
Member

That's the version without binding the method so it won't show up, you need to rebuild with it bound

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 8b3d8bc to d21b421 Compare November 27, 2024 11:26
@JekSun97
Copy link
Contributor Author

Thanks @AThousandShips , I think the PR is ready now (If CI doesn't have problems again)

scene/gui/graph_edit.cpp Outdated Show resolved Hide resolved
doc/classes/GraphEdit.xml Outdated Show resolved Hide resolved
@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from d21b421 to 3dfad59 Compare November 27, 2024 11:33
@JekSun97

This comment was marked as resolved.

@AThousandShips
Copy link
Member

You pressed the button to add the changes on GitHub, which creates commits on its own, you will need to squash the changes when you're done

@JekSun97

This comment was marked as resolved.

@AThousandShips
Copy link
Member

You need to pull the changes first, you can just make the changes locally and use git push --force otherwise

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 2b27176 to a809bab Compare November 27, 2024 13:49
@JekSun97
Copy link
Contributor Author

Everything is finally ready! Thanks again for your help

@AThousandShips
Copy link
Member

I'll let the GUI team review this but the code looks good

<param index="0" name="from_node" type="StringName" />
<param index="1" name="from_port" type="int" />
<description>
Returns the number of connections from [param from_node] to [param from_port] port.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns the number of connections from [param from_node] to [param from_port] port.
Returns the number of connections from [param from_port] of [param from_node].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I've had a lot of trouble with commit suggestions in the past, to commit and not create a new commit, should I click Commit suggestion or add suggestion to batch ? 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to batch will just add one commit for all suggestions. You have to manually copy the suggestion if you want to avoid GitHub's commits.

int GraphEdit::get_connection_count(const StringName &p_node, int p_port) {
int count = 0;
for (const Ref<Connection> &conn : connections) {
if ((conn->to_node == p_node || conn->from_node == p_node) && (conn->to_port == p_port || conn->from_port == p_port)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this condition is correct. E.g. if to_node is p_node and from_port is p_port (in another node), it could count a connection to another port.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows us to check the right ports as well as the left ones, without this only the left or right port is counted

Copy link
Contributor Author

@JekSun97 JekSun97 Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test project did not reveal any problems, this allows us, for example, to test one node that is connected to itself, from left to right, which is the correct behavior. Since we should count any connections

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns 2 connections to Port 0 in the middle node.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually happening... I had an idea to create two functions, to check from_node from_port and to_node to_port, but it seems redundant, what else can I come up with to fix this, any tips?

Copy link
Member

@KoBeWi KoBeWi Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just have to rearrange your condition, so it always checks the correct port, i.e. use from_port only if connecting from this node and to_port if connecting to this node.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just have to rearrange your condition, so it always checks the correct port, i.e. use from_port only if connecting from this node and to_port if connecting to this node.

The logic for checking the condition has been changed, now it works as expected

@JekSun97

This comment was marked as resolved.

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch 2 times, most recently from d8e32df to 4ac3cd3 Compare December 2, 2024 11:02
@JekSun97 JekSun97 marked this pull request as draft December 2, 2024 11:06
@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from f6c696b to 7d7ed0f Compare December 2, 2024 11:16
@JekSun97
Copy link
Contributor Author

JekSun97 commented Dec 2, 2024

I broke everything... Is it possible to create a new clean PR?

@AThousandShips
Copy link
Member

I would suggest figuring out how to fix it instead, these kinds of mistakes will happen now and again and it's important to not lose the discussion and feedback in a PR by closing one instead of resolving those issues

In this case you should just revert it by using git reset --hard f6c96b and then force pushing after fixing the issues

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 7d7ed0f to 7b0ceae Compare December 2, 2024 11:31
@JekSun97 JekSun97 marked this pull request as ready for review December 2, 2024 11:33
@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 7b0ceae to 17f0573 Compare December 2, 2024 11:39
int GraphEdit::get_connection_count(const StringName &p_node, int p_port) {
int count = 0;
for (const Ref<Connection> &conn : connections) {
if ((conn->from_node == p_node && conn->from_port == p_node) || (conn->to_node == p_node && conn->to_port == p_port)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((conn->from_node == p_node && conn->from_port == p_node) || (conn->to_node == p_node && conn->to_port == p_port)) {
if ((conn->from_node == p_node && conn->from_port == p_port) || (conn->to_node == p_node && conn->to_port == p_port)) {

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 17f0573 to 8865c0f Compare December 2, 2024 11:44
@AThousandShips AThousandShips changed the title Add get_connection_count function to GraphEdit Add get_connection_count function to GraphEdit Dec 2, 2024
@JekSun97
Copy link
Contributor Author

JekSun97 commented Dec 2, 2024

This PR was like a Git exam for me, but thanks to AThousandShips' tips, I got through it without opening a third PR, thanks!

All recommended edits have been applied, now this PR is DEFINITELY READY 🎉

@AThousandShips
Copy link
Member

Once this has been approved please also remove the (Fixed incorrect condition order..) from the commit message, but that can wait until then

Will approve once someone from the GUI/GraphEdit team gives their approval

@JekSun97
Copy link
Contributor Author

JekSun97 commented Dec 2, 2024

Once this has been approved please also remove the (Fixed incorrect condition order..) from the commit message, but that can wait until then

Will approve once someone from the GUI/GraphEdit team gives their approval

Can I change this now?, before something weird happens to my Git :D

Should I use git commit --amend git push --force ?

@AThousandShips
Copy link
Member

That would be the way yes!

@JekSun97 JekSun97 force-pushed the get_connetion_count_method_graphedit branch from 8865c0f to 95ca0aa Compare December 2, 2024 13:15
@JekSun97
Copy link
Contributor Author

JekSun97 commented Dec 2, 2024

Once this has been approved please also remove the (Fixed incorrect condition order..) from the commit message

Ready

@akien-mga akien-mga modified the milestones: 4.x, 4.4 Dec 2, 2024
@Repiteo Repiteo merged commit ec333a8 into godotengine:master Dec 3, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Dec 3, 2024

Thanks! Congratulations on your first merged contribution! 🎉

@JekSun97 JekSun97 deleted the get_connetion_count_method_graphedit branch December 3, 2024 20:52
@JekSun97

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add function to get number of port connections in GraphNode
5 participants