PDA

View Full Version : (FGU) Problem with replacing characters in a stringfield



Synisill
October 18th, 2020, 16:05
Edited: -PROBLEM SOLVED, solution beneath- This thread can be closed or moved.

Hello, dear developers!

I am struggling with a rather simple task. By now i am guessing there has to be an error in FGU, because i have double-checked everything i have done on my side. My problem in short words: i want to replace a certain pattern within a stringfield with an ANSI (or ASCII, i can not differentiate that properly) character. When i, for example, enter "(S)" into a stringfield, it should be replaced with the character ¸ (184).

First of all, i created a copy of the 5E ruleset in the same folder and edited the base.xml
<description>
<displayname>Atest</displayname>
<displayversion>1</displayversion>
<text>Atest ruleset</text>
<author>me</author>
<website>https://www.fantasygrounds.com</website>
</description>

Second, i created a ttf-font with the symbol i want at character 184. Copied it into \rulesets\atest\graphics\fonts

Third, i edited graphics_fonts.xml and inserted an "override" of the wanted font:
<font name="sheettext">
<ttf file="graphics/fonts/Genesys Regular.ttf" name="Genesys Regular" size="16" />
<color value="#000033" />
</font>

(Btw, i checked to link to the right font name! I am going to try my changes at a <stringu>-element which is derived from the CoreRPG ruleset:
<template name="stringu">
<stringfield>
<font>sheettext</font>
<lineoffset default="on" mergerule="replace">1</lineoffset>
</stringfield>
</template>
)


Fourth, i added my code to \rulesets\atest\campaign\record_char_notes.xml - at that spot there are no other elements which could interfere. I took the "Deity" entry as an example. Here is the code:
<stringu name="deity">
<anchored to="detailstitle2" position="belowright" offset="0,5" height="20">
<left anchor="center" offset="5" />
</anchored>
<script>
function onValueChanged()

-- get the source node value
local oldvalue = getValue();
local newvalue = oldvalue;

-- perform symbol replacements between [] or ()
newvalue = string.gsub(newvalue, "%(S%)", string.char(184));
newvalue = string.gsub(newvalue, "%(!%)", string.char(185));
newvalue = string.gsub(newvalue, "%[S%]", "&amp;#184;");
newvalue = string.gsub(newvalue, "%[!%]", "&amp;#185;");

-- set the new source node value
if newvalue ~= oldvalue then
setValue(newvalue);
end

end
</script>
</stringu>


I hope it will not be partly replaced when i send it. If so, i will add a screenshot after my post here.

That was it. All that work - for naught. In test mode, the replacement works in parts. The pattern is replaced as soon as i enter it, BUT with a question mark character (64) OR the blank html-mask as text (& # 184 ;).
Also note that the same code WORKS 100% in a formatted textfield! Can someone please help me here? I ran out of ideas.
A picture of the faulty result as it is will follow as attachment now:

Synisill
October 18th, 2020, 16:10
Why? does it not show the right character? (Addendon: When i COPY the characters from another source into the very field, they are shown as they should. So the font and the stylesheet work!)

40311

Moon Wizard
October 18th, 2020, 23:51
String fields can only handle basic text strings; and have a different format that formatted text fields (which use a very simplified HTML style) for programmatically retrieving and setting data.
Also, FGU uses UTF-8 for strings; whereas FGU uses ASCII for strings. So, anything over character code 128 will be potentially different between the 2 systems.

Regards,
JPG

Synisill
October 19th, 2020, 08:14
Thank you very much, Moon Wizard!! Your reply helped me a good deal. I am at work right now, so i could not test it yet - but how would the right notation in LUA would look like? I found out that the example character ¸ would be "194184" or "c2 b8" (hex) in UTF-8. But i guess when i put in both chars in sequential order, two characters will be displayed, not one.
And as the html-mask (&#xxxx; ) does not work with stringfields, i just would like to ask if there IS a possibility to do this with LUA, maybe via a function the StringManager from CoreRPG provides?

Trenloe
October 19th, 2020, 09:49
Look at scripts/manager_gamesystem.lua in the PFRPG2 ruleset. This has a function that replaces characters with character codes for specific symbols - and it has code for FGC and FGU. I’m not at my computer so can’t point to the exact code right now.

Moon Wizard
October 20th, 2020, 00:42
@Synisill,

Like I mentioned above, FGC only supports ISO-8859-1 encoding using single character strings (one byte per character); so it wouldn't be able to handle 2 byte characters at all.
For the character encoding, as far as I know, you can insert a character using string.char Lua function.

Even what PFRPG2 ruleset does requires special coding and special fonts. Perhaps it might be better to simplify your solution not to need special characters at all?

Regards,
JPG

Synisill
October 20th, 2020, 04:58
My main topic aside, at this point i want to thank you from heart for your support! I did not expect you to occupy yourself as deeply with my special demand, and i appreciate it greatly! Thank you!

With Moon's clarification and Trenloe's hint, i was able to solve my problem once and for all. As soon as i leave the ASCII range, i will have to replace the "simple" string.char(184) with a more complex formula. For decimals up to 2047 it would be string.char(192 + 184/64, 128 + 184%64) or, a bit easier to understand, the string "\192\184".
Fifth, for everyone interested, the easy solution looks like:

local sOriginalText = sActionText;

-- Unity uses two characters for these special symbols - \194\xxx - where xxx is the usual character code.
local sUnityExtraChar = "";
if isUnity() then
sUnityExtraChar = "\194";
end

sActionText = sActionText:gsub("%[%[S%]%]", sUnityExtraChar .. "\184");


A last note for you, the developers: maybe it would be useful for other developers who toy around with FGU to put some generic helper functions into the CoreRPG StringManager. You can find some very generic code at https://github.com/luapower/utf8