Tutorial: Introduction to Lua scripting - Page 9 - ED Forums
 


Notices

Reply
 
Thread Tools Display Modes
Old 04-30-2018, 03:47 PM   #81
kakal Ice
Junior Member
 
Join Date: Nov 2013
Location: Lens in France
Posts: 38
Default Hey ok

Hi Yurgon I think of having understood,in fact when I seized the text in the window of script of ME in the second line instead of letting in in the keyboard, I would have of to continue to write the line.
Isn'it?

Thank you now is good in the file dcs.log so i can continue to practice your tuto
Thank you very much
kakal Ice is offline   Reply With Quote
Old 05-02-2018, 05:25 AM   #82
kakal Ice
Junior Member
 
Join Date: Nov 2013
Location: Lens in France
Posts: 38
Default

Up
have a good day
kakal Ice is offline   Reply With Quote
Old 10-27-2018, 08:17 PM   #83
Tiramisu
Junior Member
 
Join Date: Oct 2018
Location: Germany
Posts: 61
Default

I am new to DCS Lua scripting and I wonder how to properly set up an aircraft group in Lua. It is very easy to define a group in the mission editor, but I do not know how to express a group in a Lua code properly.
I have already seen an example for defining a ground group, but now I want to define an aircraft group with certain waypoint, loadouts etc.
So e.g. when I want to define the loadout then I do not know what to write after the equals-sign:
Code:
["payload"] =
Currently I could only make the following code work for a simple aircraft spawn, but it still does not have all the important informations the aircraft would need:
Spoiler:
Code:
-- see https://forums.eagle.ru/showthread.php?t=174318

-- Initialization
if (initialized == nil) then
    initialized = true
    
end

function test()
    local iX, iY = -260000, 700000 
    local groupData = {
        ["visible"] = false,
        ["taskSelected"] = true,
        ["route"] = 
        {
        }, -- end of ["route"]
        ["groupId"] = 2,
        ["tasks"] = 
        {
        }, -- end of ["tasks"]
        ["hidden"] = false,        
        ["units"] = 
        {
            [1] = 
            {
                ["type"] = "Su-33", --other airplane types are stored in ...\DCSWorld\Scripts\Database\planes
                
                ["x"] = iX,
                ["y"] = iY,
                     ["alt"] = 7000, -- altitude in meters
                     ["speed"] = 300,
                ["unitId"] = 2,
                ["skill"] = "Excellent",
                ["name"] = "Russian Air Unit1"                
            }, -- end of [1]
        }, -- end of ["units"]
        ["name"] = "Russian Air Group1",
        ["start_time"] = 0,
        ["task"] = "Intercept",
    } -- end of [1]
        
    coalition.addGroup(country.id.RUSSIA, Group.Category.AIRPLANE, groupData)  
end
test();
It would be great if we could simply export an existing group from the mission editor as a Lua code. Then it would be much more easier for me to learn the Lua expressions.


Btw. I am trying to use this Lua script to spawn a group at a random location within a certain area. I know that you can also simulate random spawns in the mission editor using triggers and flags, but that method is simply to ponderous for complex mission designs.

Last edited by Tiramisu; 10-27-2018 at 08:22 PM.
Tiramisu is offline   Reply With Quote
Old 10-27-2018, 08:31 PM   #84
Yurgon
Campaign Testers
 
Yurgon's Avatar
 
Join Date: May 2010
Location: Germany
Posts: 6,540
Default

Quote:
Originally Posted by Tiramisu View Post
It would be great if we could simply export an existing group from the mission editor as a Lua code. Then it would be much more easier for me to learn the Lua expressions.
That might actually work.

I don't actually know the answer to your question.

But if there's one thing I know about Lua, it's that everything is a table.

So my take on this would be to place a unit via Mission Editor, equip it with some loadout, and then try to get the properties of this as a Lua-object (getUnit("myID")) by traversing it with table-functions. I haven't written any Lua in years, but as I recall it, there should be table iterators like "for" or "foreach" or "each" or "while" or something of that kind. That should give you a way to progress into the object until you reach the payload, and then you know how to set up your own units just via Lua.
__________________
Yurgon is offline   Reply With Quote
Old 10-28-2018, 02:56 AM   #85
Tiramisu
Junior Member
 
