Jump to content
The simFlight Network Forums

Recommended Posts

Posted

From what I can tell, the ipc.ask() lua function does not currently work in MSFS.  Is there another way with MSFS to get a multi-digit numerical variable value entered by the user into a Lua script at script run time? I'm trying to write a script that asks the user to input a number value of their choice between 1 and 999.

Best wishes for the holidays!

Thanks,

Al

Posted

Hi Al,

No, the ipc.ask() lua function isn't currently working due to problems with the SimConnect_Text function. Unfortunately I don't think there is an alternative to this at the moment, sorry.

On 12/25/2020 at 6:07 PM, ark1320 said:

Best wishes for the holidays!

Thanks - and a very Happy festive period for you as well!

John 

Posted
2 hours ago, John Dowson said:

No, the ipc.ask() lua function isn't currently working due to problems with the SimConnect_Text function. Unfortunately I don't think there is an alternative to this at the moment, sorry.

Perhaps the OP could emulate ipc.ask by using the wnd library facilities and trapping the reply keypresses with event.key?

Pete

 

Posted
2 hours ago, Pete Dowson said:

Perhaps the OP could emulate ipc.ask by using the wnd library facilities and trapping the reply keypresses with event.key?

Maybe that could work, although you would have to terminate the event.key function on an enter or return key press. Are there any standard lua library functions that can get keyboard input? I tried with io.read but this doesn't seem to get any input. I'll take a look using event.key for this in the next few days.

John

Posted
4 minutes ago, John Dowson said:

Are there any standard lua library functions that can get keyboard input? I tried with io.read but this doesn't seem to get any input

I've never tries actually opening the keyboard as a device. How did you identify it for the io.open?

7 minutes ago, John Dowson said:

I'll take a look using event.key for this in the next few days.

event.key relies on FSUIPC detecting a keypress and invoking the event, so if we are receiving keypresses when MSFS has focus, which I think must be so, then event.key should work.

Pete

 

 

Posted

 I have to wonder if Asobo realizes just how messed up their text functions are, at least when used through SimConnect.  Even when a Lua single line text display shows up correctly, if often displays multiple times for no apparent reason.  If you could come up with a work around, that would certainly be VERY helpful.

Thanks,

Al

Posted
57 minutes ago, ark1320 said:

If you could come up with a work around, that would certainly be VERY helpful.

The "work around" for ipc.ask I was suggesting is by using existing Lua library functions, not replacing the code for ipc.ask. I would suggest John just keeps pressure on Asobo for working SimConnect Text facilities rather than re-write that function!

There's a lot of pressure on them already for the related Menu facilities, needed by a number of add-ons.

Pete

 

Posted
22 minutes ago, Pete Dowson said:

The "work around" for ipc.ask I was suggesting is by using existing Lua library functions, not replacing the code for ipc.ask. I would suggest John just keeps pressure on Asobo for working SimConnect Text facilities rather than re-write that function!

There's a lot of pressure on them already for the related Menu facilities, needed by a number of add-ons.

Pete

 

Agree!  And I really appreciate your and John's support efforts.

Al

Posted
8 hours ago, John Dowson said:

@ark1320 You can try the attached lua script and adjust as needed. It pops up a window asking for a number, terminated by an enter/return key. It just logs the number entered.

John

askTest.lua 2.28 kB · 3 downloads

Hi John,

Thanks very much for the script. I've been trying it a bit and it seems to work (although just a few times nothing happened when I pressed the key I have assigned to run it -- maybe some kind of timing glitch).

What didn't work is changing the numbers in the code line that defines the display window 

local w = wnd.open("Asking for inout: test", WND_FIXED, 500,200,500,60)

Nothing I did made any difference for some reason. I have tried restating both FSUIPC7 and the sim after changing these numbers but don't see any change when the script is run. I will keep experimenting with the script a bit later today.

Al

Posted

John,

I'm trying to understand how the askTest script works and  thinking about how to incorporate the askTest code in a larger program.

At first I was just going to make the askTest code into a 'large' function that could be called as needed, but now I'm wondering if that will work with the event type function calls the script contains.  Can event calls be internal to a called function, or do they essentially have to be part of the main program since they just sit and wait to be triggered? 

It seems from the askTest code that if you have 10 event.key() functions then you need 10 event.cancel()  functions -- apparently one event.cancel () function will not call all the event.key() functions?

