Changing colours, for display purposes only, when editing a text file in Godot's internal editor

Godot Version

v4.6.2.stable.official [71f334935]

Question

Is there a way in Godot, for display purposes only, or maybe with a Tool Script to highlight or change colours of individual words and sections, when editing a text file in Godot’s internal editor?

What do you mean by “change color”? Plain text files don’t store any color information.

Just for display purposes in the editor, I don’t need to save colour information in the file. Sorry I’m aware that text files contain no colour Information, just having this ability to highlight sections when editing config files would be useful.

What’s in your config file? What’s it look like?

Mostly assembly code we run within a native engine plugin we’ve developed. So highlighting of reserved words and labels would be nice. Simple example below.

;Assembly Code Example

_start:
	; Do any initilization stuff here
	out #versionString

	;Simple loop
	lda #10
	stc
loop:
	lda c
	out
	
	lp loop

	brk


;Testing variables
.db versionString, "Game: \"Version 0.06\""

; Define global variables

;Game Play
.dw gameCurrancy, 0
.db quickRevealsTotal, 3
.db haveUsedQuickReveals, 0
.db quickRevealsTime, 3
.db haveUsedHints, 0
.dw tileMoveCount, 0
.db usedEasyMode, 0

;Data base
.db maxPictures, 0
.db maxBackgrounds, 0

Create a custom EditorSyntaxHighlighter, go look up an assembler syntax highlighter and put the two together.

2 Likes

Cheers, thanks for that, I’ll look into it, never needed or created a tool script before, so should be fun to try and figure out..

1 Like

Thanks so much got something working very quickly. Really only going to do comment, labels, and reserved words, but even that will be a big help.

1 Like

Now just need to figure out how to automatically assign it to the ASM extension, I can select it which is easy enough but would be nice to auto assign.

I think not bad for a couple of hours of work as well as my first editor plugin. Now with our native assembly engine it should make things far easier. I may add constant checks and highlighting but for the moment enough.

When our open source page is up and running on our studio’s new website, I’ll add this in as open source, I doubt there are many other programming Godot direct in Assembly, but the script should help others who want to create their own syntax highlighters..

Many, many thanks for the help.

2 Likes

You’re welcome, please come back here and post a thread with the link when you have it up. While I don’t have any interest in using assembler anymore if I can help it, I would love to see the implementation of your EditorSyntaxHighlighter.

2 Likes

I certainly will thanks again.. Been so busy the past few days, getting the new studio setup, all the domain and web hosting configured, much of our site edited and done as well an intro video on YouTube. It’s nice to get back to a little coding.. :slight_smile:

1 Like

Okay all working as well as we need and surprisingly easy to implement.

For our in studio use the following is enough:

  • comments (both full line and inline)
  • constants
  • compiler directives
  • quoted text
  • labels
  • reserved words

We’ll upload it to our website when we get the open source page done. We can’t include our library files as they relate to our in house tech, and won’t be useful to anyone else anyway. All they are really is just a text file with a list of words.

Big lesson to learn when doing this is NOT to strip the edges of the line you’re getting sent from the editor, the colour map you’re generating needs the tabs and other control characters to properly align the colours. The only other issue I found was the _init() function of the class gets called about 5 times, so don’t load your library files there, until I can find out why, I just do a check when first colourising a line and load the libraries if not already loaded.

_init() is is not a very useful function in Godot. Because so many things can’t be initialized until a Node is ready, I’ve gotten in the habit of not using _init()

1 Like

True, I don’t think I’ve ever had a need to use it before. Anyway, just checking for library load the first time is a simple enough work around..

For anyone interested in creating their own Syntax Highlighter I have uploaded our Assembly Language Syntax Highlighter to our website on the Open Source page. I wasn’t able to include our library files because they related directly to our in-house technology, but the plugin should give you a good start on creating your own syntax highlighters. The library files containing the reserved words and constants are really just text files with lists of words. I’ve left our really simple library loading code in the plugin as well.

Hope this is useful to someone.

Droofus Games Studio

1 Like

How do you initialize resource’s internals without implementing _init()?

Resources are different, but TBH I don’t create Resource objects through code very often. I typically create them in the editor and save them. So I let the ResourceLoader handle that.

Grabbed it, thanks. Do you have a public GitHub project?

1 Like

Creating them via editor is precisely the main use case. When you duplicate (or create) a resource in the editor, the only mechanism to run some initialization code at that point is _init(). I could give you a handful of use cases of resources that maintain a complex internal state beyond merely hosting a bunch of exported properties.

1 Like