Join Date: Oct 2018
Location: Germany
Posts: 61
Default

Now I have tried to follow your suggestion by using the methods Group.getByName and getUnit. With these methods it seems like I could get the data structure of the payload.


So first I created an airplane group called "Premade Group" with the tools of the mission editor and I gave it a proper payload with some missiles like the R-27. Then I used this Lua code to print the payload table in the log:

Spoiler:
Code:
function DCSPrint( Message )    
    env.info( Message, 20)
    --trigger.action.outText( Message, 20)
end

function DisplayTableStructure( Data, iHierarchy )    
    local sLineSpace = ""
    for i = 1,iHierarchy do
        sLineSpace = sLineSpace .. "\t"
    end
    if( type(Data) ~= "table" ) then
        DCSPrint( "##### No table available! #####" )
        return;
    end
    for key, value in pairs( Data ) do
        local sType = type( value )
        if( sType == "table" ) then
            if( type(key) == "string" ) then key = "\"" .. key .. "\"" end
            DCSPrint( sLineSpace .. "[" .. key .. "]" .. " = {" )
            DisplayTableStructure( value, iHierarchy + 1 )
            DCSPrint( sLineSpace .. "}," )
        else
            if( type(key) == "string" ) then key = "\"" .. key .. "\"" end
            if( type(value) == "string" ) then value = "\"" .. value .. "\"" end
            DCSPrint( sLineSpace .. "[" .. key .. "]" .. " = " .. value .. ",")
        end
    end
end

function GetPremadeAmmoLoadout()    
    local PremadeGroup = Group.getByName("Premade Group")
    local iUnitIndex = 1
    local PremadeUnit = PremadeGroup:getUnit( iUnitIndex )
    local AmmoLoadout =  PremadeUnit:getAmmo()
    --print the desired table structure in the DCS log file:
    DCSPrint( "" )
    DCSPrint( "local payload = {" )
    DisplayTableStructure( AmmoLoadout, 1)    
    DCSPrint( "}" )
    --note: PremadeGroup and PremadeUnit also have a table structure, but it only contains their ID
end
GetPremadeAmmoLoadout();

--[[    
    get the results in the DCS log file and delete the unnessecary informations in each line with the regular expression
    "(\d)+\-(\d)+\-(\d)+ (\d)+\:(\d)+\:(\d)+\.(\d)+ INFO    SCRIPTING: "     
 ]]

