Jump to content

Arithmetic on a table value?


johnv2pt0

Recommended Posts

I am trying to get the coordinates of a spot that is offset from a known position, but I keep getting an error saying you can't perform arithmetic on a field that is a table value.  

 

local client_position = event.initiator:getPosition()

local offset_position = { x = client_position.x + 50, y = 0, z = client_position.z + 50 }

 

This doesn't work, however this (unrelated example) does work:

 

HeadingToSub3 =  mist.utils.getDir({ x = (SubPos3.x - heloPos.x) , y = 0 , z = (SubPos3.z - heloPos.z) })

 

It's got to be simple...but I'm kinda dumb.

 

 

 

Link to comment
Share on other sites

I had tried :getPoint() before and got a "C Stack Overflow" crash...just tried :getPosition().p and receive the same problem.  

 

If you feel like taking a look, here's what I've got.  No worries ~  Thanks

 

-- === CONFIG ===

local Prefix_Table = {"CAS","CAP","AFAC","ROT","heli",} 

local Chief_Offsets = { x = 19 , z = -6 } -- meters x = South(-) to North(+) , z = West(-) to East(+) 

local Chief_Extinguisher = { x = 18 , z = -6 }

-- ==============

local Spawned_Chief_Table = {}

local function Get_Back_Bearing(client_heading)
      
      if client_heading < 210 then
         
         return client_heading + 150
         
      elseif client_heading >= 210 then
             
         return client_heading + 150 - 360
      end
end

ClientSpawnHandler = {}

function ClientSpawnHandler:onEvent(event)
  
  if world.event.S_EVENT_BIRTH == event.id and 
  (event.initiator:getName():find('CAS')) then
     
     local client = event.initiator
     
	 local client_Name = event.initiator:getName()
     
	 local client_Country = event.initiator:getCountry()

     local client_position = event.initiator:getPosition().p
     
     local client_heading = mist.getHeading(client)
     
     local back_heading = Get_Back_Bearing(client_heading)
     
     local back_heading_rad = back_heading * math.pi / 180 -- radians
	 
     local Offset_SpawnCoord = { x = (client_position.x + 19), y = 0, z = (client_position.z - 6) } 
	 
	 local Offset_SpawnCoord_Ext = { x = (client_position.x + 18), y = 0, z = (client_position.z - 6) }


     
