Children added to Tree Node within a WIP small plugin tool not showing up

Godot Version

4.2

Question

So I am trying to build a small plugin that will go through all my files within the project and find comments containing TODO. The only issue is, I can’t debug it, and I’m no good with using the cmd line for the only workaround I can find. (It was here I thought about print commands, but those aren’t working either, so guessing Plugins can’t print?)

The code below is it in full, and the scene consists of a VBoxContainer as the root, then a Button Node and a Tree Node; everything is connected properly (I think.)

It was thrown together out of two or three tutorials and ChatGPT4 suggestions, but most of all of that was in Godot 3’s GDscript [clear() instead of tree.clear()] I have used 4.2 Docs to update and clean errors and warnings, and technically runs with no errors… outside of it not doing what I would like, which if I remember from my early school days, is probably a Me error.

I currently have no clue if the directory lookup looks inside folders, but I’m not concerned with that functionality until I get it working. There is a .gd file with a “# TODO” line as a direct child of ‘res://’, so theoretically, it should be successfully finding something.

I’m assuming somewhere in my translating GDScript 3 into 4, I’ve messed up and the code doesn’t even run the meat of the method calls. But I’m at a point where I can’t find it and nothing new is jumping out at me. If someone has advice on what I’m missing, it would be appreciated.

This is mostly a personal learning project that I thought would be quick and simple tool that I could use in my bigger learning project, and it has successfully gotten me to ask a question to the forum community because my searching couldn’t find a solution; which is a part of the learning too I suppose. As such, my styling is a hodge-podge with no set rules right now, I’m working on adapting to proper GD styling as I am enjoying the IDE and language as I learn it more. (And if there is an etiquette thing I’m missing, let me know.)

EDIT:

Code below is different than the conversations below. It is the code from the zip file updated below. I have removed all plugin-functionality and just made it a simple app that attempts to search a single file. The file parsing works fine for the limited search that it is… and the child branches are being created as seen in the debugging process. But they still won’t become visible.
(Old script is still in the .zip file for review, but it was riddled with runtime errors I think.)

extends Node

var searchButton : Button
var resultsTree : Tree
var root : TreeItem
var falseRoot : TreeItem
var fileList : Dictionary

func _ready() -> void: # Fully Working
	searchButton = self.get_node("RunBtn")
	resultsTree = self.get_node("ResultsTree")
	searchButton.connect("pressed", _on_searchButton_pressed)
	update_tree()

func _on_searchButton_pressed():
	resultsTree.clear()

	_parse_file("res://test_file.gd", "test_file.gd") # Works, successfully parses file
	update_tree()                           # and adds Ln# and LnText to the
											# fileList dictionary
func _parse_file(path, fileName):
	var file = FileAccess.open(path, FileAccess.READ)
	if file:
		var line = file.get_line()
		var line_number = 1
		while not file.eof_reached():
			if line.find("TODO") != -1:
				if not fileList.has(fileName):
					fileList[fileName] = {}
				fileList[fileName][line_number] = line
			line = file.get_line()
			line_number += 1
		file.close()
	print(file)

func update_tree(): # Still doesn't show anything, even on initial load.
	resultsTree.clear()

	# Create the root
	root = resultsTree.create_item()
	root.set_text(0, "root")

	# Create a subheading
	var file_branch = resultsTree.create_item(root)
	file_branch.set_text(0, "Files")
	file_branch.set_selectable(0, false)

	# Creates a sorted array of file names, so the tree
	# will have them added in alpha order.
	var file_array = []
	for file in fileList.keys():
		file_array.append(file)
		file_array.sort()

	for file in file_array:
		create_tree_item(fileList[file], file_branch, file)

func create_tree_item(lines_from_file_dict, file_branch, fileName):
	var parent = resultsTree.create_item(file_branch)
	parent.set_text(0, fileName)

	for line in lines_from_file_dict:
		var child = resultsTree.create_item(parent)
		child.set_text(0, "Ln: lineKey : lineValue" ) # TODO... pull text from line Dictionary

Can you share your project? (Zip file)
Then maybe I could try to debug it

EDIT: VERY updated code. Code is now set as a scene extending just from Node just trying to build the trees out of one file. Code currently works except for tree showing up. Does it need to be in a special container not in a VBoxContainer? Idk

image

TODO_Tracker.zip via GDrive

Searching lines of a script

This will happen if you have a blank line? Is there not an end of file check that would work better?

your search is only on the root folder, if your script isn’t in the root folder (I e. In a child folder) it won’t be found.

1 Like

True, that would become a problem that I didn’t think of, it should still catch my first line comment in the script I’m testing with… but it would stop after that one. So, the problem still persists, but this is a catch that I would have struggled on for a while after I get the first one to correctly make a child on the tree.

Edit: the get_line() function… …the docs don’t say what happens if it reaches the end of file… would it return null?

Found the below at

while not file.eof_reached():

And as for the root folder searching only; I was wondering if it was or not, but I have a fix for that in mind when I get to it.

Much thanks.

I’m not sure I understand how you can’t debug, the editor has a fine debugging with break points. If you need to run the script you can right click the script in the script editor and run it manually. Otherwise print statements is another option. Or run it as a scene by itself in the editor.

Oh, I love the normal debugger. But from what I’m finding out, specifically tools or scripts that extend EditorPlugin can’t be run with it. From other posts I’ve found since they run with the editor itself, utilizing the debugger would cause the entire engine to stop/crash.

It specifically won’t let me run it manually with the extension of EditorPlugin. I tried removing that, but one of the main parts of it specifically needs to extend that class to use it’s features. I think it was the DirAccess.open()

… I’m about to take the Tree code only into a new scene and test that on a single file rather than run through the directory… I thought of that a bit after I posted the .zip file and just diving back in now. But if that does work, then the issue is regarding something I’m doing with the class extension. (again, I think)

@pennyloafers Actually, right now it extends plugin… and again several parts need that…
But I’m guessing it may be possible to extend EditorScript so I have access to run it separately, IF I just create an instance of EditorPlugin and run all the needed commands from it? … I’m going to see if that’s possible real quick.

EDIT: Yea… no.
So, I got it working as it’s own scene just fine and easy, instanced the EditorPlugin so I could search through the directory, and no errors or warnings. Ran it with some debug breaks… and none of them triggered. _ready() _enter_tree() or in the button press. :person_shrugging:

EDIT 2: No… maybe?
The answer is still no, but I hadn’t attached the script… but after doing so the EditorPlugin can’t be as it’s an abstract. I’m going to rework the scene to just look in one file, that’s doable without the plugin, and if I get that working will retest in the plugin script.

If it still doesn’t work after I get the tree working, then I’m not doing something right with the plugin script, so will have it narrowed down more. But first have to finish an essay on Horace.

1 Like

After much experimenting, I can say it’s fixed.

Evidently when in a BoxContainer, FlowContainer, or GridContainer… creating children of the Tree doesn’t properly … or ever… show up. And if I tried without one of these, I wasn’t able to make it look clean because control parents running their children’s lives down to the pixel.

But putting them in a SplitContainer (and a panel, but not sure if that is required) allowed the items to properly showup.

image

I’m going to close this, clean up the code and get it working how I want properly and then try to port it over to a plugin after that. Thanks for the help!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.