Jump to content

Tutorial: Introduction to Lua scripting


Recommended Posts

  • 1 month later...

nick = "Yurgon"

msg = "you sir, are a GOD"

 

print (nick .. ", " .. msg)

env.info ("All hail the Yurgon... let him live foreva...(said with a James Earl Jones kind of voice)")

Intel i5-7600K @3,80 (4,9 OC) + Corsair Vengeance 16GB DDR4@3200 XMP 2.0 + Cooler Master Hyper 212 LED + Asus MAXIMUS IX HERO + Thermaltake Chaser A31 Snow Edition

Geforce RTX 2060 6GB

SSD Samsung 850 EVO 500GB + HDD WDC WD500 500GB

Saitek X-52 Pro & Thrustmaster T.Flight Rudder Pedals & TrackIR 5 & Kingston HyperX Alloy FPS

Oh, yeah, also the Sony PlayStation 4 :happy:

Link to comment
Share on other sites

  • 2 months later...

I put the code pasted directly into text text box, the result in dcs.log is:

 

00044.324 ERROR Trigger: can't execute trigger, err:"[string "?"]:1: bad argument #1 to 'loadstring' (string expected, got function)"

 

code:

version = "1.2.16"

 

-- Use two dashes to start a comment. This line will be ignored by the interpreter

opinion = "rocks" -- Oh, and comments can also follow after some code

 

print(sim .. " " .. version .. " " .. opinion)

end

 

I would like to ask what is the reason?

Link to comment
Share on other sites

I put the code pasted directly into text text box, the result in dcs.log is:

 

00044.324 ERROR Trigger: can't execute trigger, err:"[string "?"]:1: bad argument #1 to 'loadstring' (string expected, got function)"

 

code:

version = "1.2.16"

 

-- Use two dashes to start a comment. This line will be ignored by the interpreter

opinion = "rocks" -- Oh, and comments can also follow after some code

 

print(sim .. " " .. version .. " " .. opinion)

end

 

I would like to ask what is the reason?

 

Is that because my lua version is 5.2.4?:(

Link to comment
Share on other sites

@richard008 u forgot the first line...

 

Oh, I make a mistake When the paste is incomplete.

 

But only have code like "print("123456")" is also have the error ----Trigger: can't execute trigger, err:"[string "?"]:1: bad argument #1 to 'loadstring' (string expected, got function)".

 

Even I removed the do script action, this error persists, in DCS 1.2.16.

 

In DCS 1.5.0, this error occurs once every 10 seconds.

 

How to fix it?

Link to comment
Share on other sites

Probably a copy & paste error.

 

If you run it in the Live demo, do you get the same error?

 

AFAIK the code example is just fine and shouldn't break just because you run a certain version. :thumbup:

 

In my PC, lua script is run fine.

23b9cdd34e4cfafd.png

 

I removed the do script action, this error persists, in DCS 1.2.16.

 

In DCS 1.5.0, this error occurs once every 10 seconds.

 

So I think maybe it is a bug.

 

How to fix it?

Link to comment
Share on other sites

Trigger: can't execute trigger, err:"[string "?"]:1: bad argument #1 to 'loadstring' (string expected, got function)"

I've gotten that same error. It generally appears when an older mission is updated and saved in a newer version of DCS. I've never been able to fix it, but it doesn't seem to really hurt anything.

Link to comment
Share on other sites

I've gotten that same error. It generally appears when an older mission is updated and saved in a newer version of DCS. I've never been able to fix it, but it doesn't seem to really hurt anything.

 

Interesting.

 

I just ran one of the missions I used to create the screenshots for the original post (created in 1.2.7), which ran fine in 1.5.2. I then edited and re-saved it in 1.5.2, and it still ran without a problem. Of course this was a very simple mission with just one aircraft and one trigger, but I couldn't find any fundamental problems with it.

 

Edit: Also ran the old mission in 1.2.16, and again, not a problem.


Edited by Yurgon
Link to comment
Share on other sites

Trigger: can't execute trigger, err:"[string "?"]:1: bad argument #1 to 'loadstring' (string expected, got function)"

I've gotten that same error. It generally appears when an older mission is updated and saved in a newer version of DCS. I've never been able to fix it, but it doesn't seem to really hurt anything.

 

So is the case, although it did hurt anything, but it seems that Trigger can not normally use.

All "do file"/"do script" does not work, if it is the problem of lua script,but a simple print ("1234") did indeed that is very puzzled Law enforcement.

Link to comment
Share on other sites

All "do file"/"do script" does not work, if it is the problem of lua script,but a simple print ("1234") did indeed that is very puzzled Law enforcement.

 

Once again, I ran a mission with a simple DO FILE trigger action in 2 different versions of DCS with no problem. If you post your mission, maybe someone can figure out why it's not working. I suggest you open a new thread in the Mission Builders' Corner for that purpose.

Link to comment
Share on other sites

I'm not talking about fairly new DCS versions, I'm talking old, old missions like some originally written for BS1 or FC2. Also, the error occurred in the log file when saving the mission not at run time. This sounds like it's a different problem.

 

A couple of things you might check: 1) If you're using DCS 1.5 or 2.0 make sure you're not using a 'mission start' trigger. It may be fixed now, but it was broken for these versions. 2) If you are copy/pasting from notepad++, try different encoding settings, for example try UTF-8 or UTF-8-BOM.