Based on the resulting payload information for the SU-33 I have created this group:
Spoiler:
Code:
local payload = {
    [1] = {
        ["count"] = 150,
        ["desc"] = {
            ["life"] = 2,
            ["warhead"] = {
                ["explosiveMass"] = 0.39,
                ["type"] = 1,
                ["caliber"] = 30,
                ["mass"] = 0.39,
            },
            ["_origin"] = "",
            ["category"] = 0,
            ["displayName"] = "30mm HE",
            ["typeName"] = "weapons.shells.GSH301_30_HE",
            ["box"] = {
                ["min"] = {
                    ["y"] = -0.12504199147224,
                    ["x"] = -6.61008644104,
                    ["z"] = -0.12113920599222,
                },
                ["max"] = {
                    ["y"] = 0.12504191696644,
                    ["x"] = 2.2344591617584,
                    ["z"] = 0.12113922089338,
                },
            },
        },
    },
    [2] = {
        ["count"] = 2,
        ["desc"] = {
            ["box"] = {
                ["min"] = {
                    ["y"] = -0.27390250563622,
                    ["x"] = -1.0582795143127,
                    ["z"] = -0.19778195023537,
                },
                ["max"] = {
                    ["y"] = 0.10370192676783,
                    ["x"] = 1.8849183320999,
                    ["z"] = 0.19778576493263,
                },
            },
            ["rangeMaxAltMin"] = 4000,
            ["fuseDist"] = 5,
            ["category"] = 1,
            ["guidance"] = 2,
            ["Nmax"] = 45,
            ["rangeMin"] = 300,
            ["altMax"] = 20000,
            ["RCS"] = 0.026200000196695,
            ["displayName"] = "R-73",
            ["altMin"] = -1,
            ["life"] = 2,
            ["missileCategory"] = 1,
            ["warhead"] = {
                ["explosiveMass"] = 13.60000038147,
                ["type"] = 1,
                ["caliber"] = 170,
                ["mass"] = 13.60000038147,
            },
            ["rangeMaxAltMax"] = 12000,
            ["typeName"] = "R-73",
            ["_origin"] = "",
        },
    },
    [3] = {
        ["count"] = 2,
        ["desc"] = {
            ["box"] = {
                ["min"] = {
                    ["y"] = -0.4617146551609,
                    ["x"] = -1.9782328605652,
                    ["z"] = -0.3530935049057,
                },
                ["max"] = {
                    ["y"] = 0.23811063170433,
                    ["x"] = 2.5149307250977,
                    ["z"] = 0.35310152173042,
                },
            },
            ["rangeMaxAltMin"] = 20000,
            ["fuseDist"] = 11,
            ["category"] = 1,
            ["guidance"] = 2,
            ["Nmax"] = 24,
            ["rangeMin"] = 700,
            ["altMax"] = 25000,
            ["RCS"] = 0.061999998986721,
            ["displayName"] = "R-27ET",
            ["altMin"] = 1,
            ["life"] = 2,
            ["missileCategory"] = 1,
            ["warhead"] = {
                ["explosiveMass"] = 39,
                ["type"] = 1,
                ["caliber"] = 260,
                ["mass"] = 39,
            },
            ["rangeMaxAltMax"] = 54000,
            ["typeName"] = "R-27ET",
            ["_origin"] = "",
        },
    },
    [4] = {
        ["count"] = 6,
        ["desc"] = {
            ["box"] = {
                ["min"] = {
                    ["y"] = -0.4617146551609,
                    ["x"] = -1.2691594362259,
                    ["z"] = -0.3530935049057,
                },
                ["max"] = {
                    ["y"] = 0.23811063170433,
                    ["x"] = 2.8275010585785,
                    ["z"] = 0.35310152173042,
                },
            },
            ["rangeMaxAltMin"] = 14000,
            ["fuseDist"] = 11,
            ["category"] = 1,
            ["guidance"] = 4,
            ["Nmax"] = 24,
            ["rangeMin"] = 500,
            ["altMax"] = 27000,
            ["RCS"] = 0.061999998986721,
            ["displayName"] = "R-27ER",
            ["altMin"] = 1,
            ["life"] = 2,
            ["missileCategory"] = 1,
            ["warhead"] = {
                ["explosiveMass"] = 39,
                ["type"] = 1,
                ["caliber"] = 265,
                ["mass"] = 39,
            },
            ["rangeMaxAltMax"] = 60000,
            ["typeName"] = "R-27ER",
            ["_origin"] = "",
        },
    },
}


local route = { 
    points = { 
        [1] = { 
          type = "TURNING_POINT", 
          airdromeId = Airbase.ID, 
          helipadId = Airbase.ID, 
          action = "FLY_OVER_POINT", 
          x = 1000, 
          y = 1000, 
          alt = -1000, 
          alt_type = "RADIO",
          speed = 200, 
          speed_locked = false, 
          --ETA = Time, 
          --ETA_locked = boolean, 
          name = "Some Waypoint name", 
          --task = Task 
        },            
    }            
}



function InitializeGroup()
    local iX, iY = -260000, 700000 
    local groupData = {
        ["visible"] = false,
        ["taskSelected"] = true,
        ["route"] = route,
        ["groupId"] = 2,
        ["tasks"] = 
        {
        }, -- end of ["tasks"]
        ["hidden"] = false,        
        ["units"] = 
        {
            [1] = 
            {
                ["type"] = "Su-33", -- other airplane types are stored in ...\DCSWorld\Scripts\Database\planes                
                ["x"] = iX,
                ["y"] = iY,
                ["alt"] = 7000, -- altitude in meters
                ["alt_type"] =  "RADIO", -- "BARO" or "RADIO" for Above sea level or above ground level
                ["speed"] = 300,
                ["unitId"] = 2,                
                ["name"] = "Russian Air Unit1",                     
                ["payload"] = payload,
                     ["skill"] = "Good",    -- can be "Excellent", "High", "Good", "Average", "Random", "Player"
                     ["heading"] = 3.141, -- in radians
                     ["callsign"] = 100, 
            }, -- end of [1]
        }, -- end of ["units"]
        ["name"] = "Russian Air Group1",
        ["start_time"] = 0,
        ["task"] = "Intercept",
    } -- end of [1]
        
    coalition.addGroup(country.id.RUSSIA, Group.Category.AIRPLANE, groupData)  