local M4_Group_Template = {
                            ["visible"] = false,  
                            ["tasks"] = {}, 
                            ["uncontrollable"] = false,
                            ["task"] = "Ground Nothing",
                            ["taskSelected"] = true,
                            ["groupId"] = nil, 
                            ["hidden"] = Hidden_On_Map ,
                            ["y"] = Offset_SpawnCoord.z , 
                            ["x"] = Offset_SpawnCoord.x ,  
                            ["name"] = client_Name.."_chief group", 
                            ["CountryID"] = client_Country ,
                            ["CategoryID"] = Unit.Category.GROUND_UNIT ,
                            
                            ["route"] = {} ,
                            
                            ["units"] = {
                                         [1] = {
                                                 ["type"] = "VPC-NATO-MAN 1",
                                        
                                                 ["transportable"] = { ["randomTransportable"] = false, }, 
                                        
                                                 ["unitId"] = nil,
                                                 ["livery_id"] = "UN",
                                                 ["skill"] = "Excellent",
                                                 ["y"] = Offset_SpawnCoord.z , 
                                                 ["x"] = Offset_SpawnCoord.x , 
                                                 ["name"] = client_Name.."_chief",
                                                 ["heading"] = back_heading_rad, -- It must be given in radians!
                                                 ["playerCanDrive"] = false,
                                                 ["ClientName"] = client_Name, -- This is a custom parameter I've added, in order to make removal simpler later on 
                                                }, -- end of [1]
										[2] = {
                                                 ["type"] = "VPC-NATO-BALON",
                                        
                                                 ["transportable"] = { ["randomTransportable"] = false, }, 
                                        
                                                 ["unitId"] = nil,
                                                 ["livery_id"] = "UN",
                                                 ["skill"] = "Excellent",
                                                 ["y"] = Offset_SpawnCoord_Ext.z , 
                                                 ["x"] = Offset_SpawnCoord_Ext.x , 
                                                 ["name"] = client_Name.."_chief_ext",
                                                 ["heading"] = back_heading_rad, -- It must be given in radians!
                                                 ["playerCanDrive"] = false,
                                                 ["ClientName"] = client_Name, -- This is a custom parameter I've added, in order to make removal simpler later on 
                                                }, -- end of [2]
                                         }, -- end of ["units"]
                                        
                           } -- end of Unit_Template
      
      coalition.addGroup(M4_Group_Template.CountryID, M4_Group_Template.CategoryID, M4_Group_Template) 
     
      Spawned_Chief_Table[client_Name.."_chief"] = M4_Group_Template
     
      --table.insert(Spawned_Chief_Table, M4_Group_Template) 
      
      for TemplateName , TemplateTable in pairs( Spawned_Chief_Table ) do 
         
          if Unit.getByName(TemplateTable.units[1].name) ~= nil and Unit.getByName(TemplateTable.units[1].ClientName) == nil then
           
             Unit.getByName(TemplateTable.units[1].name):destroy()
             Unit.getByName(TemplateTable.units[2].name):destroy()
			
             Spawned_Chief_Table[TemplateName] = nil
            
          end
      end
  end
  
  if world.event.S_EVENT_ENGINE_STARTUP == event.id and 
  (event.initiator:getName():find('CAS')) then
     
     for TemplateName , TemplateTable in pairs( Spawned_Chief_Table ) do 
         
         if TemplateTable.units[1].ClientName == event.initiator:getName() and Unit.getByName(TemplateTable.units[1].name) ~= nil then
           
			trigger.action.outSoundForGroup( event.initiator:getGroup():getID(), "WHEEL CHOCKS b.ogg")
			
            Unit.getByName(TemplateTable.units[1].name):destroy()
            Unit.getByName(TemplateTable.units[2].name):destroy()
			
            Spawned_Chief_Table[TemplateName] = nil
            
         end
     end
  end
end


world.addEventHandler(ClientSpawnHandler)
env.info("Crew Chief Script Loaded!")

 

Most credit goes to Hardcard for the code, I'm adjusting it to remove the MOOSE requirement because it is unreliable for some reason.  

Link to comment
Share on other sites

I see I made a mistake not referencing the correct subtable within getPosition().p...  Updated code below doesn't produce stack overflow, but now says the ".p" part of the position table is a nil value.  

 

-- === CONFIG ===

local Prefix_Table = {"CAS","CAP","AFAC","ROT","heli",} 

local Chief_Offsets = { x = 19 , z = -6 } -- meters x = South(-) to North(+) , z = West(-) to East(+) 

local Chief_Extinguisher = { x = 18 , z = -6 }

-- ==============

local Spawned_Chief_Table = {}

local function Get_Back_Bearing(client_heading)
      
      if client_heading < 210 then
         
         return client_heading + 150
         
      elseif client_heading >= 210 then
             
         return client_heading + 150 - 360
      end
end

ClientSpawnHandler = {}

function ClientSpawnHandler:onEvent(event)
  
  if world.event.S_EVENT_BIRTH == event.id and 
  (event.initiator:getName():find('CAS')) then
     
     local client = event.initiator
     
	 local client_Name = event.initiator:getName()
     
	 local client_Country = event.initiator:getCountry()

     local client_position = client:getPosition().p
     
     local client_heading = mist.getHeading(client)
     
     local back_heading = Get_Back_Bearing(client_heading)
     
     local back_heading_rad = back_heading * math.pi / 180 -- radians
	 
     local Offset_SpawnCoord = { x = (client_position.p.x + 19), y = 0, z = (client_position.p.z - 6) } 
	 
	 local Offset_SpawnCoord_Ext = { x = (client_position.p.x + 18), y = 0, z = (client_position.p.z - 6) }


     
