Jump to content
The simFlight Network Forums

having multiple if-statements in one file


Recommended Posts

Hi folks, I have these two scripts which work seperately from each other:

spdstate = ipc.readSTR(0xABCD, 9)

if string.find(spdstate, "---")
    then
ipc.writeUB(0x66C2,1)
    else
ipc.writeUB(0x66C2,0)

end

----------------------------

spdstate = ipc.readSTR(0xABCD, 9)

if string.find(spdstate, "*")
    then
ipc.writeUB(0x66C3,1)
    else
ipc.writeUB(0x66C3,0)

end

 

I would like to fuse both of them into one script. I don't see how I should use the function expression for this. I could do a nested if statement with all possible four combinations, but I don't know how to code "not find".

Best regards

-shorthauler

 

 

Link to comment
Share on other sites

  • shorthauler changed the title to having multiple if-statements in one file
11 hours ago, shorthauler said:

I would like to fuse both of them into one script. I don't see how I should use the function expression for this. I could do a nested if statement with all possible four combinations, but I don't know how to code "not find".

You can use 'if ( not( string.find...' - but you shouldn't need this o combine them:

Quote

spdstate = ipc.readSTR(0xABCD, 9)

if (string.find(spdstate, "---") and string.find(spdstate, "*")) then
      ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,1)
elseif string.find(spdstate, "---") then
      ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,0)
elseif string.find(spdstate, "*") then
      ipc.writeUB(0x66C2,0)
      ipc.writeUB(0x66C3,1)
else
    ipc.writeUB(0x66C2,0)
    ipc.writeUB(0x66C3,0)
end

John

Link to comment
Share on other sites

Thanks a lot, John. This is the file, as it stands now. I have changed it a bit. We are looking at two elements here, dashes and dots, and I think it is necessary to explicitly add the "not string.find" part (I hope my syntax is ok).

The conundrum is that The 0x66C2 offset is always 1 where it should be 0 and vice versa. Even if I swap the ones and the zeroes in the code!

 

-- dashes C2, dot C3

spdstate = ipc.readSTR(0xABCD, 9)

if (string.find(spdstate, "---") and string.find(spdstate, "*")) then
      ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,1)
elseif (string.find(spdstate, "---") and not string.find(spdstate, "*")) then
      ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,0)
elseif (string.find(spdstate, "*") and not string.find(spdstate, "---")) then
      ipc.writeUB(0x66C2,0)
      ipc.writeUB(0x66C3,1)
else
    ipc.writeUB(0x66C2,0)
    ipc.writeUB(0x66C3,0)
end

speed = string.match(spdstate, "%d+")
ipc.writeSW(0x66C1, speed)

 

Link to comment
Share on other sites

1 hour ago, shorthauler said:

I think it is necessary to explicitly add the "not string.find" part (I hope my syntax is ok).

It is not necessary, as if  string.find was true then the first test (string.find(spdstate, "---") and string.find(spdstate, "*")) would be have been true...

1 hour ago, shorthauler said:

The conundrum is that The 0x66C2 offset is always 1 where it should be 0 and vice versa. Even if I swap the ones and the zeroes in the code!

Add some logging statements , e.g.

Quote

spdstate = ipc.readSTR(0xABCD, 9)
ipc.log("spdstate='" .. spdstate .. "'")

if (string.find(spdstate, "---") and string.find(spdstate, "*")) then
      ipc.log("*** Both true")
      ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,1)
elseif (string.find(spdstate, "---")) then
     ipc.log("*** Only --- found")
     ipc.writeUB(0x66C2,1)
      ipc.writeUB(0x66C3,0)
elseif (string.find(spdstate, "*")) then
     ipc.log("*** Only * found")
      ipc.writeUB(0x66C2,0)
      ipc.writeUB(0x66C3,1)
else
     ipc.log("*** Neither found")
    ipc.writeUB(0x66C2,0)
    ipc.writeUB(0x66C3,0)
end

speed = string.match(spdstate, "%d+")
ipc.writeSW(0x66C1, speed)

If you test with the console window open, this should tell you what is happening....

I think the issue is that the * character has a special meaning (match 0 or more repetitions) and needs to be escaped - maybe string.find(spdstate, "%*")?

John
 

