[help] Convert string to num

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Eagle_Four
Posts: 231
Joined: Wed, 6. Nov 02, 20:31
x3

[help] Convert string to num

Post by Eagle_Four » Wed, 5. Jul 23, 13:20

i am facing the problem that i need to convert a string variable into a numeric one. the string variable contains only a number.
I have already cut it to a number with substitute_text.

I wanted to use it here:

Code: Select all

<speak actor="player.computer" page="20101" line="$Ship_SpeakLine" priority="100"/>.

but line expects a number,since $Ship_SpeakLine is still a string variable,so the speaker remains silent.

Xenon_Slayer
EGOSOFT
EGOSOFT
Posts: 13096
Joined: Sat, 9. Nov 02, 11:45
x4

Re: [help] Convert string to num

Post by Xenon_Slayer » Wed, 5. Jul 23, 14:12

No, that's not possible. I've even looked for some hacky ways for you to do it but nothing yet :roll:
If you only have a small subset of possible lines, perhaps you could build a lookup table.
Come watch me on Twitch where I occasionally play several of the X games

Eagle_Four
Posts: 231
Joined: Wed, 6. Nov 02, 20:31
x3

Re: [help] Convert string to num

Post by Eagle_Four » Wed, 5. Jul 23, 15:39

Unfortunately, the list could be very long. I have read the Ship.Rawname and converted it so that only the text line is left. This could include all possible ships. I don't know the exact ship type at this point. Therefore this is not possible with a table (At least not in a simple way, I think).
I want the ship name to be spoken.

I had imagined it would be easier.

Lua provides a function: ConvertStringTo64Bit
But I would have to learn Lua first.

Maybe there is a function or possibility that converts this to e.g. {20101,12345} and returns me this or something like this: page id="20101" id="12345" ? But probably not.

User avatar
Dj_FRedy
Posts: 237
Joined: Mon, 27. Jun 11, 05:58
x4

Re: [help] Convert string to num

Post by Dj_FRedy » Wed, 5. Jul 23, 21:00

If you don't mind dealing with Lua, 'tonumber()' would suffice to convert a string with numeric matches.
I have prepared a mod for you so that you have an example.
Check the dependency in the readme file.
source: https://github.com/DjFRedy/X4-Extension ... f_lua_help
z_df_lua_help.7z: https://github.com/DjFRedy/X4-Extensions/releases

To create a call in your code, something like this:

Code: Select all

<set_value name="$speakline" exact="$Ship_SpeakLine"/>
<set_value name="$is_numeric_line" exact="(typeof $speakline).isnumeric"/>

<do_if value="$is_numeric_line">
	<debug_text text="'speakline: ' + $speakline" chance="100"/>
	<speak actor="player.computer" page="20101" line="$speakline" priority="100"/>
</do_if>
<do_else>
	<debug_text text="'line ' + $speakline +' is a string value, convert to number'" chance="100"/>
	<signal_cue_instantly cue="md.LuaHelp.StringToNumber" param="$speakline"/>
</do_else>
'StringToNumber' calls the previously created Lua event, 'onConvertStringToNumber()' converts the value, 'SpeakShipName' waits for the already converted value. You can move 'SpeakShipName' to your own cue, it's just the reception example. mm check it all out and see if it works for you.
"All my contributions to the Technical Support and Public Beta Feedback sections will be concise and to the point, no diatribes, that's what the other sections are for".
Thank you for your efforts.

Xenon_Slayer
EGOSOFT
EGOSOFT
Posts: 13096
Joined: Sat, 9. Nov 02, 11:45
x4

Re: [help] Convert string to num

Post by Xenon_Slayer » Thu, 6. Jul 23, 12:10

Have you tried passing in the ship itself into the <name> subnode?

Code: Select all

<speak actor="player.computer">
  <name value="$Ship"/>
</speak>
Come watch me on Twitch where I occasionally play several of the X games

Eagle_Four
Posts: 231
Joined: Wed, 6. Nov 02, 20:31
x3

Re: [help] Convert string to num

