Jump to content

Tutorial: Introduction to Lua scripting


Recommended Posts

  • 6 months later...

Hello, I am trying to get something to work in the ME.

 

function getFuel ()

local number = Unit.getFuel("test")

if number < 0.4 then

trigger.action.outText("returning true", 10)

return true

else

trigger.action.outText("returning false", 10)

return false

end

end

 

I have this code in a Switch waypoint command Condition (LUA PREDICATE)

 

It doesnt give an error but doesnt do anything else

Link to comment
Share on other sites

DCS does not return errors any more for some strange reasons (or only on special occasions). So your code might be faulty even when you have no errors in DCS.

First of all make sure that you define a unit table correctly. E.g.

pUnit = Group.getByName( "Aircraft Group #001" ):getUnit( 1 )

or

pUnit = Unit.getByName( "Test Unit #001" )

In addition you should write

pUnit:getFuel()

instead of

Unit.getFuel("test")

Link to comment
Share on other sites

  • 4 weeks later...

Are there any lib files or something like that in DCS folder that I can import to my project in LDT so I can use autocomplete functions and all that good stuff? Just starting on scripting simple stuff before trying out MOOSE. Have been using hoggit for reference, but if there's something like that in DCS it would be much better.

Link to comment
Share on other sites

Suggest you pose your question in Moose Discord channel where you will find help aplenty. AFAIK (and I am a scripting novice) DCS has no such libraries. I believe the more proficient Moose script writers have developed script modules for creating tables of airfields, aircraft types, etc. that run at mission start.

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...
  • 3 months later...

Hello again! Its the complete n00b again! Yay!

 

I found a couple of LUA courses online that are actually really cheap until midnight tonight (GMT), and I'm wondering if someone can help me decide which one would be best for me to try with a view to being able to create simple scripts for DCS. (Even just understanding other people's scripts would be useful!)

 

Is there any chance somebody could just have a quick glance at the overview for a couple of these and advise me which one you think would be most beneficial?

 

https://www.udemy.com/course/lua-programming-master-the-basics/

https://www.udemy.com/course/lua-programming-become-a-master-of-lua/

https://www.udemy.com/course/lua-programming-course-a-path-to-your-career-in-lua/

https://www.udemy.com/course/luamasterclass/

https://www.udemy.com/course/lua-programming-master-the-fundamentals-for-beginners/

 

There seems to be a huge overlap, which I suppose one should expect, but it makes it hard to know which one to go for. As I said in an earlier post, my last experience with programming was using LOGO about 40 years ago when I was about 10, so I'm going in as a complete newbie. I'm already working my way through a free Python course from that site, and also just picked up another one entitled "LUA's Core Syntax" which I assume will have some overlap on basic principles, but would like to pick up one of these other courses which hopefully will be more relevant to what I need to know.

 

Thanks everyone.


Edited by frostycab
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Just trying to do a simple script. It works on lua demo but in game it gives me the following error.

 

lua :3: unexpected symbol near ','

 

 

do 
local x = trigger.misc.getUserFlag('2')
triggeraction.outText(x .. " points " ..   70)
end

 

also tied 

 

do 
local x = trigger.misc.getUserFlag('2')
triggeraction.outText(x .. " points " .. , 70)
end

 

which gives the the error lua :3: unexpected symbol near ',' both in game and in lua demo

Tried trigger.action as well.

 

Its driving me mad.

 

Any help would be welcome 

 

Thanks

 

Edit 

 Changes I make to my lua script dont take. When I look in the tmp file that dcs uses it completely different.  Even if I change the var x to y in my file the temp version still reads x.

 

Got it working but have to use do script instead of script file which is not ideal


Edited by paulthomas
additional info
Link to comment
Share on other sites

On 3/13/2021 at 7:07 AM, paulthomas said:

 Changes I make to my lua script dont take. When I look in the tmp file that dcs uses it completely different.  Even if I change the var x to y in my file the temp version still reads x.

 

If you make a change to your lua file that's in a do script file event, you need to open the mission, go into the specific trigger, and re-click the button to load the script file again.  Otherwise, it retains what the script file contained from the previous time you loaded it - i.e. it doesn't change the script file within the .mis file when you change the source script.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I work a lot with DCS Mission Editor, and started learning LUA about two weeks ago. As an excercise, I wrote a simple script allowing to call my flight for fuel reports. I found it useful in long range fighter sweeps or caps. To use it in a mission, two triggers have to be created:

 

1. ONCE trigger, with condition TIME MORE(5) and two actions

 - adding a radio item for report call, flag 1 value 1

 - do script with code below:

------- Initialization -------	
player = world.getPlayer()
local playerFlight = Unit.getGroup(player)
planesInFlight = Group.getUnits(playerFlight)

2. REPETITIVE ACTION trigger, with condition FLAG IS TRUE(1) and two actions:

 - do script with code below:

------- Fuel report -------	
for i = 1, #planesInFlight do
  if Unit.isExist(planesInFlight[i]) then
	if Unit.getID(planesInFlight[i]) ~= Unit.getID(player) then
	  trigger.action.outText(Unit.getCallsign(planesInFlight[i]) .. " fuel state: " .. math.floor(100 * Unit.getFuel(planesInFlight[i])) .. "%", 10) 
	end
  end
end	

- set flag 1 back to 0

 

What's the purpose of it? If a plane has full internal tanks, getFuel returns 1. If it has also external tanks, the returned value is above 1. I converted it to percents, it's more readable.

Normally, my wingmen drop tanks during defensive maneuvers, having a missile fired at. It can be too late for them. Of course, I can order them to drop external tanks earlier. But having their fuel state reported, I can order them to do it just as they have 100% or less. This reduces drag and can save some fuel.

I use "Drop ordnance command". Unfortunately, I can't find how to make them drop payload from specific pylons.

Why did I use nested IFs? When I used one if with two expressions connected by AND, the script crashed, I don't know why. It happened after I lost a wingman in combat and call my fuel report.

 

Here is a simple mission I created to test my scripts.

 

02 Scripting tests.miz

  • Like 1
Link to comment
Share on other sites

  • 5 weeks later...

Don't feel ready to sink my head into LUA scripting just yet...but,

 

Your example is very interesting.

 

Thanks for sharing.

 

MOBO/MSI X570-A PRO/PCIe 4. CPU/AMD RYZEN 9 3900X (12 CORE), RAM/DDR4-32 GB (3200MHZ with XMP activated on the MOBO), GPU/EVGA-RTX 3080Ti, HMD/PIMAX Crystal, HOTAS/Thrustmaster Cougar

Link to comment
Share on other sites

  • 2 months later...

I have a script I am trying to do something with but I have little to no experience with Lua at all. The following script works in my mission however once all the tasks requested are completed they cease to continue.

 

I'm looking to make the last chunk (where the warehouse requests are made for the various trips) at the bare minimum loop over and over again but in a best case scenario , to be radomised as to when they are gonna happen , but again still repeating until the mission ends.

 

Using The MOOSE framework. Also just started a Lua course but it's like Japanese to me.

Caucaus Warehousing.lua


Edited by Badass1982
Uploaded Script
Link to comment
Share on other sites

  • 1 month later...
On 9/18/2021 at 3:08 AM, okopanja said:

Nice tutorial.

Is there any official documentation?

I tried several and mostly got dead links

Hoggit wiki is where I try to keep scripting information up to date: https://wiki.hoggitworld.com/view/Simulator_Scripting_Engine_Documentation

  • Like 1
  • Thanks 2

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

  • 1 year later...

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
  • Recently Browsing   0 members

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