Scripts / Lua question regarding a single quote within double quoted string

Author
Message
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 30th May 2022 04:03
Calling all Lua users with experience using it in GG classic.

I have just finished writing code to word-wrap a long (several hundred) character string. This can wrap on any desired display length. Upon tweaking the code, I noticed that when using a single quote/apostrophe the character doesn't show up. I tried using the "escape" method ( \' ) but that didn't work. \" will work but can't have a double quote for an apostrophe. LOL. Any ideas? Of course, I'm not expecting any replies since the script forum for GG classic is almost dead.
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 30th May 2022 08:32 Edited at: 30th May 2022 08:38
What are you displaying the string with?

i.e. Text command or Prompt?

Does GG even have that character in its font definition?

***Edited to add: Looks like it does!
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 31st May 2022 06:01 Edited at: 31st May 2022 06:05
This is test code displaying text.

Panel(23,2,87,38)
TextColor(25,5,3,"string.find (s, pattern [, init [, plain]])",255,0,0)
Text(25,10,3,g_LineText[1])
Text(25,14,3,g_LineText[2])
Text(25,18,3,g_LineText[3])
Text(25,22,3,g_LineText[4])
Text(25,26,3,g_LineText[5])
Text(25,30,3,g_LineText[6])
Text(25,34,3,g_LineText[7])

The following is the text I used.


The following are the variations. "find substring" is the phrase I used as an example. The one where you see the double quotes is when I used the escape character ( \" ) The other two do not show the use of a single quote ( 'find substring' ) in which in one case I used the escape character and the other without. This has always been typical of TGC's engines. For some people it works and for others it won't work if our life depended upon it.



Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory

Attachments

Login to view attachments
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 31st May 2022 09:30
Without seeing the script I can't really help, sorry.
Been there, done that, got all the T-Shirts!
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 2nd Jun 2022 00:31 Edited at: 2nd Jun 2022 00:32
No worries. The only time I want to use a single quote is when I use contractions; eg, can't for cannot. So, I just won't use contractions. :LOL:
If I want to quote something or emphasize a phrase, I will use \".

Thanks so much for taking the time to look at this and responding. You know, I use a lot of your scripts plus the two utility code libraries. I especially found the code for "hidden objects" quite useful.

The attached file is the word wrap script I coded for GG. A long time ago,in a galaxy far, far away, I wanted the ability to word wrap in FPSC. This was in the era we were able to actually have the source code for the engine and if you got DBPro you were all set. I first searched various engine source codes. I Found something in C used in one of the few game engines I had access to. I then proceeded to translate it into DBPro which was the source code for FPSC. This language was similar to VB. It allowed the ability to access a single letter using a loop. here is the code for DBPro.

function rpg_showtalkbox()
local i as integer
`
tbwa_sTemp$ = tbwa_text$
tbwa_sTemp$ = tbwa_sTemp$ + " "
tbwa_lines = 1
`
set text font rpg_chattextstyle.fontname$
set text size rpg_chattextstyle.fontsize
if rpg_chattextstyle.fontbold = 1
set text to bold
else
set text to normal
endif
`
while text width(tbwa_sTemp$) > tbwa_width
tbwa_length = len(tbwa_sTemp$)
for i = 1 to tbwa_length
if mid$(tbwa_sTemp$, i) = " "
tbwa_wIndex = i
endif
if text width(left$(tbwa_sTemp$,i)) > tbwa_width
inc tbwa_lines
exit
endif
next i
tbwa_sTemp$ = right$(tbwa_sTemp$,tbwa_length-tbwa_wIndex)
endwhile
`
tbwa_height = text height(tbwa_text$) * (tbwa_lines)
ink tbwa_bColor,0
box tbwa_x,tbwa_y,tbwa_x+tbwa_width,tbwa_y+tbwa_height
ink tbwa_tColor,0
wrapText(tbwa_x,tbwa_y,tbwa_text$,tbwa_width,0)
`
endfunction

You will notice there is a command Lua doesn't have and that is "mid$" which allows you to parse one character at a time. What I decided to do with Lua was to break it down into word chunks and placed them in a table. I then concatenated each chunk until it formed a line of text that fit the desired text width. The number of lines is determined by length of the string divided by the desired length of the line. In the example, I used "70" as the length.

I congratulate all of you who has read this to the end. Kudos and commendations to you. I realize this is a long-winded post, however, I enjoyed writing it for the nostalgia and a pat on God's back for helping me figure out the logic and writing of the Lua code. For something that would have taken me one afternoon to write, in my state, it took me several weeks off and on to write. Part of the delay is not listening to the Holy Spirit on several occasions.
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory

Attachments

Login to view attachments
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 2nd Jun 2022 10:43
string.sub( str, i, j )

Will give you the part of the string from the i'th character to the j'th one, if j is omitted you get the rest of the string returned.

So string.sub( "Hello World", 6 ) would return "World"
and string.sub( "Hello World", 6, 6 ) would return "W".

When used in conjunction with string.find it is all pretty powerful!
Been there, done that, got all the T-Shirts!
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 4th Jun 2022 05:05 Edited at: 4th Jun 2022 05:07
Thank you for showing me that you can use "string.sub." When I initially looked at that command and concluded it only returned several letters. I was used to mid$(s, i) and as you can see when you iterate through the string all you need to do is increase the index (i) by one. That's for some reason how I thought the "sub" method would work. So, I tried calling it thusly: string.sub( s,i ) thinking that it would work the same as mid$. So, s was equal to the whole text, and "i" began at 1. You can guess what it returned. Yup, the entire sentence. It is good to know that "j" needs to be set to the same value as "i" when iterating through the string, one character at a time. I actually hope this is helpful for others. It sure was helpful for me. string.sub( s,i ,j)
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Jun 2022 11:24
local function midStr( str, chr )
return string.sub( str, chr, chr )
end

There you go, now you can do:

local character = midStr( "Hello", 3 )

Which should give 'e'

Been there, done that, got all the T-Shirts!
PM
Flatlander
GameGuru Master
17
Years of Service
User Offline
Joined: 22nd Jan 2007
Location: The Flatlands
Posted: 4th Jun 2022 23:08
Wonderful
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory

Login to post a reply

Server time is: 2024-02-27 22:33:16
Your offset time is: 2024-02-27 22:33:16