godot-docs
Add C# version for examples of custom BBCode tags and text effects
#9420
Open

Add C# version for examples of custom BBCode tags and text effects #9420

mateuseap wants to merge 1 commit into godotengine:master from mateuseap:feat/mesh
mateuseap
mateuseap353 days ago (edited 353 days ago)

What I did

  • Added C# version of the examples for creating custom BBCode tags and text effects.

Closes: #9334

AThousandShips AThousandShips requested a review 353 days ago
AThousandShips AThousandShips added topic:dotnet
AThousandShips AThousandShips added area:manual
AThousandShips AThousandShips added topic:gui
raulsntos
raulsntos commented on 2024-05-26
tutorials/ui/bbcode_in_richtextlabel.rst
943 // Syntax: [ghost freq=5.0 span=10.0][/ghost]
944
945 // Define the tag name.
946
public string bbcode = "ghost";
raulsntos353 days ago👍 1

We should make this a constant, and it probably doesn't need to be public. Although I don't see this being used at all.

Suggested change
public string bbcode = "ghost";
private const string Bbcode = "ghost";
RedMser352 days ago (edited 352 days ago)👍 1

It seems to be magic - not sure how it exactly needs to be defined to be read in C# but in GDScript it just is a variable named bbcode:

https://docs.godotengine.org/en/stable/classes/class_richtexteffect.html

This has to be tested / check RichTextEffect source code in godot.

raulsntos352 days ago

Ah, I see. It looks like this only works with scripts that have a property named exactly bbcode:

https://github.com/godotengine/godot/blob/be56cab58c056c074d1e02cd0b38641204e39f41/scene/gui/rich_text_effect.cpp#L49

That's a weird pattern, I don't think we usually do that. For example, in editor plugins there are virtual methods for doing this kind of thing (e.g.: EditorPlugin::_get_plugin_name).

So to make this work the member would have to be named bbcode even though that goes against our C# naming conventions. And it can't be a constant.

Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1009 // Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
1010
1011 // Define the tag name.
1012
public string bbcode = "matrix";
raulsntos353 days ago👍 1
Suggested change
public string bbcode = "matrix";
private const string Bbcode = "matrix";
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
945 // Define the tag name.
946 public string bbcode = "ghost";
947
948
public override bool _ProcessCustomFx(CharFXTransform charFx)
raulsntos353 days ago👍 1

To match the parameter name in the virtual method definition.

Suggested change
public override bool _ProcessCustomFx(CharFXTransform charFx)
public override bool _ProcessCustomFx(CharFXTransform charFX)
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1017 return TextServerManager.GetPrimaryInterface();
1018 }
1019
1020
public override bool _ProcessCustomFx(CharFXTransform charFx)
raulsntos353 days ago👍 1
Suggested change
public override bool _ProcessCustomFx(CharFXTransform charFx)
public override bool _ProcessCustomFx(CharFXTransform charFX)
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
951 var speed = charFx.Env.Get("freq", 5.0f);
952 var span = charFx.Env.Get("span", 10.0f);
953
954
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.x / span)) * 0.5f + 0.5f;
955
charFx.Color.a = alpha;
raulsntos353 days ago👍 1
Suggested change
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.x / span)) * 0.5f + 0.5f;
charFx.Color.a = alpha;
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.X / span)) * 0.5f + 0.5f;
charFx.Color.A = alpha;
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1026
1027 var value = charFx.GlyphIndex;
1028
1029
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.x / textSpan), clearTime + dirtyTime);
raulsntos353 days ago👍 1
Suggested change
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.x / textSpan), clearTime + dirtyTime);
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.X / textSpan), clearTime + dirtyTime);
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
933934
935 .. code-tab:: csharp
936
937
#if TOOLS
raulsntos353 days ago👍 1

This class still needs to exist in exported games, so you don't want to conditionally compile this code only when the TOOLS define constant is present.

Suggested change
#if TOOLS
mateuseap mateuseap force pushed from fe47fa8d to f42ff8d4 352 days ago
mateuseap mateuseap requested a review from raulsntos raulsntos 352 days ago
mateuseap
mateuseap352 days ago

