Error Lua Scoring

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
DavidRN
Posts: 91
Joined: Tue Jan 17, 2023 8:41 pm

Error Lua Scoring

Post by DavidRN »

HI There

Getting the error message
ERROR: [string "Console"]:22: unexpected symbol near '<\226>'
at the the following
if string.match(unit.classname, “J%-15 Flying Shark [Su%-33 Copy]”) then points = 10

Any assisstance appreciated.

DB


Code: Select all

local side = "Blue" -- put the side that you are keeping track of scoring here
local currentScore = ScenEdit_GetScore(side) -- get the current score
local points = 0 -- default amount of points to award, set to zero so only explicitly set scores are used
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed
if unit.type == "Weapon" then
return --exit if destroyed unit is really a weapon
end

if -- initial score setup, defaults
unit.type == "Aircraft" then points = 10
elseif 
unit.type == "Ship" then points = 50
elseif 
unit.type == "Submarine" then points = 50
elseif 
unit.type == "Facility" then points = 5
end

--red aircraft – J-15 Flying Shark, red submarine – Type 039C Yuan
--% sign is there because a dash is a special character, the % tells it to not use it as a special character

if string.match(unit.classname, “J%-15 Flying Shark [Su%-33 Copy]”) then points = 10
end
if string.match(unit.classname, “Type 039C Yuan”) then points = 50
end