local M4_Group_Template = {
                            ["visible"] = false,  
                            ["tasks"] = {}, 
                            ["uncontrollable"] = false,
                            ["task"] = "Ground Nothing",
                            ["taskSelected"] = true,
                            ["groupId"] = nil, 
                            ["hidden"] = Hidden_On_Map ,
                            ["y"] = Offset_SpawnCoord.z , 
                            ["x"] = Offset_SpawnCoord.x ,  
                            ["name"] = client_Name.."_chief group", 
                            ["CountryID"] = client_Country ,
                            ["CategoryID"] = Unit.Category.GROUND_UNIT ,
                            
                            ["route"] = {} ,
                            
                            ["units"] = {
                                         [1] = {
                                                 ["type"] = "VPC-NATO-MAN 1",
                                        
                                                 ["transportable"] = { ["randomTransportable"] = false, }, 
                                        
                                                 ["unitId"] = nil,
                                                 ["livery_id"] = "UN",
                                                 ["skill"] = "Excellent",
                                                 ["y"] = Offset_SpawnCoord.z , 
                                                 ["x"] = Offset_SpawnCoord.x , 
                                                 ["name"] = client_Name.."_chief",
                                                 ["heading"] = back_heading_rad, -- It must be given in radians!
                                                 ["playerCanDrive"] = false,
                                                 ["ClientName"] = client_Name, -- This is a custom parameter I've added, in order to make removal simpler later on 
                                                }, -- end of [1]
										[2] = {
                                                 ["type"] = "VPC-NATO-BALON",
                                        
                                                 ["transportable"] = { ["randomTransportable"] = false, }, 
                                        
                                                 ["unitId"] = nil,
                                                 ["livery_id"] = "UN",
                                                 ["skill"] = "Excellent",
                                                 ["y"] = Offset_SpawnCoord_Ext.z , 
                                                 ["x"] = Offset_SpawnCoord_Ext.x , 
                                                 ["name"] = client_Name.."_chief_ext",
                                                 ["heading"] = back_heading_rad, -- It must be given in radians!
                                                 ["playerCanDrive"] = false,
                                                 ["ClientName"] = client_Name, -- This is a custom parameter I've added, in order to make removal simpler later on 
                                                }, -- end of [2]
                                         }, -- end of ["units"]
                                        
                           } -- end of Unit_Template
      
      coalition.addGroup(M4_Group_Template.CountryID, M4_Group_Template.CategoryID, M4_Group_Template) 
     
      Spawned_Chief_Table[client_Name.."_chief"] = M4_Group_Template
     
      --table.insert(Spawned_Chief_Table, M4_Group_Template) 
      
      for TemplateName , TemplateTable in pairs( Spawned_Chief_Table ) do 
         
          if Unit.getByName(TemplateTable.units[1].name) ~= nil and Unit.getByName(TemplateTable.units[1].ClientName) == nil then
           
             Unit.getByName(TemplateTable.units[1].name):destroy()
             Unit.getByName(TemplateTable.units[2].name):destroy()
			
             Spawned_Chief_Table[TemplateName] = nil
            
          end
      end
  end
  
  if world.event.S_EVENT_ENGINE_STARTUP == event.id and 
  (event.initiator:getName():find('CAS')) then
     
     for TemplateName , TemplateTable in pairs( Spawned_Chief_Table ) do 
         
         if TemplateTable.units[1].ClientName == event.initiator:getName() and Unit.getByName(TemplateTable.units[1].name) ~= nil then
           
			trigger.action.outSoundForGroup( event.initiator:getGroup():getID(), "WHEEL CHOCKS b.ogg")
			
            Unit.getByName(TemplateTable.units[1].name):destroy()
            Unit.getByName(TemplateTable.units[2].name):destroy()
			
            Spawned_Chief_Table[TemplateName] = nil
            
         end
     end
  end
