• Godot Help
  • Stripping BBCode from a RichTextLabel in C#

Hey there all! Unity dev learning the ropes of Godot and I'm trying to make a typing game (Mostly following the tutorial here but substituting out GDscript for the language I'm more familiar with. So far, so good but I've come across an unusual problem when it comes to the Text property of the RichTextLabel. I've been lead to believe that this field is devoid of any BBCode but when I print it, it includes it. Naturally for a typing game this is a big deal! The following is my "enemy" class as it stands

   using Godot;
   using System;

     public partial class Enemy : Sprite2D
     {
	
	RichTextLabel prompt;
	
	public String prompt_text;
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		prompt = this.GetNode<RichTextLabel>("RichTextLabel");
		
		prompt_text = prompt.CleanString(prompt.Text);
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		GD.Print(prompt_text);
	}

and my attempt at doing this is referencing a GD script and calling the function "CleanString"

   class_name RegExCustomFunc extends RichTextLabel

    func _ready():
pass # Replace with function body.

    func _process(delta):
pass


    func CleanString()-> String:
var regex = RegEx.new()
regex.compile("\\[.*?\\]")
var text_without_tags = regex.sub(text, "", true)

return text_without_tags

Which naturally, doesn't work and returns "CS1061: 'RichTextLabel' does not contain a definition for 'CleanString' and no accessible extension method 'CleanString' accepting a first argument of type 'RichTextLabel' could be found (are you missing a using directive or an assembly reference?)"

Can't quite seem to figure out where I'm going wrong or if there's a better way to do what I'm doing! Anyone see my issue?

Thanks for reading and have a lovely day!

    KiwiCaveGremlin class_name RegExCustomFunc extends RichTextLabel

    In GDScript, you can't extend built-in classes such as RichTextLabel and add methods.

    You can still create a custom class, but you'll have to pass the RichTextString as a parameter to CleanString.

    Please place ~~~ before and after the code to display it correctly.

      DaveTheCoder Okay, so what would that look like? I'm very new to Godot so I'm not quite sure how to parse things with it's native script (I spent 2 days trying to figure out how I could get nodes via code haha). Is this something that's better to do via GDscript and call in C# or just handle it all via C#?

      I only use GDScript for Godot, so I can't give advice regarding C#. If you know how to use C#, you could master GDScript in a few hours.

        DaveTheCoder Your estimations of my talents are very kind! However I believe they may be slightly unfounded haha

        As for the GDScript error, I'm looking into using the "call" function to get a GDscript via my C# code but that's its own can of worms haha, thanks for the assistance, I'll let ya know if I make any progress here

        DaveTheCoder That said, I think I got this one! I'll post my work just in case anyone else stumbles across this specific issue!

        So, for context I have two relevant nodes. An Enemy with the enemy script attached, and a child node RichTextLabel with the script named "NewRegFunc"

        The code for the Enemy is as follows, utilizing a form of the Call function

        
        using Godot;
        using System;
        
        public partial class Enemy : Sprite2D
        {
        	
        	RichTextLabel prompt;
        	
        	[Export]
        	public String prompt_text;
        	
        	// Called when the node enters the scene tree for the first time.
        	public override void _Ready()
        	{
        		
        		prompt = this.GetNode<RichTextLabel>("RichTextLabel");
        		
        		//This is the money maker! Needs to be cast exactly like this to work. If yours isn't working, check the GetNode() directory path is correct!
        		prompt_text = (string)GetNode("RichTextLabel").Call("CleanString", prompt.Text);
        		
        		//Test just to make sure it prints correctly
        		GD.Print(prompt_text);
        	}
        	
        }

        And for my "RichTextLabel" extension we have....

        
        class_name NewRegFunc
        	
        extends RichTextLabel
        	
        func CleanString(rawBBCodeText: String)-> String:
        	var regex = RegEx.new()
        	regex.compile("\\[.*?\\]")
        	var text_without_tags = regex.sub(rawBBCodeText, "", true)
        
        	
        	return text_without_tags
        	# `text_without_tags` contains the text with all BBCode tags removed.

        And there you have it! A C# script that calls a GD script to remove the BBCode from a string and passes back to your C# script to use as you please! I hope someone else finds this useful, and thanks again Dave for your response!