Jump to content
The simFlight Network Forums

Trying to fix some problems in my Lua script...


Recommended Posts

Hello, 

I have a few problems running certain Lua code in FSUIPC 3.9.

I have been working with the log files and starting to get the hang of understanding what is going on.  I appreciate all the help so far and the push to get me to where I am now; I finally see some light at the end of the tunnel. 

The first problem I have rooted out and am unable to solve for the moment on my own is that I am using the event.key() function to pull controls and run functions inside my code.  event.key(k,o,"'function'") seems to be working well (and therefore "keydown" registers properly).  However if I'm looking for event.key(k,o,2,"'function'") - ie: key up - does not seem to return a result. 

To test this I literally used:

event.key(k,o,"keyDown")

event.key(k,o,2,"keyUp")

where:

function keyDown()

ipc.lineDisplay("Key Down")

end

《and》

function keyUp()

ipc.lineDisplay("Key Up")

end

 

Even omitting the 'down' function, I don't get any results from the 'up' function.  Perhaps I'm missing something somewhere?

This is to run the "Triple Use" code, however I'm setting ipc flags to track Key up and down because the example Lua file uses a joystick test function (that I do not believe, has an equivalent "key" counterpart... not that I have found).  According to the documentation this method should work. 

 

Another issue I found was with trying to get COM and NAV offset values.   For example ipc.readUW(0x311A), If I use:

comDisp = "1" .. ipc.readUW(311A)

ipc.lineDisplay(comDisp)

OR

comDisp = "1" .. string.sub(ipc.readUW(0x311A), 3)

ipc.lineDisplay(comDisp)

I get results... but I cannot fathom how to interpret the results, the numbers I get on screen or something like 10007, or 1961.  I tried converting these from hexadecimal to decimal but even that doesn't give me a "BCDE" frequency.  So I'm not sure what step I'm missing; same happens with Com1, and Nav frequencies. 

 

A few minor issues I will hope to clean up, are, FSUIPC's log reports "... not defined, sent to FS"; but it still runs in my Lua script properly.   Do you have to define keys (and joystick buttons for that matter... but mostly keys), in both fsuipc and Lua?  If so do you just assign the key press to "Lua script" general?  Or "Lua set"?  Or one of the other options?  Do you need to specify an offset or parameter?!  I'm not clear on all that but it seems like there's a lot of wasted pushing around that I hope to clean up. 