Thank you, @raulsntos, for the code corrections you've provided! I've applied all of them to the examples.

raulsntos
raulsntos commented on 2024-05-27
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
942 // Syntax: [ghost freq=5.0 span=10.0][/ghost]
943
944 // Define the tag name.
945
private const string Bbcode = "ghost";
raulsntos352 days ago👍 1

Sorry about this, as explained in #9420 (comment) this needs to be named exactly bbcode.

Suggested change
private const string Bbcode = "ghost";
private string bbcode = "ghost";
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1006 // Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
1007
1008 // Define the tag name.
1009
private const string Bbcode = "matrix";
raulsntos352 days ago👍 1
Suggested change
private const string Bbcode = "matrix";
private string bbcode = "matrix";
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
944 // Define the tag name.
945 private const string Bbcode = "ghost";
946
947
public override bool _ProcessCustomFx(CharFXTransform charFX)
raulsntos352 days ago👍 1
Suggested change
public override bool _ProcessCustomFx(CharFXTransform charFX)
public override bool _ProcessCustomFX(CharFXTransform charFX)
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
950 var speed = charFx.Env.Get("freq", 5.0f);
951 var span = charFx.Env.Get("span", 10.0f);
952
953
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.X / span)) * 0.5f + 0.5f;
raulsntos352 days ago👍 1

Since the parameter name was changed.

Suggested change
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.X / span)) * 0.5f + 0.5f;
var alpha = Mathf.Sin(charFX.ElapsedTime * speed + (charFX.Range.X / span)) * 0.5f + 0.5f;
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
951 var span = charFx.Env.Get("span", 10.0f);
952
953 var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.X / span)) * 0.5f + 0.5f;
954
charFx.Color.A = alpha;
raulsntos352 days ago👍 1
Suggested change
charFx.Color.A = alpha;
charFX.Color.A = alpha;
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1014 return TextServerManager.GetPrimaryInterface();
1015 }
1016
1017
public override bool _ProcessCustomFx(CharFXTransform charFX)
raulsntos352 days ago👍 1
Suggested change
public override bool _ProcessCustomFx(CharFXTransform charFX)
public override bool _ProcessCustomFX(CharFXTransform charFX)
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1021 var dirtyTime = charFx.Env.Get("dirty", 1.0f);
1022 var textSpan = charFx.Env.Get("span", 50);
1023
1024
var value = charFx.GlyphIndex;
raulsntos352 days ago👍 1
Suggested change
var value = charFx.GlyphIndex;
var value = charFX.GlyphIndex;
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1023
1024 var value = charFx.GlyphIndex;
1025
1026
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.X / textSpan), clearTime + dirtyTime);
raulsntos352 days ago👍 1

There's no Mathf.Fmod, use the % operator instead.