Link to comment
Share on other sites

  • 1 year later...

Great tutorial! Whenever someone wants to start scritping for DCS, I just link them here.

 

I'd just like to add that Lua Development Tools (LDT) is completely unmatched when it comes to free IDEs for .lua. Notepad++, Atom, etc has context highlighting but LDT also has extensive content assist (intellisense), automatic refactoring etc.

 

I've been using Notepad++ for years and not just lua programming. But once you go LDT there's no turning back :-P

 

screenCodeCompletion.png

Link to comment
Share on other sites

Great tutorial! Whenever someone wants to start scritping for DCS, I just link them here.

 

I'd just like to add that Lua Development Tools (LDT) is completely unmatched when it comes to free IDEs for .lua. Notepad++, Atom, etc has context highlighting but LDT also has extensive content assist (intellisense), automatic refactoring etc.

 

I've been using Notepad++ for years and not just lua programming. But once you go LDT there's no turning back :-P

 

screenCodeCompletion.png

 

Indeed, I can only confirm the feedback from Gromit190.

 

The LDT environment should be standard for mission designers doing lua scripting. The Mission Object Oriented Scripting Environment was built with it.

 

http://flightcontrol-master.github.io/MOOSE/

 

https://www.youtube.com/channel/UCjrA9j5LQoWsG4SpS8i79Qg

 

Fc

[TABLE][sIGPIC][/sIGPIC]|

[/TABLE]

Link to comment
Share on other sites

  • 6 months later...