end
InitializeGroup();

However, in the simulation I do not see any outer weapons on the spawned SU-33. So the payload is apparently empty. There are no error messages and the airplane itself spawned, but surely there must be something missing.
I hope someone can find a solution for my problems.
Tiramisu is offline   Reply With Quote
Old 10-28-2018, 03:04 PM   #86
Habu_69
Member
 
Join Date: Jul 2011
Location: USA VA
Posts: 545
Default

@Tiramisu: I suggest you would find your scripting goals much easier to achieve using a LUA-based script extension like MOOSE or MiST rather than raw LUA. Example: Set up a group in ME, then in MOOSE use
Code:
GROUP:FindByName( "Your Group" )
Habu_69 is offline   Reply With Quote
Old 10-28-2018, 03:32 PM   #87
Tiramisu
Junior Member
 
Join Date: Oct 2018
Location: Germany
Posts: 61
Default

Ok, I will try MOOSE. It seems to be very helpful for most of my purposes, although it would be even better, if I was able to control all the details of the random spawns myself.

Anyway, thanks for your help, guys!
Tiramisu is offline   Reply With Quote
Old 10-29-2018, 02:14 AM   #88
Habu_69
Member
 
Join Date: Jul 2011
Location: USA VA
Posts: 545
Default

Quote:
Originally Posted by Tiramisu View Post
Ok, I will try MOOSE. It seems to be very helpful for most of my purposes, although it would be even better, if I was able to control all the details of the random spawns myself.

Anyway, thanks for your help, guys!

I think once you gain some familiarity with MOOSE you wll be amazed at the control you can impose on random, and any other, spawn process. The folks who hang out at the MOOSE Discord channel are most generous with their assistance.
Habu_69 is offline   Reply With Quote
Old 10-29-2018, 12:50 PM   #89
Hardcard
Member
 
Hardcard's Avatar
 
Join Date: May 2013
Posts: 185
Default

Quote:
Originally Posted by Tiramisu View Post
Ok, I will try MOOSE. It seems to be very helpful for most of my purposes, although it would be even better, if I was able to control all the details of the random spawns myself.
What details would those be, exactly?
Hardcard is offline   Reply With Quote
Old 10-29-2018, 05:17 PM   #90
Tiramisu
Junior Member
 
Join Date: Oct 2018
Location: Germany
Posts: 61
Default

Quote:
Originally Posted by Hardcard View Post
What details would those be, exactly?

If I got it right, then MOOSE only allows you to randomly spawn groups in certain airports or in predefined trigger zones. I.e. you have to set those circular zones manually, so your airplanes can spawn randomly in those static areas.
I know you can achieve a lot using this method, but in complex missions setting all those static zones and naming them would become somehow cumbersome.

Instead I would prefer to compute the random spawn locations and the random waypoints dynamically depending on the current situation. That would also allow me to prorgam the AI behaviour a little bit and create more strategic depth in the missions.

A very simple example for a dynamic spawning behaviour is spawning an enemy unit around an airplane on a random heading between 90° and 140° on a random distance between 20nm and 30nm. If I wanted to imitate such a spawning behaviour using only the MOOSE in predefined trigger zones, then it would be very hard.
Also if MOOSE is only using uniform probability distributions, then I could not use custom distributions like the Gaussian one.

So it would be really nice, if I could design missions using almost only Lua codes. It would be much more versatile. Still I will give MOOSE a try and maybe I will change my mind.
Cheers!
Tiramisu is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 02:08 PM. vBulletin Skin by ForumMonkeys. Powered by vBulletin®.
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.