Topic was automatically imported from the old Question2Answer platform.
Asked By
Cyber-Kun
I’m making a script editor and trying to add syntax highlighting… keyword: “trying”.
I have a “CodeHighlighter” resource and it has a few keywords and color regions set up in the editor. but there are a lot of keywords that I want to have the same color.
at first I thought to add a key in the keyword dictionary in the editor of type array. and this key would have all the words and it’s value would be the color
but this doesn’t work and I’ve tried loads of other things that also don’t work:
under the RefCounted section of the CodeHighlighter resource, I added a script and tried to add keyword arrays as in this video: https://youtu.be/f5RfboOKKgw?t=393
Non of the things shown in this video works! I should have known since godot 4 is completely different. the add_keyword_color function doesn’t work on any of the nodes or syntax highlighter class/objects that i’ve tried to use them in.
it seems if I wan’t to add highlighing I would have to every keyword individually
For a list of keywords, you can iterate over an array like this :
var keywords = ["Hello", "Godot"]
for keyword in keywords:
$CodeEdit.syntax_highlighter.add_keyword_color(keyword, Color.RED)
I tested and it worked fine
Thank you…
when you say $CodeEdit.syntax_highlighter....
will it add the keywords to the syntax highlighter that is attached to the CodeEdit node in the editor?
and also since I am planning to add multiple syntax highlighters
And I saw you could add a script to a syntax_highlighter resource. can I add keywords in the same way from a script attached to a syntax highlighter?
Cyber-Kun | 2023-04-05 11:35
Indeed, $CodeEdit is a CodeEdit node in your scene tree.
Like CodeSyntaxHighlighter is a resource, you can create a new SyntaxHighlighter resource that inherites CodeSyntaxHighlighter.
The approach would be as follows:
Create a script with this code :
class_name MyCodeHighlighter
extends CodeHighlighter
func add_keywords_color(keywords: Array, color: Color):
for keyword in keywords:
add_keyword_color(keyword, color)
You have now a new resource called MyCodeHighlighter which has the method add_keywords_color().