If you would talk a little bit about certain meanings to attract others like me to the language but in terms for complete newbs (with no programming history or education), for example the meanings of chunks, lexical conventions, functions, compilers and interpreters (you covered this, guys this is what you use to have the niche Lua (Loo-aa) on your device to test the compiled code chunks (compiled as in aggregated, before running?), code (the bits of instructions given to Lua to execute (or not), chunks (differs from comments, the main body of code sans the comments and other unimportant stuff).

 

I want to also add the Lua Digest which has been helpful into wetting my baby feet into programming in Lua for the first time (or any programming for that matter!). You guys can find great help by talking to the devs (or if unavailable their friends directly).

AWAITING ED NEW DAMAGE MODEL IMPLEMENTATION FOR WW2 BIRDS

 

Fat T is above, thin T is below. Long T is faster, Short T is slower. Open triangle is AWACS, closed triangle is your own sensors. Double dash is friendly, Single dash is enemy. Circle is friendly. Strobe is jammer. Strobe to dash is under 35 km. HDD is 7 times range key. Radar to 160 km, IRST to 10 km. Stay low, but never slow.

Link to comment
Share on other sites

  • 4 months later...

Just a heads up, I've been updating the scripting engine wiki a bit.

- Added enumerator data for callsigns and formations

- reorganized enumerator lists to better reflect a tables contents (I got sick of trying to remember if the shorthand values for different tables started at 0 or 1)

- Task pages now categorized by which group types can perform a task. Also added enumerator links for weapons and AI tasking.

 

If there is anything specific that you guys can think of that is still missing just post it here and I'll try to address it.

  • Like 1

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

Just a heads up, I've been updating the scripting engine wiki a bit.

- Added enumerator data for callsigns and formations

- reorganized enumerator lists to better reflect a tables contents (I got sick of trying to remember if the shorthand values for different tables started at 0 or 1)

- Task pages now categorized by which group types can perform a task. Also added enumerator links for weapons and AI tasking.

 

If there is anything specific that you guys can think of that is still missing just post it here and I'll try to address it.

 

Appreciate the work there Grimes. :)

Link to comment
Share on other sites

  • 3 weeks later...

Grimes, what do you use to enumerate this data from within DCS Lua? I know about the showTable function and I've been playing with snippets but I'm curious if you've poked around in the various metatables? How often have we found "new" stuff just "appear" in the scripting API or change/add something on an existing item after a patch?

Link to comment
Share on other sites

I dump the _G table to a file and check to see if anything is new in there. I try to check it once a month or if ED does a major patch. I'll also do it whenever a feature request for a scripting function is said to be fulfilled. Sadly that doesn't happen much as of late, but it does occasionally. Then its down to communicating with ED for all the proper syntax and waiting for it to be merged into live builds to publish documentation.

 

Enumerators show up like this:

    ["trigger"]["smokeColor"] = table: 000000006F20D8B8         {
       ["trigger"]["smokeColor"]["Blue"] = 4,
       ["trigger"]["smokeColor"]["Green"] = 0,
       ["trigger"]["smokeColor"]["Red"] = 1,
       ["trigger"]["smokeColor"]["Orange"] = 3,
       ["trigger"]["smokeColor"]["White"] = 2,
       },

  • Like 1

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

Thanks for info. I can see the nested tables and understand how to walk that recursively.

 

When I see stuff like this, I know where to go to learn more (EventHandlers.lua):

 ["world"]["addEventHandler"] = "function: 000000000029DFF0, defined in (3-5) Scripts/World/EventHandlers.lua 

 

But I'm curious for the these kinds of C functions how I determine what their expected parameters are and the return value. How might I proceed chasing that?

["world"]["getPlayer"] = "function: 000000007481A5A0, C function",

 

One of the reasons for trying to better understand this for me is I want to see if the _G scan result is the same for these ME mission scripts as all the other places where we add scripts, like the slmod and place where the DCS-BIOS pulls it's info. That, plus I'd also like to try to hook other C dlls and explore whether completely novel features could be added to scripts. I've done this kind of work on C# .Net games, which was kind of easy if you know how .Net works and that it leads directly to obtaining "source code" through reflection. With DCS, it's all C dll's and still possible, just a little more difficult. The first part is understanding how the existing Lua scripting engine interfaces with these "C function" 's listed in showTable.

 

...If there's a better existing place to post questions like this, please let me know. If I should start a new thread, just let me know that.

Link to comment
Share on other sites

But I'm curious for the these kinds of C functions how I determine what their expected parameters are and the return value. How might I proceed chasing that?

["world"]["getPlayer"] = "function: 000000007481A5A0, C function",

 

Its literally just from communicating with ED. In the case of me making a feature request I add in what I think the expected return value and input values should be. In the few times ED have just gone and added a scripting function I will try to experiment with it a little on my own or just directly ask for the relevant documentation.

 

DCS-BIOS and slmod have access to different lua environments. The net environment which slmod uses has pretty well maintained documentation by ED in the API/DCS_controlAPI.txt files from the install folder. The export lua environment documentation can be found in Scripts/Export.lua. I haven't messed with it to much, so I can't really answer a whole lot of questions on it. I can say its more geared toward interacting with the players own aircraft for making home cockpits and the like. But it does have access to the greater "world" of objects in the game, which is how tacview uses it to record data.

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

Ok great, that answers a lot. So from just a cursory first look, there are at least 2 undocumented C functions. They might not be actually useful or anything, but I was trying to see if I could find anything novel.

 

getDrawArgumentValue

["Unit"]["getDrawArgumentValue"] = "function: 0000000080418010, C function",

 

cancelChoosingCargo

["Unit"]["parentClass_"]["cancelChoosingCargo"] = "function: 000000007750BBA8, C function",

 

So to confirm, we don't have any current back-and-forth collaborations with ED persons about enhancing Lua scripting capabilities?

 

And yeah, I saw the DCS_controlAPI.txt and Scripts/Export.lua portion. That's when I first had the thought that maybe the Lua environments being exposed to each of those 3 purposes might be different. I suppose without knowing more there might be some value/function in one _G that is not available in another _G. So maybe mission scripts could have increased capabilities by some kind of out-of-band "bridge" between those various standard DCS scripting interfaces.

 

That leads me to what I thought might be useful to make: I suppose right now we don't have a general-use tool that will dump _G recursively and find all the C function parameters and return values? From what you describe Grimes, the current info about that was either given to you to start or after questioning ED, or from your own trial and error. I think I might be able to make us a tool that would be able to pull a complete set of this info on demand by querying the associated dlls for function prototypes. Then we could run it in any of the scripting environments to see if anything new had been added/removed but unannounced.

 

Then there is this kind of possibility to explore the idea even further to add novel scripting features (3 part series):

http://stan-bobovych.com/2017/06/10/hooking-lua-part-1

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

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