Question about a problem with a script

Question about a problem with a script

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Question about a problem with a script

Post by Broker »

Hi,

I’ve got a problem with a script. Unfortunately my tries have failed.

The problem is:
To execute a chunk of code only if a unit belongs to a certain list by name or by object name.

-- if the condition is true

if IsSkirm(ud)==true then

-- the following code to be executed:

IsSkirm(ud) -- the function returns true if the unit is in the list

local function IsSkirm(ud1)
for i, v in pairs(unitArray) do
if (v==ud1.objectname) then
return true
end
end
return false
end

-- the list:

local unitArray = {
"CORCV",
"CORCOM",
"ARMCOM",
"ARMCK",
}
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Question about a problem with a script

Post by FLOZi »

objectname will contain 3do or s3o, why not just use ud1.name?

Also are you passing a unitID or a unitDefID?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Question about a problem with a script

Post by knorke »

There is a UnitDefNames table that can be used for this.
example:
http://springrts.com/phpbb/viewtopic.ph ... es#p522427
(see 2nd code block)
If I did not misunderstand, it is excactly what you want to do.
(the "fly" table would be your "IsSkirm" table)
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: Question about a problem with a script

Post by Broker »

FLOZi wrote:objectname will contain 3do or s3o, why not just use ud1.name?

Also are you passing a unitID or a unitDefID?
I tried to change the widget named Adv Idle Indicator aka IdleBuildersNEW.
This widget now displays on the screen icons of any idle units that can be used to build - factories, combat engineers, constructors, and the commander. I want to limit the list only by constructors and the commander.
The following codes is used in the widget for selections:

local function IsIdleBuilder(unitID)
local udef = Spring.GetUnitDefID(unitID)
local ud = UnitDefs[udef]
local qCount = 0
if ud.buildSpeed > 0 then --- can build

last line selects all units that may be build.
For my purposes, I added a list of units that I would like to see in the result

local unitArray = {
"CORCV",
"CORCOM",
"ARMCOM",
"ARMCK",
}

Added a function that serves as a criterion for getting a unit to the list

local function IsSkirm(ud1)
for i, v in pairs(unitArray) do
if (v==ud1.objectname) then
return true
end
end
return false
end


and code was changed like this

local function IsIdleBuilder(unitID)
local udef = Spring.GetUnitDefID(unitID)
local ud = UnitDefs[udef]
local qCount = 0

before --if ud.buildSpeed > 0 then --- can build

after if IsSkirm(ud)==true then


after the changes it shows nothing :-)
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Question about a problem with a script

Post by FLOZi »

Code: Select all

if (v:lower() == ud1.name) then
1. Compare ud.name this is e.g. armpw
2. Either make your comparison strings lowercase in the table, or use :lower() to convert them
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: Question about a problem with a script

Post by Broker »

FLOZi wrote:

Code: Select all

if (v:lower() == ud1.name) then
1. Compare ud.name this is e.g. armpw
2. Either make your comparison strings lowercase in the table, or use :lower() to convert them
thank you very much )))
Post Reply

Return to “Lua Scripts”