currentScore = currentScore + points
print(unit.name.. " ("..UnitX().classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)
ScenEdit_SetScore(side, currentScore, unit.name.. " ("..unit.classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

Re: Error Lua Scoring

Post by michaelm75au »

Wouldn't be easier to test the unit.dbid???
Michael
User avatar
blu3s
Posts: 1063
Joined: Fri Jul 08, 2022 9:45 am

Re: Error Lua Scoring

Post by blu3s »

String.match doesnt return true or false, it returns the match on the string or nil if not found. You can read more info here

As mention by michaelm75au is better to use a dbid==integer than a classname with a regex, or if you need a specific action for a specific unit use the GUID of the unit.
DavidRN
Posts: 91
Joined: Tue Jan 17, 2023 8:41 pm

Re: Error Lua Scoring

Post by DavidRN »

HI

Thanks very much for the replies changing to GUID worked a treat (as below).

Have another error .....

if unit.type == "Weapon" then
ERROR: [string "Console"]:5: attempt to index a nil value (local 'unit')

Tried fiddling with the scripting but nothing worked.

Regards

DB



Code: Select all

local side = "Blue" -- put the side that you are keeping track of scoring here
local currentScore = ScenEdit_GetScore(side) -- get the current score
local points = 0 -- default amount of points to award, set to zero so only explicitly set scores are used
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed
if unit.type == "Weapon" then
return --exit if destroyed unit is really a weapon
end

if -- initial score setup, defaults
unit.type == "Aircraft" then points = -10
elseif 
unit.type == "Ship" then points = -50
elseif 
unit.type == "Submarine" then points = -50
elseif 
unit.type == "Facility" then points = -5
end

-- ships – DDG 991 Sejong Daewang [KDX%-3], F 591 Virginio Fasan [FREMM ASW], aircraft – Lynx Mk99A
--% sign is there because a dash is a special character, the % tells it to not use it as a special character

if string.match(unit.classname, "DSWMNJ-0HMO03TMIV0O6") then points = -50
end
if string.match(unit.classname, "DSWMNJ-0HMO0T5T7Q9TH") then points = -50
end
if string.match(unit.classname, "DSWMNJ-0HMO30F0NLA3L") then points = -10
end
if string.match(unit.classname, "DSWMNJ-0HMO30F0NOGE4") then points = -10
end

currentScore = currentScore + points
print(unit.name.. " ("..UnitX().classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)
ScenEdit_SetScore(side, currentScore, unit.name.. " ("..unit.classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)


User avatar
blu3s
Posts: 1063
Joined: Fri Jul 08, 2022 9:45 am

Re: Error Lua Scoring

Post by blu3s »

DavidRN wrote: Mon Jan 30, 2023 10:38 pm HI

Thanks very much for the replies changing to GUID worked a treat (as below).

Have another error .....

if unit.type == "Weapon" then
ERROR: [string "Console"]:5: attempt to index a nil value (local 'unit')

Tried fiddling with the scripting but nothing worked.

Regards

DB
It is necessary to obtain a destroyed unit to have a value in ScenEdit_UnitX() if not, UnitX is nil.

Edit to remember you than string.match doesn't return True or False so your code doesnt work as you expected. Returns the string that matches or nil otherwise
DavidRN
Posts: 91
Joined: Tue Jan 17, 2023 8:41 pm

Re: Error Lua Scoring

Post by DavidRN »

Thanks again,

I found another way of setting the score via a Lua Legion user and I found this one easier to work with and it (finally- Lua scoring) worked for me, the one above I followed ephotopros on YouTube.

Only being playing C:MO for a few weeks now and had never heard of Lua, but wanted to get into it straight away

I assume this ....
if unit.subtype == "2657" then points = - 50 -- FFG

Is the trigger for this (or vice versa?) ....
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed


Code: Select all

--Blue Unit Destroyed
local side = "Blue" -- put the side that you are keeping track of scoring here
local currentScore = ScenEdit_GetScore(side) -- get the current score
local points = 0 -- default amount of points to award, set to zero so only explicitly set scores are used
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed

if unit.type == "Weapon" then
return --exit if destroyed unit is really a weapon
end

if -- initial score setup, defaults
unit.type == "Aircraft" then points = -10
elseif 
unit.type == "Ship" then points = -50
elseif 
unit.type == "Submarine" then points = -50
elseif 
unit.type == "Facility" then points = -5
end

if unit.subtype == "2657" then points = - 50 -- FFG
end      

currentScore = currentScore + points
print(unit.name.. " ("..UnitX().classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)
ScenEdit_SetScore(side, currentScore, unit.name.. " ("..unit.classname.." Sub Type: "..unit.subtype.." ) Destroyed - Points: "..points)
Regards

DB
User avatar
blu3s
Posts: 1063
Joined: Fri Jul 08, 2022 9:45 am

Re: Error Lua Scoring

Post by blu3s »

DavidRN wrote: Tue Jan 31, 2023 6:03 am Thanks again,

I found another way of setting the score via a Lua Legion user and I found this one easier to work with and it (finally- Lua scoring) worked for me, the one above I followed ephotopros on YouTube.

Only being playing C:MO for a few weeks now and had never heard of Lua, but wanted to get into it straight away

I assume this ....
if unit.subtype == "2657" then points = - 50 -- FFG

Is the trigger for this (or vice versa?) ....
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed

...



Regards

DB

I wrote a script in lua that filtered by type and then subtypes / dbid I leave it at the end of the message

Some considerations. When filter, start always for the big groups, as you do filter by type, and inside the if, create the filters necessary.

In this case if you want to filter when a specific FFG is destroyed, use unit.dbid == 2657 but inside the if unit.type=='Ship'.

Unit subtype are strings and as you can se here, have this values for aircraft (and if you scroll down you can see the rest of the subtypes)

Here is the Lua Function, use a ChangeScore in a LuaInit script that is executed when the scen is loaded. Attached too

Code: Select all

function ChangeScore(side,amt,reason)
	local newScore = ScenEdit_GetScore(side) + amt
	ScenEdit_SetScore(side,newScore,reason)
	print (side..' score changed to '..newScore)
	return newScore
end

--UNIT IS DESTROYED script
local unit =  ScenEdit_UnitX()
if unit.type ~= 'Weapon' then
	if unit.type == "Ship" then
		if unit.subtype=="2008" then --CARRIER or use unit.dbid==number of carrier
			ChangeScore('NATO',-25000,unit.name.. ' was lost; the mission is doomed.')
		else
			ChangeScore('NATO',-1000,unit.name.. ' was sunk')
		end
	elseif unit.type == "Submarine" then
		ChangeScore('NATO',-700,unit.name.. ' was sunk')
	elseif unit.type == "Facility" then
		if unit.subtype == "3001" then --BUILDING SURFACE
			ChangeScore('NATO', -250,unit.name.. ' was destroyed')
		elseif unit.subtype == "3005" then
			ChangeScore('NATO',-25000,unit.name.. ' city was destroyed. More than 245.000 casualities; the mission is doomed.')
			ScenEdit_EndScenario()
		else
			ChangeScore('NATO', -100,unit.name.. ' was destroyed')
		end
	elseif unit.type == "Aircraft" then
		if unit.dbid == 1626 then --AWACS
			ChangeScore('NATO', -100,unit.name.. ' was destroyed')
		elseif unit.dbid == 5833 then --RJ
			ChangeScore('NATO', -150,unit.name.. ' was destroyed')
		else --Rest of cases
			ChangeScore('NATO', -75,unit.name.. ' was destroyed')
		end
	end
end
DavidRN
Posts: 91
Joined: Tue Jan 17, 2023 8:41 pm

Re: Error Lua Scoring

Post by DavidRN »

blu3s wrote: Tue Jan 31, 2023 7:57 am
DavidRN wrote: Tue Jan 31, 2023 6:03 am Thanks again,

I found another way of setting the score via a Lua Legion user and I found this one easier to work with and it (finally- Lua scoring) worked for me, the one above I followed ephotopros on YouTube.

Only being playing C:MO for a few weeks now and had never heard of Lua, but wanted to get into it straight away

I assume this ....
if unit.subtype == "2657" then points = - 50 -- FFG

Is the trigger for this (or vice versa?) ....
local unit = ScenEdit_UnitX() -- get the unit wrapper for the unit that was destroyed

...



Regards

DB

I wrote a script in lua that filtered by type and then subtypes / dbid I leave it at the end of the message

Some considerations. When filter, start always for the big groups, as you do filter by type, and inside the if, create the filters necessary.

In this case if you want to filter when a specific FFG is destroyed, use unit.dbid == 2657 but inside the if unit.type=='Ship'.

Unit subtype are strings and as you can se here, have this values for aircraft (and if you scroll down you can see the rest of the subtypes)

Here is the Lua Function, use a ChangeScore in a LuaInit script that is executed when the scen is loaded. Attached too

Code: Select all

function ChangeScore(side,amt,reason)
	local newScore = ScenEdit_GetScore(side) + amt
	ScenEdit_SetScore(side,newScore,reason)
	print (side..' score changed to '..newScore)
	return newScore
end

--UNIT IS DESTROYED script
local unit =  ScenEdit_UnitX()
if unit.type ~= 'Weapon' then
	if unit.type == "Ship" then
		if unit.subtype=="2008" then --CARRIER or use unit.dbid==number of carrier
			ChangeScore('NATO',-25000,unit.name.. ' was lost; the mission is doomed.')
		else
			ChangeScore('NATO',-1000,unit.name.. ' was sunk')
		end
	elseif unit.type == "Submarine" then
		ChangeScore('NATO',-700,unit.name.. ' was sunk')
	elseif unit.type == "Facility" then
		if unit.subtype == "3001" then --BUILDING SURFACE
			ChangeScore('NATO', -250,unit.name.. ' was destroyed')
		elseif unit.subtype == "3005" then
			ChangeScore('NATO',-25000,unit.name.. ' city was destroyed. More than 245.000 casualities; the mission is doomed.')
			ScenEdit_EndScenario()
		else
			ChangeScore('NATO', -100,unit.name.. ' was destroyed')
		end
	elseif unit.type == "Aircraft" then
		if unit.dbid == 1626 then --AWACS
			ChangeScore('NATO', -100,unit.name.. ' was destroyed')
		elseif unit.dbid == 5833 then --RJ
			ChangeScore('NATO', -150,unit.name.. ' was destroyed')
		else --Rest of cases
			ChangeScore('NATO', -75,unit.name.. ' was destroyed')
		end
	end
end

Thanks again will try in the next few day

DB
Post Reply

Return to “Lua Legion”