Jump to content

Lua tips


Durham
 Share

Recommended Posts

I am trying to develop a tcp/ip mod that takes data from DCS, puts it into a C#/SQL Server environment and then does lots of wonderful things.

 

At present these wonderful things are limited to a message system that will display text in game, a command line that will execute a lua command (which I call LUA injection) and a map of Nevada from Google that will put the units on it and updates their positions in a timed loop.

 

So nothing earth shattering and nothing that hasn't been done before by the community.

 

In doing this, I communicated via PM with Grimes and he opined that all conversations should be in the forums, so I thought I would publish two useful things I have learnt in LUA during this project.

 

Firstly, LUA has no try{} catch{} finally{} statement, so you send the game commands it doesn't like and it will either hang, crash or just ignore them.

 

The solution I found was the pcall() statement.

 

For example, I am converting a vec2 to Latitude and Longitude to give the unit location to google maps.

 

My application calls:-

 

--protected call to convert lat long

function pcallLOtoLL(x, y)

if pcall(LOtoLL, x, y, true) then

else

tcpClient:send("Lat Long error!:\n")

end

end

 

which then calls

--Get Lat Long

function LOtoLL(x , y, send)

--create vec3 with altitude = 0

tblVec3 = {x = x, y = 0, z = y}

--convert to lat/long in decimals

local lat, long = coord.LOtoLL(tblVec3)

--send to server

if send == true then

tcpClient:send("Lat = "..lat.." Long = "..long.."\n")

elseif send == false then

return lat, long

end

end

 

Just as with try catch, we either get a valid return of lat and long or we get an error message inside the pcall() statement with no hang or crash.

 

The second issue I had was with the table object in LUA which is so flexible and loosely typed as to be a real nightmare for a SQL Server guy like me.

 

If you read Grimes' MIST source code, you will see that mist.DBs is actually one table, which contains lots of tables within it - just like a SQL Server database where you would call [dataBaseName].dbo.[tableName]. Now we cannot use SQL on them with joins etc, but that was a big leap forward in understanding for me.

 

I wrote the following in the command prompt to start comprehending LUA tables and if at least one of you finds it helpful, then that is good:-

 

local tblResult = {}

local x=1

local y=1

local z =1

 

--insert into table

for i=1, 10, 1 do

tblResult
= {["xVal"] = x, ["yVal"] = y, ["zVal"] = z}

x = x + 1

y = y + 1

z = z + 10

end

 

--output the index value followed by the zVal

for k, v in pairs(tblResult) do

print(k)

for iK, iV in pairs(v) do

if iK == "zVal" then

print(iK, iV)

end

end

end

 

This shows the table within a table syntax as well as giving a basic idea of the beginnings of a sql query using two in pairs loops (one for the main table, second for the records (which are tables) and then an if statement to output only one of the fields from the records.

 

Hope that this is useful to someone and thanks again to Grimes for getting me going.

 

Kind regards to all

 

Durham

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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