Jump to content
Sign in to follow this  
Zach Decou

Multi Function Scripts

Recommended Posts

Looks like my session timed out, so i get to type this all over again. Yay!

I'm not going to type code into my iPhone because it'll take a month with plenty of typos. Screen shots of Komodo will have to suffice...Plus, I'm snowed in and iPhone is my only available Internet.

I an attempting to write a plugin that does more than one thing with variables that are isolated and shouldn't affect other functions in the script. Easier said than done. The low down...


I want to have multiple functions running simultaneously. 

For this example, I have three functions. One is simply text drawn on screen that disappears after 5 seconds. The next is a script that ties angle of incidence request with flap ratio. As flap ratio increases, so does angle of incidence request. The third is a function that shifts the pilots head to the left as though he is leaning under certain high aoa conditions. 


The third function, when turned on, is crosstalking with the other functions in various ways. The order in which the functions appear in the script affect the result in sim. Also, function hierarchy causes problems (of course, this is likely due to my misunderstanding of function hierarchy). For instance, I will have one function main()/end block, containing two if-then statements. Wackiness ensues. 

Here is what appears to be happening. I want incidence tied to flaps. I want view position tied to AOA. In one configuration, AOA affects incidence, while flap ratio doesn't. Not what I want. Or in another configuration, flap ratio sends viewpoint hurtling to the right about 10 feet. These two variables shouldn't be connected in any way, but obviously are. 

This is weird: I tried populating init.lua with 'dofile's and broke up the script into their individual functions as "view.lua" etc. Still have crosstalk. 

I like that lua is 'for the people', but the people might not always be as proficient as the author. I'd like to see a basic tutorial for having multiple functions that stay out of the way of each other. 

And I am still working on the Mu indicator. I've got problems of a different sort that I may be on the verge of solving. Until then, nothing worth posting. Once the above problem is solved, I can use lessons learned to solve other problems. Until then, I'm banging my head against the wall. I envy the folks who just 'get it'. But it is much more gratifying when you spend all night on a simple problem that finally gets solved at 4 in the morning. Of course, this is after everyone else 'got it' weeks ago, thwarting your desire to have a parade. 

Here are my screen shots. The scrips should work with any aircraft that has variable incidence of the main wing. My test aircraft is Hal's Angel. You can download an early version here:






Share this post

Link to post
Share on other sites

If I read your screenshots correct Zach, it looks as though you have 2  functions named "function main()"  is that correct?  The main function is gizmo is a "flight loop" function and runs every flight loop and you should generally only have one.  There are other ways though but I won't get into that here.  In Lua, when you define a function with a specific name (like main) and put in some code...and then do another function with the same name and put in some different code, then the "last function" written is the one that gets run while the code in the previous function (of the same name) does not get run....perhaps I'm misreading your separate images.  

You should name the second function anything you like....say, function bob().  THEN in the main function, you just call the function "bob"  like so:

function bob
do some stuff

function main()

then the code in "bob" will get run while main is running.....well actually, code doesn't really run at the same time....it runs sequentially ;-)..unless you have multi-processors and code written for those.  You'll note that I wrote the function "bob" BEFORE it was used later in the program.  That is good programming practice....isn't it Ben?  at least in Lua?  It's been a few weeks since I messed with Lua and I've honestly forgotten if that's required.

Share this post

Link to post
Share on other sites

Hi, Zach,

Tom is correct.

My suggestion:

Make all of your functions their own. This means you will have a fourth function with function main(). My suggestion is to put the functions you wish to run listed in your main function. For instance:

--create a function that will call others:

if( foo )then

another_function_to run()

"another_function_to_run" will be a function OUTSIDE of your function main().

If you need further clarification of help please don't hesitate to ask!

EDIT: looks like Tom edited to include what I have said here just at the same time I posted. ;)

function main()

Share this post

Link to post
Share on other sites

Lua has the keyword local for variables that might help.

Consider this.

dr_example = xp.getDataref(...)

function foo()
  local x = 1
  xp.setFloat( dr_example, x )

function bar()
  local x = 2
  xp.setFloat( dr_example, x )

--setup a global called x
x = 3

function snafu()
  xp.setFloat( dr_example, x ) --we're using the global variable value here!

function main()
    --dataref is now 1

    --dataref is now 2

    --dataref is now 3

  -- we will now return CPU control to X-Plane, the dataref value that x-plane will see is 3.

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.