Post by Eagle_Four » Thu, 6. Jul 23, 14:59

Dj_FRedy wrote:
Wed, 5. Jul 23, 21:00
If you don't mind dealing with Lua, 'tonumber()' would suffice to convert a string with numeric matches.
I have prepared a mod for you so that you have an example.
Check the dependency in the readme file.
source: https://github.com/DjFRedy/X4-Extension ... f_lua_help
z_df_lua_help.7z: https://github.com/DjFRedy/X4-Extensions/releases


'StringToNumber' calls the previously created Lua event, 'onConvertStringToNumber()' converts the value, 'SpeakShipName' waits for the already converted value. You can move 'SpeakShipName' to your own cue, it's just the reception example. mm check it all out and see if it works for you.
What can I say? What you are doing is working. Just like in this case. I put that in my code and a few extra lines because I had to build a sentence together and the ship name was just a part of it. It works just like I thought it would. A very big thank you to you dear DJ_Freddy. I hope you stay loyal to X4 for a long time. You are a real enrichment. :mrgreen:

Especially I can use the extension now to realize some other useful functions which are easy to implent with LUA. I am missing so many functions, especially string operations. I can easily include those now. Lua doesn't seem too hard to learn. Is like any other normal programming language.
Xenon_Slayer wrote:
Thu, 6. Jul 23, 12:10
Have you tried passing in the ship itself into the <name> subnode?

Code: Select all

<speak actor="player.computer">
  <name value="$Ship"/>
</speak>
I have tried it out. Betty stays mute and doesn't say anything. :(
But thanks anyway for your effort.

Mycu
Posts: 410
Joined: Fri, 28. Sep 12, 08:30
x4

Re: [help] Convert string to num

Post by Mycu » Thu, 6. Jul 23, 15:17

Eagle_Four wrote:
Thu, 6. Jul 23, 14:59
Xenon_Slayer wrote:
Thu, 6. Jul 23, 12:10
Have you tried passing in the ship itself into the <name> subnode?

Code: Select all

<speak actor="player.computer">
  <name value="$Ship"/>
</speak>
I have tried it out. Betty stays mute and doesn't say anything. :(
But thanks anyway for your effort.
Xenon_Slayer solution should work - not only for ships, but also for factories, sectors etc.
Maybe you passed the 'rawname' instead of the actual object?
X3 AP 3.1 + XRM
X3 AP 3.1 + LU

User avatar
Dj_FRedy
Posts: 237
Joined: Mon, 27. Jun 11, 05:58
x4

Re: [help] Convert string to num

Post by Dj_FRedy » Thu, 6. Jul 23, 15:18

Xenon_Slayer 'Witchcraft', this works for me ...
"All my contributions to the Technical Support and Public Beta Feedback sections will be concise and to the point, no diatribes, that's what the other sections are for".
Thank you for your efforts.

Eagle_Four
Posts: 231
Joined: Wed, 6. Nov 02, 20:31
x3

Re: [help] Convert string to num

Post by Eagle_Four » Thu, 6. Jul 23, 17:59

Mycu wrote:
Thu, 6. Jul 23, 15:17
Eagle_Four wrote:
Thu, 6. Jul 23, 14:59
Xenon_Slayer wrote:
Thu, 6. Jul 23, 12:10
Have you tried passing in the ship itself into the <name> subnode?

Code: Select all

<speak actor="player.computer">
  <name value="$Ship"/>
</speak>
I have tried it out. Betty stays mute and doesn't say anything. :(
But thanks anyway for your effort.
Xenon_Slayer solution should work - not only for ships, but also for factories, sectors etc.
Maybe you passed the 'rawname' instead of the actual object?

Code: Select all

<set_value name="$ship" exact="event.param"/>
<!--

a lot of codelines later

-->
<speak actor="player.computer">
  <name value="$Ship"/>
</speak>
Who can find the error?

Now this also works. Thank you Xenon_Slayer. 'Witchcraft'? Cool.

Return to “X4: Foundations - Scripts and Modding”