Suggested change
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.X / textSpan), clearTime + dirtyTime);
var matrixTime = (charFX.ElapsedTime + (charFx.Range.X / textSpan)) % (clearTime + dirtyTime);
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1033 value %= (126 - 65);
1034 value += 65;
1035 }
1036
charFx.GlyphIndex = GetTextServer().FontGetGlyphIndex(charFx.Font, 1, value, 0);
raulsntos352 days ago👍 1
Suggested change
charFx.GlyphIndex = GetTextServer().FontGetGlyphIndex(charFx.Font, 1, value, 0);
charFX.GlyphIndex = GetTextServer().FontGetGlyphIndex(charFX.Font, 1, value, 0);
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
947 public override bool _ProcessCustomFx(CharFXTransform charFX)
948 {
949 // Get parameters, or use the provided default value if missing.
950
var speed = charFx.Env.Get("freq", 5.0f);
951
var span = charFx.Env.Get("span", 10.0f);
raulsntos352 days ago👍 1

Godot.Dictionary doesn't implement a Get method. Use the extension method GetValueOrDefault. And cast to float because the dictionary returned by Env is untyped.

Suggested change
var speed = charFx.Env.Get("freq", 5.0f);
var span = charFx.Env.Get("span", 10.0f);
var speed = (float)charFx.Env.GetValueOrDefault("freq", 5.0f);
var span = (float)charFx.Env.GetValueOrDefault("span", 10.0f);
Conversation is marked as resolved
Show resolved
tutorials/ui/bbcode_in_richtextlabel.rst
1017 public override bool _ProcessCustomFx(CharFXTransform charFX)
1018 {
1019 // Get parameters, or use the provided default value if missing.
1020
var clearTime = charFx.Env.Get("clean", 2.0f);
1021
var dirtyTime = charFx.Env.Get("dirty", 1.0f);
1022
var textSpan = charFx.Env.Get("span", 50);
raulsntos352 days ago👍 1
Suggested change
var clearTime = charFx.Env.Get("clean", 2.0f);
var dirtyTime = charFx.Env.Get("dirty", 1.0f);
var textSpan = charFx.Env.Get("span", 50);
var clearTime = (float)charFx.Env.GetValueOrDefault("clean", 2.0f);
var dirtyTime = (float)charFx.Env.GetValueOrDefault("dirty", 1.0f);
var textSpan = (float)charFx.Env.GetValueOrDefault("span", 50);
mateuseap Add C# version for examples of custom BBCode tags and text effects
6d03be11
mateuseap mateuseap force pushed from f42ff8d4 to 6d03be11 351 days ago
mateuseap
mateuseap351 days ago

Thanks once again for the providing all these adjustments, @raulsntos! I've applied them all.

mateuseap mateuseap requested a review from raulsntos raulsntos 351 days ago
mateuseap mateuseap requested a review from RedMser RedMser 351 days ago
RedMser
RedMser commented on 2024-05-28
RedMser351 days ago👍 1

You should probably try following the tutorial here and see if the effect actually works as expected, especially for things like the bbcode string since it seems to be a very weirdly handled thing.

mateuseap
mateuseap350 days ago (edited 350 days ago)

I've created a C# script to test out the matrix effect code provided in this PR, but I wasn't able to load this custom effect — it didn't show up in the inspector. On the other hand, I created a script called RichTextGhost.gd using the GDScript code provided in the tutorial, and it worked properly. I was able to load and use it.

The effect in action:
2024-05-29-01-23-33

Do you guys have any idea of what could be the problem?

raulsntos
raulsntos350 days ago

You have to use the [GlobalClass] attribute to get C# classes to show up in the inspector, it's somewhat equivalent to GDScript's class_name.

mateuseap
mateuseap350 days ago (edited 350 days ago)

I've added [GlobalClass], but it still didn't show up as expected:

image

I've tested many things but still haven't figured out the problem.

paulloz
paulloz350 days ago🎉 1

After a very quick test, I don't seem to have an issue seeing the effect in editor once it is marked with [GlobalClass].
godot windows editor dev x86_64 mono_01mCxiLfXw

mateuseap
mateuseap350 days ago

Then it's probably a misconfiguration with my Godot. I've tried it on two different devices (both with the .NET 6.0 SDK (v6.0.423) - Windows x64 installed) but still I wasn't able to use the C# version of these custom effects, not even was able to load them in the inspector using the [GlobalClass].

image

I've used v4.3.dev5.mono.official [89f70e98d] and v4.2.2.stable.mono.official [15073afe3].

paulloz
paulloz350 days ago

Did you compile the C# solution?

AThousandShips
AThousandShips commented on 2024-05-29
tutorials/ui/bbcode_in_richtextlabel.rst
913913
914::
915
914.. tabs::
915 .. code-tab:: gdscript GDScript
916
AThousandShips350 days ago (edited 350 days ago)
Suggested change

No space, rather than deleting the line, GitHub doesn't allow that clearly

mateuseap350 days ago

I don't get it, you suggested to delete the line between the code-tab and the code itself?

AThousandShips
AThousandShips commented on 2024-05-29
tutorials/ui/bbcode_in_richtextlabel.rst
932933 return true
933934
935 .. code-tab:: csharp
936
AThousandShips350 days ago (edited 350 days ago)
Suggested change

Same here

Login to write a write a comment.

Login via GitHub

Assignees
No one assigned
Labels
Milestone