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