pdpo Posted November 30, 2012 Report Posted November 30, 2012 Hi all, i havent gotten much expertise with lua and I am trying to make s small script. I read the flight number, airline string, tail number, and so on (ofsets 3130 and further) and display them on a widefs connected computer with the display.show functionality. So far so good. On the display I see the following for a variable (plane_type) I have defined in the script : Piper But when I add a line in the script like this : if (plane_type == "Piper") then ... end i dont enter the code between the 'then' and 'end'. It seems that somehow the if never returns true. Any idea? Oh, the plane_type is filled in by : plane_type = ipc.readSTR(0x3160,24) Thanks a lot, Peter Depoortere
Pete Dowson Posted November 30, 2012 Report Posted November 30, 2012 You posted your support question to the User Contributions subforum. Ive moved it for you to the correct place, the Support Forum, so it can be answered. On the display I see the following for a variable (plane_type) I have defined in the script : Piper But when I add a line in the script like this : if (plane_type == "Piper") then ... end i dont enter the code between the 'then' and 'end'. It seems that somehow the if never returns true. Any idea? The problem here is that Lua compares strings as binary objects. Your plane_type will be 24 bytes in length including the zero terminator: "Piper" followed by whatever else was in those 24 offset bytes -- a zero then unknown. The zero terminating the string doesn't terminate the comparison, only the very last zero (or whatever) does. So there's no way your literal string "Piper" matches -- you'd need to compare with a 24-byte string with the same contents, which you don't actually know. The only way to compare such strings properly is to use a string library function to force the unknown string to be the same length: if string.sub(plane_type, 1,5) == "Piper" then ... end The string.sub function delivers a string made up of characters 1 to 5 of the original. The original remains intact so you can proceed with other comparisons. Regards Pete
pdpo Posted November 30, 2012 Author Report Posted November 30, 2012 Hi Peter, i'll try it, thanks a lot. Peter Depoortere
pdpo Posted December 5, 2012 Author Report Posted December 5, 2012 Hi Peter, another string trouble I encounter and cant find a solution I explain : i made an array with 9 strings called plane_ident, the first string is "Piper OO-VMC" the rest is "" with ipairs i loop through these 9 strings and I compare the string with a string that I builded up as follows : tail_number = string.sub(ipc.readSTR(0x313C,12),1,6) plane_type = string.sub(ipc.readSTR(0x3160,24),1,5) ident_string = plane_type .. " " .. tail_number the compare I do the following way for i,v in ipairs(plane_ident) do if (string.find(ident_string,v) ~= nil ) then plane_idx = plane_ty end the ident_string is coming from FS and is currently "Piper OO-VMC" but the string.find always returns nil except when I change the string in the array to "Piper OO-" then it works when I change the string in the array to VMC it works too, even OO-VMC works to and it looks like it starts not to work when the string becomes more then 9 chars. Any idea?
Pete Dowson Posted December 6, 2012 Report Posted December 6, 2012 Hi Peter, another string trouble I encounter and cant find a solution I explain : i made an array with 9 strings called plane_ident, the first string is "Piper OO-VMC" the rest is "" with ipairs i loop through these 9 strings and I compare the string with a string that I builded up as follows : tail_number = string.sub(ipc.readSTR(0x313C,12),1,6) plane_type = string.sub(ipc.readSTR(0x3160,24),1,5) ident_string = plane_type .. " " .. tail_number the compare I do the following way for i,v in ipairs(plane_ident) do if (string.find(ident_string,v) ~= nil ) then plane_idx = plane_ty end the ident_string is coming from FS and is currently "Piper OO-VMC" but the string.find always returns nil except when I change the string in the array to "Piper OO-" then it works when I change the string in the array to VMC it works too, even OO-VMC works to and it looks like it starts not to work when the string becomes more then 9 chars. Any idea? No, I can't figure out what you are doing I'm afraid. What does "ipairs" do?I don't think i've ever used that. I don't actually program Lua much, I add Lua support functions to FSUIPC and WideFS. Please use the logging faciklities. The Lua Debug option will not only trace your code but show you the variable vales. Pete
pdpo Posted December 6, 2012 Author Report Posted December 6, 2012 OK, thats a real pitty as you are my only aid line concerning this. I'll see if I can find out how to do the LUA debugging! Never used that. Anyway, for your info : I defined : plane_ident = {} (LUA table object) plane_ident[1] = "Piper OO-VMC" plane_ident[2] = "" plane_ident[3] = "" and so on when you do for i,v in ipairs(plane_ident) do ... end then you get a loop over all the elements in the plane_ident array and on each iteration the value for i is the index in the array (from 1 to 3) and the v get the value of the array element (first "Piper OO-VMC" then "" then "") the problem is mostly the string function. I was expecting if I do string.find("Piper OO-VMC", "Piper OO-VMC") I would get a match but I dont where the first "Piper OO-VMC" string is coming out of the above array and the second one is build up from two fsuipc offsets Anyway i'll continue my search further Thanks Peter
Pete Dowson Posted December 6, 2012 Report Posted December 6, 2012 I was expecting if I do string.find("Piper OO-VMC", "Piper OO-VMC") I would get a match but I dont I looked up "string.find" and it seems to me you have the parameters the wrong way round? The pattern you are searching should be second, the thing in which you are searching should be first. Also the pattern matching is not a simple string operation, it is for patterns defined using quite a complex character-based system.In particular the '-' in the pattern has a special meaning, being one of the "magic characters" (which are ^$()%.[]*+-?). You can make string.find operate with plain strings -- the 4th parameter controls that. I suspect that's what you need. I'm not an expert, but I can look things up! Luckily I purchased the reference manual in print, which makes it slightly easier perhaps than referring to the Lua website. Regards Pete
pdpo Posted December 9, 2012 Author Report Posted December 9, 2012 Hey Pete, just to let you know that you nailed it. The 4th parameter in the string.find to true and voila the find was successfull I also played around with the string functions and got it working much better. Just one question, my program starts as soon as wideclient connects to FS. But how can I tell that this LUA script needs to run with debugging on. I also copied the ipcDebug.lua to my directory but didnt see any diff. Ones the program is ready I will release it here too, it might be usefull for other people. It will start and stop different programs based on the plane that you put in FS. Example : when I start FS with my airbus it will start the programs from project magenta. then when I change to the cessna, it will stop the project magenta and start analogue instruments then when i put the beech baron it will stop the cessna instruments and start instruments for the beechcraft baron... and so on... Greetz Peter
Pete Dowson Posted December 9, 2012 Report Posted December 9, 2012 .Just one question, my program starts as soon as wideclient connects to FS. But how can I tell that this LUA script needs to run with debugging on. I also copied the ipcDebug.lua to my directory but didnt see any diff. Sorry, WideClient doesn't feature any debugging facilities. It isn't really interactive like FSUIPC is. Can you debug on the FS PC? Otherwise you'd need to simply add your own "ipc.log" lines to log progress and values. Regards Pete
pdpo Posted December 10, 2012 Author Report Posted December 10, 2012 Ok Pete, thanks for the answer, currently i am using the wnd.text facility, easier to follow what happens then with the ipc.log as i have to open the logfile and then refresh it to see the latest updates. Thanks Greetz Peter
Pete Dowson Posted December 10, 2012 Report Posted December 10, 2012 thanks for the answer, currently i am using the wnd.text facility, easier to follow what happens then with the ipc.log as i have to open the logfile and then refresh it to see the latest updates. Yes, good idea! Pete
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now