Link to comment
Share on other sites

Yes, you were right, thank you very much. I realize that "*" is a "magic character". It can be escaped by putting "\" in front. Same goes for the hyphen / minus. In the script I have reduced the number of hyphens to one. Because there will always be three hyphens in the string or none. So it's enough to only find one.

But still the same problem. When 66C2 should be set to one, it is set to 0 and vice versa.

The script now looks like this, with line numbers:

1   -- dashes C2, dot C3
2
3   spdstate = ipc.readSTR(0xABCD, 9)
4
5   if (string.find(spdstate, "\-") and string.find(spdstate, "\*")) then
6       ipc.writeUB(0x66C2,1)
7       ipc.writeUB(0x66C3,1)
8   elseif string.find(spdstate, "\-") then
9       ipc.writeUB(0x66C2,1)
10     ipc.writeUB(0x66C3,0)
11 elseif string.find(spdstate, "\*") then
12    ipc.writeUB(0x66C2,0)
13    ipc.writeUB(0x66C3,1)
14 else
15    ipc.writeUB(0x66C2,0)
16     ipc.writeUB(0x66C3,0)
17 end
18
19 speed_str = string.match(spdstate, "%d+[%d.,]") -- speed as string
20 speed = tonumber(speed_str) -- converting speed from string to number
21 ipc.writeSW(0x66C1, speed)

 

 

Link to comment
Share on other sites

This is a snippet of the log file, with my commments:

  2750813 LUA.0: beginning "E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua"
=> The script is started, airplane is in managed mode with dashes and dot displayed.
  2750813 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:3
  2750813 LUA.0: Global: ipcPARAM = 0
=> I do not know which parameter this refers to. Line 3 is spdstate = ipc.readSTR(0xABCD, 9)
  2750813 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:5
  2750828 LUA.0: Global: spdstate = SPD
---*
=> I am puzzeled - there is a line change. The string seems to work in two lines. I wonder whether this makes the string difficult to read.
  2750828 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:6
  2750875 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:7
  2750875 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:19
  2750875 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:20
  2750891 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:21
=> Airplane has been changed to selected mode, knots. Then the script is called again.
  2756047 LUA.0: beginning "E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua"
  2756047 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:3
  2756047 LUA.0: Global: ipcPARAM = 0
  2756063 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:5
  2756063 LUA.0: Global: spdstate = SPD
289
=> Again, a line change.
  2756063 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:8
  2756078 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:11
  2756078 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:15
  2756109 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:16
  2756125 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:19
  2756125 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:20
  2756125 LUA.0: Global: speed_str = 289
=> Speed as characters, extracted correctly from string.
  2756125 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:21
  2756125 LUA.0: Global: speed = 289
=> Speed as number.
=> Airplane is in managed mode, speed changed to Mach.
  2759688 LUA.0: beginning "E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua"
  2759688 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:3
  2759703 LUA.0: Global: ipcPARAM = 0
  2759703 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:5
  2759703 LUA.0: Global: spdstate = MACH
0.54
=> Mach number read correctly from string.
  2759703 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:8
  2759719 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:11
  2759719 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:15
  2759766 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:16
  2759766 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:19
  2759766 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:20
  2759781 LUA.0: Global: speed_str = 0.
=> This should be only the numbers from the string. See other post on the Mach number.
  2759781 LUA.0: E:\FSX\Flight Simulator X\Modules\A320 SPD split.lua:21
  2759781 LUA.0: Global: speed = 0

 

Link to comment
Share on other sites

Apologies for the separate threads, but one is to extract the numbers from a string and write it into an offset, the other one (this one) is to learn how to find a certain character in a string and write 0 or 1 into an offset accordingly. I though it would be less confusing to separate the threads.

Best regards,
Holger

Link to comment
Share on other sites

55 minutes ago, shorthauler said:

the other one (this one) is to learn how to find a certain character in a string and write 0 or 1 into an offset accordingly.

This one is for 'having multiple if-statements in one file'....

I don't mind helping out with lua questions, but I am no lua expert. I support FSUIPC really, not lua. FSUIPC includes a lua interpreter, but for non-fsuipc related lua questions, you should refer to the available lua resources on the web. That is what I do when I get asked such questions...

John

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.