Jump to content


Photo

LUA: repeated evemt.key action and plane recognition


  • Please log in to reply
3 replies to this topic

#1 qqwertz

qqwertz

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationNova Scotia, Canada

Posted 26 June 2012 - 10:09 AM

Hi,

I am new to LUA and have two rather basic questions.

1) I am trying to set up a function that is called when Shift-H is pressed, i.e., something like

event.key(72, 1, "functionName")

This works fine when I call it the first time, but it doesn't call the function when I press Shift-H again. Only if I hold Shift-H for a while and then press Shift-H once again the function will be called again.
I also tried to set a downup parameter but received the same result. Is there any way to avoid having to hold Shift-H ?

2) I want to program a function that recognizes the airplane in use. Currently I am using ipc.getLvarId
and test if a local variable that can be associated with a specific plane is not nil. For instance,
I can recognize the PMDG JS 4100 through

if ipc.getLvarId("ConspicLight") ~= nil

This works but is rather complicated because I first have to find a variable specific for that plane. Is there a way to read the airplane name in LUA?

Thanks,
Peter
  • 0

#2 Pete Dowson

Pete Dowson

    Advanced Member

  • Moderators
  • 27,283 posts
  • LocationNear Stoke-on-Trent, UK

Posted 26 June 2012 - 10:33 AM

This works fine when I call it the first time, but it doesn't call the function when I press Shift-H again.


This is a little bug which was discovered, reported and fixed a few weeks ago. Please go to the Download Links subforum and download the latest version of FSUIPC3 or FSUIPC4, whichever it is you are using. (It is always best to do this before asking about a problem, just in case. There is a lst of changes included with each update).

This works but is rather complicated because I first have to find a variable specific for that plane. Is there a way to read the airplane name in LUA?


Of course! You can read any FSUIPC offset in Lua. You can set an event for a change in the aircraft name offset, and so receive the name in a string automatically in the function it calls, or an event.sim for "AIRCRAFTCHANGE" and read the aircraft name string in the function.

All FSUIPC offsets are listed in the offset lists in the FSUIPC SDK. Just search on "name" or "aircraft". I don't remember all the offsets myself and would need to do this in any case, but I think it's a string at offset 0x3D00.

Regards
Pete
  • 0

#3 Gypsy Baron

Gypsy Baron

    Advanced Member

  • Members
  • PipPipPip
  • 177 posts
  • LocationDaly Ciry, California

Posted 26 June 2012 - 04:22 PM

Poster Peter,

Pete's memory is spot on. 0x3D00.

Here is a snippet of a Lua script I wrote some time ago that uses that offset and another related offset. Perhaps it will help you.

-- Offsets 0x3D00 and 0x11D4 are used to chech to see if
-- the correct aircraft is selected and the flight is loaded

-- Offset 0x3D00 contains the ASCII name of the current aircraft

-- Offset 0x11D4 will be 0 when the flight is not loaded or a MENU
-- is opened, otherwise it will be 0x0000FADE

function Do_Spit(offset, PFlag)

-- Initialize a variable for use in sending the "ACTIVE" message

  if x == nil then x = 0

  end

-- The next section of code will check to see if the aircaft is a Spitfire Mk I or II
-- We check the first 13 ASCII characters of the variable holding the current aircraft name
-- bypass the read and write sections if the plane has changed
-- NOTE: Make sure FSUIPC.ini has "ShortAircraftNameOk=Yes" declared

  if ipc.readSTR(0x3D00, 13) == "Spitfire Mk I" then

-- Also check to see that the flight is loaded and we are not PAUSED
-- If either condition is not met, bypass the read/write sections of the code

	 if ipc.readUD(0x11D4) > 0 and ipc.readSW(0x0264) == 0 then

-- If This is a Spit and the flight is loaded and we are not paused, Read the L:Variables

	   RPM = ipc.readLvar("L:Eng1_RPMGauge")
	   OIL_PRESS = ipc.readLvar("L:Eng1_OilPressureGauge")
	   OIL_TEMP = ipc.readLvar("L:Eng1_OilTempGauge")
	   BOOST = ipc.readLvar("L:Eng1_MPGauge")
	   RAD_TEMP = ipc.readLvar("L:Eng1_WaterTempGauge")
	   FUEL_PRESS = ipc.readLvar("L:Eng1_FuelPressureGauge")
	   LEFT_BRAKE = ipc.readLvar("L:LeftBrakePressureGauge")
	   RIGHT_BRAKE = ipc.readLvar("L:RightBrakePressureGauge")
	   Top_Needle = ipc.readLvar("L:FuelGauge1")
	   Bot_Needle = ipc.readLvar("L:FuelGauge2")

-- Scale a few to get the tenths portion into the integer variable

	   RPM = RPM * 10
	   OIL_PRESS = OIL_PRESS * 10
	   OIL_TEMP = OIL_TEMP * 10
	   BOOST = BOOST * 10
       RAD_TEMP = RAD_TEMP * 10
	   FUEL_PRESS = FUEL_PRESS * 10
	   Top_Needle = Top_Needle * 10
	   Bot_Needle = Bot_Needle * 10

-- Write the data out to chosen user-defined offsets

	   ipc.writeUW(0x66C0,RPM)
	   ipc.writeUW(0x66C2,OIL_PRESS)
	   ipc.writeSW(0x66C4,OIL_TEMP)
	   ipc.writeSB(0x66C6,BOOST)
       ipc.writeUB(0x66C7,FUEL_PRESS)
	   ipc.writeSW(0x66C8,RAD_TEMP)
	   ipc.writeUB(0x66CA,LEFT_BRAKE)
	   ipc.writeUB(0x66CB,RIGHT_BRAKE)
	   ipc.writeSW(0x66EC,Top_Needle)
	   ipc.writeSW(0x66EE,Bot_Needle)

-- Output a "feel good" message for test purposes periodically

		 if x == 20 then

		  ipc.setdisplay(1000, 25, 100, 50)

		  ipc.display("ACTIVE", 1)

-- Reset the test counter after the message is diplayed
		  x = 1

-- Or increment the counter if it hasn't reached 20 yet

    	 else x = x + 1

		 end

     else

-- If the conditions for doing the reading/writing are not satisfied
-- wait 1 second and check again

    ipc.sleep(1000)

     end
  end
end

Paul
  • 0
Posted Image
Posted Image

#4 qqwertz

qqwertz

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationNova Scotia, Canada

Posted 26 June 2012 - 09:49 PM

Hi Pete and Paul,

wow, you are fast, thanks a lot for the very quick response. I tried it and it worked like a charm.

I just bought the FSUIPC registration last Saturday because I never really understood why I would need it.
Now I can't see how I could live without it, FSUIPC really opens another world. Thanks for this awesome
program, Pete.

Peter
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users


About simFlight - simflight.com - simflight.de - simflight.fr - simflight.nl - simflight.pt - simflight.es - simflight.it - simflight.jp - simrussia.com - simMarket