MOARdV / AvionicsSystems

MOARdV's Avionics Systems for Kerbal Space Program - a new generation of IVA enhancement.
Other
52 stars 26 forks source link

Lua-question #184

Closed Alexustas closed 6 years ago

Alexustas commented 6 years ago

I plan to make the Event system for automatic switching Standard and Custom Action Groups.

For example, switch AG2 when the altitude is over 65000 m.

So, in order to make the system more flexible and not very difficult to implement, I want to do the following. By using the soft-keys of the CDU I'll set the values of three persistents:


softkey = XXX, fc.SetPersistent("FMS_AG2_EVENT_PARAMETER","fc.Altitude()")
softkey = YYY, fc.SetPersistent("FMS_AG2_EVENT_OPERATOR",">=")
softkey = ZZZ fc.SetPersistent("FMS_AG2_EVENT_VALUE",65000)

then I use the LUa function:


function AG2_EventTrigger()

    local cond = load("if "..fc.GetPersistent("FMS_AG2_EVENT_PARAMETER")..fc.GetPersistent("FMS_AG2_EVENT_OPERATOR")..fc.GetPersistent("FMS_AG2_EVENT_VALUE").." then return true; else return false; end")

    return cond()   

end

This piece of code works fine in Notepad++ tests, but will it work in the KSP/MAS?

MOARdV commented 6 years ago

I do not know. I have never thought about trying it. :)

If it does not work, I am sure I can find a way to make it work.

Although it may be slow if you call load() every FixedUpdate, because that means recompiling that code every time, even if it does not change. Another way to do it may be.

softkey = XXX, AG2_Set_Parameter("fc.Altitude()")
softkey = YYY, AG2_Set_Operator(">=")
softkey = ZZZ, AG2_Set_Value(65000)

I do not know what load() does on error - if it returns nil, then this code will work. Maybe also use startupScript to update AG2 so that it is restored.

-- Initialize cond so that it is never nil
local cond = function 
  return false 
end

function UpdateAG2()
  local newAG = load("if "..fc.GetPersistent("FMS_AG2_EVENT_PARAMETER")..fc.GetPersistent("FMS_AG2_EVENT_OPERATOR")..fc.GetPersistent("FMS_AG2_EVENT_VALUE").." then return true; else return false; end")

  if newAG ~= nil then
    cond = newAG
  end
end

function AG2_Set_Parameter(parameter)
  fc.SetPersistent("FMS_AG2_EVENT_PARAMETER", parameter)

  UpdateAG2()
end

function AG2_Set_Operator(operator)
  fc.SetPersistent("FMS_AG2_EVENT_OPERATOR", operator)

  UpdateAG2()
end

function AG2_Set_Value(value)
  fc.SetPersistent("FMS_AG2_EVENT_VALUE",value)

  UpdateAG2()
end

function AG2_EventTrigger()
  return cond()
end
Alexustas commented 6 years ago

Although it may be slow if you call load() every FixedUpdate, because that means recompiling that code every time, even if it does not change.

my function is just a sketch. The real implementation will be more developed and, I hope, optimized enough

here is the page layout:

bandicam 2018-07-06 21-50-12-217

after adjusting the conditions, the user must save it (1). At this point, a function will be called that compiles the code. then, the user must make this event active (2), and click the "EXEC" button (3) which gives the global permission for the MAS to perform automatic events.

MOARdV commented 6 years ago

Very cool!

To answer your original question: No, right now MAS is configured so load() does not work. However, it is very easy for me to change that, and I am trying right now to test it.

MOARdV commented 6 years ago

Okay, here is how it can work (very simple example):

local function dynamicfunction()
    return 5
end

function MAS_Mfd2_Wrapper()
 return dynamicfunction()
end

function MAS_MFd2_Load()
    local result, errormsg = load("return 3 + 4")
    if result == nil then
-- load returns (function), (error).  If (function) is nil, then (error) has the text of the error.
        fc.LogMessage(errormsg)
    else
        dynamicfunction = result
    end
end

with

softkey XXX, MAS_MFd2_Load()
TEXT
{
  text = <=0=> $&$ MAS_Mfd2_Wrapper()
}

prints "5" until I press the softkey. Then it prints "7".

dynamicfunction is the function that you create at runtime with load. MAS_Mfd2_Wrapper() is a function that can be called by the MFD (or other props).

If you replace MAS_Mfd2_Wrapper instead of dynamicfunction, the text never updates, because the MAS variable does not know that it changed, since the change was inside Lua.

Beta on DropBox so you can experiment with it (all I have tested is the code I showed here).