Another thing; I haven't been able to get SPAD working... but I did notice during my COM display tests,  as mentioned above, when I use my rotaries to adjust the COM frequency, while calling for the display to update there is a severe bottleneck; however it's not the rotary or display independently (it's mostly likely the circular reference in my code); if I adjust the COM frequency with the Saitek the display is instant, the rotary works well for other commands and is instant; but the code together has an issue I'll have to look at better. 

 

The login in fsuipc is actually very good; even helps with Lua code issues just like a terminal; thank you ever so much for this program!

Link to comment
Share on other sites

9 hours ago, Iceking007 said:

To test this I literally used:

event.key(k,o,"keyDown")

event.key(k,o,2,"keyUp")

where:

function keyDown()

ipc.lineDisplay("Key Down")

end

《and》

function keyUp()

ipc.lineDisplay("Key Up")

end

 

Even omitting the 'down' function, I don't get any results from the 'up' function.  Perhaps I'm missing something somewhere?

That looks like it should be ok (except it looks like you are using 'o' instead of '0' for the shifts parameter....) but it is always better to attach your full lua script so that I can check it. Rather than using ipc.linedisplay, its easier to use ipc.log and keep the logging console open when debugging lua scripts. You can then also use the log Lua Plugins function to see what is happening when debugging lua scripts.
And always better to follow the documentation, and so your handling functions should be of the form:
    function keyDown(keycode, shifts, downup)
    ...
    end

 

9 hours ago, Iceking007 said:

If I use:

comDisp = "1" .. ipc.readUW(311A)

This is not correct - you need to use either 0x311A or "311A" 

9 hours ago, Iceking007 said:

comDisp = "1" .. string.sub(ipc.readUW(0x311A), 3)

This is also incorrect - use the string.format function to get a hex representation of the string and then prepend the 1 - something like
     comDisp = string.format("1%04x", ipc.readUW(0x311A))

9 hours ago, Iceking007 said:

FSUIPC's log reports "... not defined, sent to FS"; but it still runs in my Lua script properly.   Do you have to define keys (and joystick buttons for that matter... but mostly keys), in both fsuipc and Lua? 

Not sure what you mean by this, but its really up to you. You can have an assignment to a key/button in FSUIPC and an event on the same key/button if you like, although that is a a strange thing to do (but can sometimes be required). You would usually either use an FSUIPC assignment or a lua event-based script, not both.

9 hours ago, Iceking007 said:

If so do you just assign the key press to "Lua script" general?  Or "Lua set"?  Or one of the other options?  Do you need to specify an offset or parameter?!  I'm not clear on all that but it seems like there's a lot of wasted pushing around that I hope to clean up. 

If you are controlling via lua, you don't need any assignments. You should auto-start event-based luas from the FSUIPC ini [Auto] or profile [Auto.xxxx] sections.

9 hours ago, Iceking007 said:

Another thing; I haven't been able to get SPAD working... but I did notice during my COM display tests,  as mentioned above, when I use my rotaries to adjust the COM frequency, while calling for the display to update there is a severe bottleneck; however it's not the rotary or display independently (it's mostly likely the circular reference in my code); if I adjust the COM frequency with the Saitek the display is instant, the rotary works well for other commands and is instant; but the code together has an issue I'll have to look at better. 

Not sure what you are trying to say here, but I do not support SPAD.

John

Edited by John Dowson
Noticed using o instead of 0 for shift parameter
Link to comment
Share on other sites

Also, maybe try just one event function, e.g

function keyUpDown(keycode, shifts, downup)
    if downup == 1 then
        ipc.lineDisplay("Key Down")
    elseif downup == 0 then
        ipc.lineDisplay("Key Up")
    end
end

event.key(k,0,3,"keyUpDown")

And if you don't want key events handled in lua to also be forwarded to the FS, use the LuaTrapKeyEvent ini parameter:

Quote
Note that, by default, such key events are not trapped and are passed on to the FS. If you would like to prevent them from being forwarded, add the ini parameter LuaTrapKeyEvent (=Yes) to the [General] section of your ini file.

John

Link to comment
Share on other sites

Okay, that's excellent, thank you for all of that information.

- yes the 311A was just a typo... I am using 0x311A in all instances as per documentation; I saw the "311A" reference as well, but don't use it. 

Also instead of k, o as per above, I'm using variables that are assignments to my key; it's working because it's the same code throughout. 

 

I'll try your downup grab, hopefully that works out better; and the logging could be useful.

 

Thank you, have a good one. 

Link to comment
Share on other sites

So for reference, the string.format("%x") returns a hex value; I altered this to %d in order to return a decimal value... or at least it works in a Lua terminal, I have yet to test it on the simulator.  I was even able to get the decimal in there, part of the code In using is below.

local comVal = "\t " .. string.sub(string.format("1%d", Com1Actv), 1, 3) .. "." .. string.sub(string.format("1%d", Com1Actv), 4)

 

I have been listing my Lua code in the Auto section as well,  FYI, but thank you for that tidbit.

 

I'm curious, could you explain the function "..."(#, #, "downup")?

Is downup a variable fsuipc sends?  I'm assuming these are just (unclassified) variables,  in that I could use: function "..."(apple, peach, spaghetti) and it would still work, or would I break things?

[obviously that's a ridiculous example, but I have seen so many variations of joy vs j, downup vs du]

And what is the specific reason to specifying or calling/ pushing a variable?  Won't all functions see all variables unless they are local?

 

Thank you; not enough time to completely work on all of these changes, hopefully I can get this working this weekend!

Link to comment
Share on other sites

6 hours ago, Iceking007 said:

So for reference, the string.format("%x") returns a hex value;

Exactly - you need the hex value as offset 0x311A is a BCD value - Binary Coded Decimal. So, as the example given in the offset status document, a value of 0x2345 would indicate a frequency of 123.45. If you use the decimal value, you will get the wrong frequency. If you want the frequency as a decimal, use offset 0x05CC instead.

6 hours ago, Iceking007 said:

Is downup a variable fsuipc sends?  I'm assuming these are just (unclassified) variables,  in that I could use: function "..."(apple, peach, spaghetti) and it would still work, or would I break things?

It is a value that will be stored in the variable. If you use "(apple, peach, spaghetti)", the downup value will be stored in the variable 'spaghetti'.

6 hours ago, Iceking007 said:

And what is the specific reason to specifying or calling/ pushing a variable?  Won't all functions see all variables unless they are local?

Always better to scope variables. Functions have parameters that are passed to them. How would you know the value otherwise? You could declare a 'downup' variable in the script, but it won't have the downup value needed when the function is called/triggered (unless you declare that again as  the correct function parameter).

John

Link to comment
Share on other sites

Hi John, 

I'll have to try those out in the Sim to see what I'm actually getting; I don't think there's much extra processing to convert the hex?!  But I certainly was not aware there was also a decimal registry; I didn't seem to find it in the offset document but I will look that up as well. 

 

So with the keys and buttons, then I'm assuming fsuipc just automatically assigns a down/ up (1/0) bit to every input and that is 'catalogued' in the third "slot" ie(1, 2, 3)?

 

I haven't added conditions to any of my functions, they seem to be working, save for this issue.  However if what I'm thinking above is correct... then I'm thinking I could bypass the downup function and just pass that indicator bit onto my actually function and qualify it with a simple if statement; less code I figure that way, as I don't need a discreet return of this state. 

 

Thank you. 

Link to comment
Share on other sites

6 minutes ago, Iceking007 said:

But I certainly was not aware there was also a decimal registry; I didn't seem to find it in the offset document but I will look that up as well. 

May not be there in FSUIPC3 - best to check.

6 minutes ago, Iceking007 said:

So with the keys and buttons, then I'm assuming fsuipc just automatically assigns a down/ up (1/0) bit to every input and that is 'catalogued' in the third "slot" ie(1, 2, 3)?

This really isn't the place for programming basics... The values are passed to the function via its parameters. The third parameter contains the downup value (hance this is why we call the variable that holds this downup) as an integer (not a bit), with possible values described in the documentation.

10 minutes ago, Iceking007 said:

However if what I'm thinking above is correct... then I'm thinking I could bypass the downup function and just pass that indicator bit onto my actually function and qualify it with a simple if statement; less code I figure that way, as I don't need a discreet return of this state. 

Not  sure what this means  - 'bypass the downup function'? there is no 'downup function'...
And event handling functions don't return anything.
Did you not see my example above:

On 3/16/2022 at 12:40 PM, John Dowson said:
function keyUpDown(keycode, shifts, downup)
    if downup == 1 then
        ipc.lineDisplay("Key Down")
    elseif downup == 0 then
        ipc.lineDisplay("Key Up")
    end
end

event.key(k,0,3,"keyUpDown")

I suggest that you look at some of the example lua scripts provided, or check out some of the user contributed lua scripts (see the User Contributions sub-forum).

John

Link to comment
Share on other sites

Okay I appreciating all of that. 

 

Yes by skipping the downup function I was referring to your keyUpDown(); I should be able to just use that within my current function rather than shooting another one. 

 

I'll get it sorted out, thank you. 

Link to comment
Share on other sites

Yes, better to use one function rather than two. However, two (or multiple) events on a key or button shouldn't be a problem really, but I cannot confirm that for FSUIPC3 - that may be restricted to a single event per keysytoke/button (and per lua thread).

John

Link to comment
Share on other sites

So I completely misunderstood the hex BCD reference, that is until you spelled it out for me of course.  I apologize for my stupidity; much to learn I have. 

 

I believe I've made all the corrective changes to my code now, I'm hoping to test it sometime in the next week.  With any luck it should all work. 

 

I'll try not to pester you with my learning; I have spent a lot of time (most of my time as of late in fact) learning Lua, and reading all the documents, and going through other Lua programs both related and unrelated to fs.  But I'm sure you are aware, these skills cannot be mastered in two weeks. 

 

Thank you for your guidance; IF I get my code working as intended I shall post it online to share with others.

Link to comment
Share on other sites

Re: offset 05CC appears to be "The viewpoint Bank" according to my documentation.

Are these offsets an fsuipc thing?!  I was under the impression they were fs memory locations; but if so then they would not change with fsuipc updates, only with fs versions (ie fs9, fs10).

Link to comment
Share on other sites

4 hours ago, Iceking007 said:

Re: offset 05CC appears to be "The viewpoint Bank" according to my documentation.

Ok, but that is strange. Usually once an offset is allocated to a simvar (or sim event for writes) it doesm't generally change. \in the documentation I have, viewpoint piyvh, bank and heading are at offset 0x83D4 and marked as 'FS2004 only' (i.e. FSUIPC3!).

4 hours ago, Iceking007 said:

Are these offsets an fsuipc thing?

Yes. They are offset addresses into an FSUIPC byte array.

4 hours ago, Iceking007 said:

I was under the impression they were fs memory locations; but if so then they would not change with fsuipc updates, only with fs versions (ie fs9, fs10).

No. They are populated from FS memory locations in earlier version of FSUIPC (i.e. hacked) , but from FSUIPC4 onwards they are populated using the SimConnect SDK.
Once an offset is allocated for a particular simvar or function, we try to keep that offset allocated for the same simvar/function across all versions of FSUIPC, although this sometimes isn't possible.

Cheers,

John

Link to comment
Share on other sites

  • 2 weeks later...

Hi John,

Well... it seems no matter what I do I am unable to get a key up reaction.  Is there a quick method you would suggest to viewing the key state changes?  It seems to me that the key up does occur somewhere in the chain as my key downs are tracked without fail; I need to be able to verify if the key release (key up after a precursor key down) is being seen in Lua and how it's being interpreted.  I'm loosing a lot of wind in my sail with this whole fiasco, I sincerely appreciate all the assistance, it's just I was expecting to take less than a week on this and now it's been a couple months.

I just recently purchased a used pc, and so I am slowly changing over to a Windows 7 64bit build for my fs.  I should be then able to run the software, but I'm not giving up on this coding project, I forsee the ability to implement it, and I don't like to stop just because something is broken that should work.  The other problem with going away from Windows XP is that some software I want to run is only XP compatible, I'm hoping I can still run it on W7 in compatibility mode.

To add insult to injury, as if I don't have enough on my mind, I just recently lost my job... and so now in this uncertain economy I need to get that sorted ASAP; the bills are already piling up!

I miss flying, I'm hoping I can get back up into the air soon!

I didn't have much luck with event.keypress(#,#,2,""), but if I use:

event.keypress(#,#,kd,"")

and then:

function ""(#,#,kd)

if kd == 1 then

ipc.display("Key is Down")

else

if kd == 0 then

ipc.display("Key is Up")

else

ipc.display("Key is Other")

end

end

That's simple and should work?

 

- you're probably going to poke all sorts of holes in my code, but at this point I just don't care... I haven't been sleeping very well lately and now I'm up in the middle of the night typing this out without much though, so I guess I deserve it.  Funny how we all get what we deserve.

 

Thank you for your time and your patience, I hope to one day not be a burden!

Link to comment
Share on other sites

1 hour ago, Iceking007 said:

Is there a quick method you would suggest to viewing the key state changes?

That is what Button & Key logging does.

1 hour ago, Iceking007 said:

I didn't have much luck with event.keypress(#,#,2,""), but if I use:

event.keypress(#,#,kd,"")

and then:

function ""(#,#,kd)

if kd == 1 then

ipc.display("Key is Down")

else

if kd == 0 then

ipc.display("Key is Up")

else

ipc.display("Key is Other")

end

end

That's simple and should work?

No. The function is event.key, not event.keypress, and why are you using # for parameters and an empty string for the function name? Also, the downup paremeter you are using is '2', which is for releases only. Please see the documentation, and always attach your lua files so that I can see EXACTLY what they are doing, rather than posting extracts.

Why don't you just try a simple test like the one I provided?

On 3/17/2022 at 3:00 PM, John Dowson said:
function keyUpDown(keycode, shifts, downup)
    if downup == 1 then
        ipc.log("Key Down")
    elseif downup == 0 then
        ipc.log("Key Up")
    end
end

event.key(k,0,3,"keyUpDown")

Just replace k with the keycode of the key you want to use (or define and set it before it is used.
Why are you doing this anyway? You can assign to key presses and releases in the FSUIPC UI. You only need to use lua if you want to do something not possible with standard assignments.

Also, if using lua, make sure the lua script is running. For event based luas, you would usually start them from an [Auto] pr [Auto.ccc] profile section, but for development purposes toy may want to assign a button or key to start/kill the lua script.

John

Link to comment
Share on other sites

This is my file so far...

 

It does run in fs.  The # and "" as used above are just place holders, and obviously just forgot it was only event.key instead of event.keypress; I cannot memorize everything you know.

 

Yes, the 2 was supposed to track the key up; if you recall I was using that earlier and it was not working, then you suggested just tracking the key up/down variable; as you will see, that is what I am using now; however tracking it both as 1 or 0 in my timer code doesn't yield the results I'm after.

 

FYI: As a side note, I'm planning on separating my 'display' code into a separate Lua file, it has some bugs currently, so please ignore it.

 

Please don't laugh at my work, remember that I am not a programmer or I wouldn't even be here in the first place.

 

Link to comment
Share on other sites

19 minutes ago, Iceking007 said:

It does run in fs.  The # and "" as used above are just place holders, and obviously just forgot it was only event.key instead of event.keypress; I cannot memorize everything you know.

That is why you should attach your scripts...

22 minutes ago, Iceking007 said:

Please don't laugh at my work, remember that I am not a programmer or I wouldn't even be here in the first place.

I'm not going to laugh, i'm not even going to look... I haven't got time to look at such a lengthy lua script. I am trying to help you recognise key presses/releases, nor debug such a lengthy script...

You keep saying that you are having issues with keypresses or releases not being recognised, so why don't you just try a simple script to test this, as I keep asking.
Try running the following lua script and tell me what you see logged when you press the k, l and m keys:

function keyUpDown(keycode, shifts, downup)
    if downup == 1 then
        ipc.log(keycode .. "Key Down")
    elseif downup == 0 then
        ipc.log(keycode .. "Key Up")
    end
end
k = 0x4B -- k key
l = 0x4C -- l key
m = 0x4D -- m key
event.key(k,0,1,"keyUpDown")
event.key(l,0,2,"keyUpDown")
event.key(m,0,3,"keyUpDown")

When you run that and then press:
   - the k key, you should see the key down logged
   - the l key, you should see the key up logged
   - the m key, you should see both the key down and the key up logged

If that is working for you then your issues is with your script. Try lua debug logging to see what is going on.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • 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.