end


world.addEventHandler(ClientSpawnHandler)
env.info("Crew Chief Script Loaded!")

 

Link to comment
Share on other sites

Alright sorry about the long wait, took a bit to figure out what was going on but here's what I've come up with:

ClientSpawnHandler = {}
function ClientSpawnHandler:onEvent(event)
  
    if world.event.S_EVENT_BIRTH == event.id and (event.initiator:getName():find('CAS')) then

        local client = event.initiator
        
        local client_Name = event.initiator:getName()

        local client_Country = event.initiator:getCountry()

        local client_position = event.initiator:getPosition().p
        
        local client_heading = mist.getHeading(client)
        
        local back_heading = Get_Back_Bearing(client_heading)
        
        local back_heading_rad = back_heading * math.pi / 180 -- radians
        
        local Offset_SpawnCoord = { x = (client_position.x + 19), y = 0, z = (client_position.z - 6) } 
        
        local Offset_SpawnCoord_Ext = { x = (client_position.x + 18), y = 0, z = (client_position.z - 6) }

        local M4_Group_Category = Group.Category.GROUND

        local M4_Group_Template = {
            ["visible"] = false,
            ["taskSelected"] = true,
            ["route"] = 
            {
            }, -- end of ["route"]
            --["groupId"] = 1,
            ["tasks"] = 
            {
            }, -- end of ["tasks"]
            ["hidden"] = false,
            ["units"] = 
            {
                [1] = 
                {
                    ["type"] = "LAV-25",
                    ["transportable"] = 
                    {
                        ["randomTransportable"] = false,
                    }, -- end of ["transportable"]
                    --["unitId"] = 2,
                    ["skill"] = "Average",
                    ["y"] = Offset_SpawnCoord.z, 
                    ["x"] = Offset_SpawnCoord.x, 
                    ["name"] = "_chief",
                    ["playerCanDrive"] = false,
                    ["heading"] = back_heading_rad, -- It must be given in radians!
                }, -- end of [1]
                [2] = 
                {
                    ["type"] = "LAV-25",
                    ["transportable"] = 
                    {
                        ["randomTransportable"] = false,
                    }, -- end of ["transportable"]
                    --["unitId"] = 2,
                    ["skill"] = "Average",
                    ["y"] = Offset_SpawnCoord_Ext.z, 
                    ["x"] = Offset_SpawnCoord_Ext.x, 
                    ["name"] = "_chief_ext",
                    ["playerCanDrive"] = false,
                    ["heading"] = back_heading_rad, -- It must be given in radians!
                }, -- end of [2]
            }, -- end of ["units"]
            ["y"] = Offset_SpawnCoord.z, 
            ["x"] = Offset_SpawnCoord.x,   
            ["start_time"] = 0,
            ["task"] = "Ground Nothing",
        } -- end of [1]

        M4_Group_Template.name = client_Name.."_chief_group"
        
        coalition.addGroup(client_Country, M4_Group_Category, M4_Group_Template) 
        
        local spawned_group_units = Group.getByName(M4_Group_Template.name):getUnits()

        for _, data in pairs(spawned_group_units) do
            if whatever then
                Unit.getByName(data:getName()):destroy()
                trigger.action.outText(data:getName().." destroyed", 15)
            end
        end
    end
end

A few things to note:
1) I've changed the structure a little bit to match what's being given as an example for addGroup() on the SSE wiki for easier reference. 
2) You need to use Group.Category instead of Unit.Category
3) For whatever reason unknown to me, the stack overflow error comes from concatenating client_Name for the units name.

4) Adding to 3, the group name CAN be concatenated with client_Name, again I have no idea why.
5) You can change the unit type back to what you were using as I don't have the mod for them.

I've included an example way at the bottom of how you could destroy the units after if you wanted to, I didn't dive into that though since the main problem was getting them spawned.
 
Hopefully this might help some!

Link to comment
Share on other sites

  • Recently Browsing   0 members

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