Jump to content

Local variables, functions and scope


Pizzicato

Recommended Posts

Hey guys,

 

 

I've got a couple of pretty naive and basic questions about Lua scripting in DCS. As it stands, I'm loading a Lua script named missionConfig.lua that defines some mission-specific constants. I then have a script named aircraftSpawner.lua that spawns flights of aircraft based on the constants defined in the missionConfig.lua.

 

 

For the sake of the discussion, one of the missionConfig.lua constants that is referenced by aircraftSpawner.lua is a string named alertLevel.

 

 

So the questions/issues I have are:

 

 

1. The alertLevel constant in missionConfig.lua is defined as local alertLevel = "HIGH", but I can still access it in aircraftSpawner.lua with trigger.action.outText(alertLevel, 5, false). It seems wrong to me that I can reference a local variable from one script in a different script. What am I missing?

 

 

2. I'd like to use a C#-like Get method/function in the missionConfig.lua (e.g. function getAlertLevel() return alertLevel end), but I can't figure out how to create that type of static method and make it accessible to the aircraftSpawner.lua

 

 

Any help or advice on these two issues would be much appreciated.

 

 

 

Thanks in advance.

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

1. The alertLevel constant in missionConfig.lua is defined as local alertLevel = "HIGH", but I can still access it in aircraftSpawner.lua with trigger.action.outText(alertLevel, 5, false). It seems wrong to me that I can reference a local variable from one script in a different script. What am I missing?

 

That's weird :huh:

 

2. I'd like to use a C#-like Get method/function in the missionConfig.lua (e.g. function getAlertLevel() return alertLevel end), but I can't figure out how to create that type of static method and make it accessible to the aircraftSpawner.lua

 

I think it's just a matter of including this in missionConfig.lua

 

local alertLevel = "HIGH"

function getAlertLevel() 

  return alertLevel 
end

 

 

As long as missionConfig.lua is loaded in the mission, I think you should be able to call getAlertLevel() from any other script and get the string value as return

getAlertLevel()

 

I don't see why it shouldn't work... as I understand it, this is what frameworks like MIST and MOOSE do all the time.

Link to comment
Share on other sites

Thanks Hardcard.

 

Turns out I was falling foul of the old "ME-not-updating-the-script-files" issue. My scripts were actually right, but it was consistently running outdated versions. It was driving me insane.

 

Thanks for your help.:thumbup:

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...