BTW, I see the line  event.cancel("mytimer2") but do not see any function named mytimer2. I assume the 2 in the function name is a typo.

Thanks again for the script,

Al

Posted
15 hours ago, ark1320 said:

Nothing I did made any difference for some reason. I have tried restating both FSUIPC7 and the sim after changing these numbers but don't see any change when the script is run. I will keep experimenting with the script a bit later today.

Once the window has been created, it saves its size and position in the FSUIPC7.ini file - look for the following line in the [ExtWindow] section and remove it:
     Asking for inout: test=[444,146,346,102]
Note that you can manually move and resize the display window by selecting it and then using the arrow keys, with Ctrl switching between move and resize.

15 hours ago, ark1320 said:

Can event calls be internal to a called function, or do they essentially have to be part of the main program since they just sit and wait to be triggered? 

They can be be anywhere.

15 hours ago, ark1320 said:

It seems from the askTest code that if you have 10 event.key() functions then you need 10 event.cancel()  functions -- apparently one event.cancel () function will not call all the event.key() functions?

Yes, this is a bug that was picked-up recently. I will correct this in the next release so that a single evemt.cancel() call will cancel all events registered on that function.

15 hours ago, ark1320 said:

BTW, I see the line  event.cancel("mytimer2") but do not see any function named mytimer2. I assume the 2 in the function name is a typo.

Yes, thats a typo (as well as 'inout' in the name of the window created). I will correct and replace the uploaded file.

John
 

Posted
5 hours ago, John Dowson said:

Once the window has been created, it saves its size and position in the FSUIPC7.ini file - look for the following line in the [ExtWindow] section and remove it:
     Asking for inout: test=[444,146,346,102]

That fixed the issue with resizing/moving the window -- thanks.

5 hours ago, John Dowson said:

They (event type function calls) can be anywhere.

Just to clarify, so if an event function call like event.key() is called as part of a larger function, does that event remain active even after the program returns from the larger function?

BTW, I noticed the mytimer() and keyPressed() functions did not end with a Return instruction -- does that have something to do with the nature of the event .cancel() function calls contained in the mytimer() and keyPressed() functions?

Thanks,

Al

Posted
51 minutes ago, ark1320 said:

Just to clarify, so if an event function call like event.key() is called as part of a larger function, does that event remain active even after the program returns from the larger function?

Yes, that should be the case, although I haven't tried this....

52 minutes ago, ark1320 said:

BTW, I noticed the mytimer() and keyPressed() functions did not end with a Return instruction

It depends on what you mean by 'end'!! If they had an event call, the function should exit, but the lua thread will keep running as it is waiting for events....
Remember that each lua will run in its own thread, and that thread will terminate when that lua has finished. If it has event.* calls, it will only terminate when those event calls have been cancelled.

John

Posted
1 hour ago, John Dowson said:

Yes, that should be the case, although I haven't tried this....

It depends on what you mean by 'end'!! If they had an event call, the function should exit, but the lua thread will keep running as it is waiting for events....
Remember that each lua will run in its own thread, and that thread will terminate when that lua has finished. If it has event.* calls, it will only terminate when those event calls have been cancelled.

John

What I'm asking about regarding the return instruction is I normally think of a function definition as having the form:

function name( params )

    body of the function

   return    -- return to where function was called from

end

I was surprised to see that the mytimer() and keyPressed() functions did not have a return instruction to transfer code execution back to the calling location.

BTW, do you have any idea at this point about when FSUIPC7 will be updated again?

Thanks for your time with all this,

Al

Posted
15 minutes ago, ark1320 said:

I was surprised to see that the mytimer() and keyPressed() functions did not have a Return instruction to transfer code execution back to the calling location.

"return" is used in Lua to terminate the function other than at the 'end', otherwise the latter does the job. Same in C/C++ -- you only need the return for functions returning a value or to terminate before the end.

Pete

 

Posted
20 minutes ago, ark1320 said:

I was surprised to see that the mytimer() and keyPressed() functions did not have a Return instruction to transfer code execution back to the calling location.

But the function returns on end - you only need to explicitly return if you want to pass back a value, which isn't needed by these functions....
... I see Pete has also replied in the same vein...!

John

Posted
11 minutes ago, John Dowson said:

But the function returns on end - you only need to explicitly return if you want to pass back a value, which isn't needed by these functions....
... I see Pete has also replied in the same vein...!

John

Ah, I have learned something! Thanks Pete and John for the clarification.  As you can tell, my programming experience has been limited. I expect you could also use a return if you need to exit the function early before the end,  e.g., in the middle of the function body based on some data variable.

Best,

Al

Posted
20 minutes ago, ark1320 said:

I expect you could also use a return if you need to exit the function early before the end,  e.g., in the middle of the function body based on some data variable.

Yes, exactly (as I said).

Pete

 

Posted

I have been trying to develop a function based on the askTest.lua script that could be called from a main program wherever numerical input was needed -- no success so far.  So to make sure I understood correctly how things worked, I wrote the following little test script. I expected that once the event.key() function was executed, then if I pressed the 5 key while the k counter was counting up, the event.key function would detect the key press and the DisplayTest function would be called and Test would be briefly displayed. But as far as I can tell, nothing happens when I press the 5 key -- all I see is k continuing to count up without any interruption.  Even if I disable the display of k, I get no response to pushing the 5 key. So either there is something fundamental I don't understand, or maybe it is a SimConnect text display problem?

Best for the New Year!

Al

function DisplayTest()
  ipc.writeSTR(0x3380, "Test")
  ipc.writeSW(0x32FA, 3)
  ipc.sleep(3000)
end

-- Main program

event.key(0x35 , 0, "DisplayTest")   -- 5 digit
k=0
while k <= 3600 do
 k = k+1
 ipc.writeSTR(0x3380, k)
 ipc.writeSW(0x32FA, 1)
 ipc.sleep(1000)
end

Posted
8 hours ago, ark1320 said:

So either there is something fundamental I don't understand, or maybe it is a SimConnect text display problem?

SimConnect Text facilities aren't working properly at all in MSFS. That's the reason John used the Lua "wnd" library to create a window instead!

Pete

 

Posted
5 hours ago, Pete Dowson said:

SimConnect Text facilities aren't working properly at all in MSFS. That's the reason John used the Lua "wnd" library to create a window instead!

Pete

 

My issue here is when the counter is running, the event.key() function does not seem to work. I changed the test script to the below. The counter counts up and displays correctly, but when I press the 0 key, the count is not reset as I would expect . It seems the event.key(0x30 , 0, "ResetCount") function does not detect a 0 key press and call the ResetCount function to reset the counter.   I don't understand why event.key() is not working. The 0 key is not assigned in FSUIPC7 or in MSFS, so it is not being 'trapped' as best as I can tell. I also tried detecting other number keys in this test script, none work.

Thanks for any ideas,

Al

function ResetCount()
  k = 0
end

-- Main program
w = wnd.open("Counter test", WND_FIXED, 800,100,200,30)        
wnd.backcol(w, 0x000)
wnd.show(w, WND_TOPMOST)                    
wnd.textcol(w, 0x6c0)
wnd.font(w, WND_ARIAL,-1)
event.key(0x30 , 0, "ResetCount")   -- detect 0 keypress
k=0
while k <= 3600 do
 k = k+1
 wnd.text(w, "Count = "..k)
 ipc.sleep(1000)
end

Posted
51 minutes ago, ark1320 said:

I don't understand why event.key() is not working.

This part of the code:

while k <= 3600 do
 k = k+1
 wnd.text(w, "Count = "..k)
 ipc.sleep(1000)
end

is a loop which makes the Lua stay executing that sequence for 3,600 seconds. Events can only occur when the Lua program relinquishes control, so therefore waiting for the next event.

You should never use such loops in an event-driven plug-in.  Put that code into a function and call it with event.timer(1000,"<function name>") instead.

Pete

 

Posted
22 minutes ago, Pete Dowson said:

Events can only occur when the Lua program relinquishes control, so therefore waiting for the next event.

AH! That's what I didn't understand. Thank you, Pete! I will try what you have suggested.

Best,

Al

EDIT: Now works as expected -- many thanks again!

Posted

The standard ipc.ask() function does not return an input until a Return character is detected. With this in mind, I have been trying to rewrite John's askTest.lua script in the form of a function so it can be called from a main program when needed. What I can't figure out is how to prevent the return from the askTest() function back to the main calling program until after the Return key has been detected thus indicating the input process has been completed. 

Thanks for any suggestions,

Al

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.