Unit in area -> destroy units

Unit in area -> destroy units

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

Moderator: Moderators

Post Reply
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Unit in area -> destroy units

Post by daryl »

Hello,
please can you help me with code? I'm trying to learning LUA.

I would like to do this:
- When an unit of Team 0 is in the rectangle, "U" unit will be destroy

I wrote this code:

Code: Select all

function gadget:GetInfo()
   return {
      name = "test minimission",
      desc = "Minimission",
      author = "molixx",
      date = "September 19st,  2010",
      license = "GPL,  v.2,  2008",
      layer = 1,
      enabled = true,
   }
end





local u
local CreateUnit = Spring.CreateUnit
local GetGaiaTeamID = Spring.GetGaiaTeamID

function GameStart()

u = CreateUnit("armcv",150,0,150,90,GetGaiaTeamID())
Spring.SetUnitNeutral (u,  true)
Spring.SetUnitAlwaysVisible(u,  true)
Spring.SetUnitNoMinimap(u,  true)


if Spring.GetUnitsInRectangle(0,0,0,0,0) then
Spring.DestroyUnit("u",false,false)



end
Script create "u" unit, ok
But probably i Wrong something using GetUnitsInRectangle. Can y help me please??
Thx!
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Unit in area -> destroy units

Post by Jools »

Start by reading the documentation (and maybe not by creating a gadget, use widget instead):

Spring.GetUnitsInRectangle
( number xmin, number zmin, number xmax, number zmax [,number teamID] )
-> nil | unitTable = { [1] = number unitID, etc... }

What happens when you try to execute the code? What is the output of the unitTable that is returned?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Unit in area -> destroy units

Post by zwzsg »

Spring.GetUnitsInRectangle(0,0,0,0,0) is a rectangle from 0,0 to 0,0 -> it's a degenerate rectangle, all corners are the same point!
Use an actual rectangle, one with a not null area, if you hope to catch units.

Spring.GetUnitsInRectangle is a function that returns the units that are in the rectangle!
You must make use of the results of that function.

For example:

local units = Spring.GetUnitsInRectangle(0,0,500,500)
for _,u in ipairs(units)
Spring.DestroyUnit(u)
end
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Unit in area -> destroy units

Post by daryl »

zwzsg wrote:Spring.GetUnitsInRectangle(0,0,0,0,0) is a rectangle from 0,0 to 0,0 -> it's a degenerate rectangle, all corners are the same point!
Use an actual rectangle, one with a not null area, if you hope to catch units.

Spring.GetUnitsInRectangle is a function that returns the units that are in the rectangle!
You must make use of the results of that function.

For example:

local units = Spring.GetUnitsInRectangle(0,0,500,500)
for _,u in ipairs(units)
Spring.DestroyUnit(u)
end
sorry zwzsg i'm tring to learn LUA....
i wrote this code:

Code: Select all

function gadget:GetInfo()
   return {
      name = "test_dario",
      desc = "Aggiunge unità all'inizio del gioco.",
      author = "daryl - www.totalannihilation.it",  -- thx to http://springrts.com community,  knorke,  smoth,  Beherith,  jK .
      date = "September 19st,  2010",
      license = "GPL,  v.2,  2008",
      layer = 1,
      enabled = true,
   }
end

local u
local CreateUnit = Spring.CreateUnit
local GetGaiaTeamID = Spring.GetGaiaTeamID
local units = Spring.GetUnitsInRectangle(200,200,300,300)

function GameStart()
u = CreateUnit("armcv",100,0,100,0,GetGaiaTeamID())
Spring.SetUnitNeutral (u,  true)
Spring.SetUnitAlwaysVisible(u,  true)
Spring.SetUnitNoMinimap(u,  true)

for _,u in ipairs(units)
Spring.DestroyUnit(u)



end

but doesn't work..
ok for creating units...but units doesn't destroy when an unit is inside the rectangle... what i wrong again?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Unit in area -> destroy units

Post by zwzsg »

This code would not even compile. You should have an error about it in your infolog.

In Lua, but in truth in about every computing language even if the syntax varies, whenever you begin a function or a loop, you must mark where it ends. In Lua that is done with the keyword "end".

For exemple:

Code: Select all

function GameStart()
-- stuff
end
Or:

Code: Select all

for _,u in ipairs(units) do
-- stuff
end
Also, the gathering of unit with Spring.GetUnitsInRectangle and their destruction of units must happen in a GameFrame callin.

Overall, you cannot throw words in a file and expect it to work. Programming is very strict, a single mistyped character and nothing works. The computer won't guess what you want from vague indications. You must give precise instructions.

You must form a mental model of what happens, at what time. Then translate it accurately in code.
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Unit in area -> destroy units

Post by daryl »

zwzsg wrote:Overall, you cannot throw words in a file and expect it to work. Programming is very strict, a single mistyped character and nothing works. The computer won't guess what you want from vague indications. You must give precise instructions.
Hi zwzsg, I know that... but I don't know LUA, I wrote here becouse i'm trying to know this language, but it's so difficult for me (i'm not a coder).
Can I ask to you to make a simple code, as example, about my question?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Unit in area -> destroy units

Post by gajop »

Why not use SpringBoard or ZK's Mission editor for this? Both are primarily designed for non coders.

I demonstrate something similar to what you're trying to achieve in this video: https://www.youtube.com/watch?v=DBigX19uiYA
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Unit in area -> destroy units

Post by daryl »

gajop wrote:Why not use SpringBoard or ZK's Mission editor for this? Both are primarily designed for non coders.

I demonstrate something similar to what you're trying to achieve in this video: https://www.youtube.com/watch?v=DBigX19uiYA
hi gajop, I know ZK mission editor and work well with my game (a BA variant).
But the question is: can I publish missions for my game made with ZK Mission Editor in my website? is ZK mission editor open source?
also is Zero K Mission Editor protected by copiright or other and I can publish only missions for Zero K game??

I don't know Spring Board, it's interesting also... this evening I will try it.

Thx!
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Unit in area -> destroy units

Post by gajop »

daryl wrote: hi gajop, I know ZK mission editor and work well with my game (a BA variant).
But the question is: can I publish missions for my game made with ZK Mission Editor in my website? is ZK mission editor open source?
also is Zero K Mission Editor protected by copiright or other and I can publish only missions for Zero K game??
ZK mission editor is open source, and you can publish missions made from it on your website.
It might be protected with copyright, but it's licensed with an Open Source license. Missions are not protected under copyright, although they might still require a GPL compatible license as they use the SpringRTS engine.
This is what I remember at least. I recommend you look into the details yourself.
daryl wrote: I don't know Spring Board, it's interesting also... this evening I will try it.
For SpringBoard the situation is similar. It's open source (GPLv2) and you are free to license your missions as you want (but you likely need to satisfy the GPL license of the engine).
I am the author of it, and if you have any questions regarding it feel free to ask (I recommend the #springboard channel on Discord or in the spring lobby).
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Unit in area -> destroy units

Post by daryl »

gajop wrote:
daryl wrote: hi gajop, I know ZK mission editor and work well with my game (a BA variant).
But the question is: can I publish missions for my game made with ZK Mission Editor in my website? is ZK mission editor open source?
also is Zero K Mission Editor protected by copiright or other and I can publish only missions for Zero K game??
ZK mission editor is open source, and you can publish missions made from it on your website.
It might be protected with copyright, but it's licensed with an Open Source license. Missions are not protected under copyright, although they might still require a GPL compatible license as they use the SpringRTS engine.
This is what I remember at least. I recommend you look into the details yourself.
daryl wrote: I don't know Spring Board, it's interesting also... this evening I will try it.
For SpringBoard the situation is similar. It's open source (GPLv2) and you are free to license your missions as you want (but you likely need to satisfy the GPL license of the engine).
I am the author of it, and if you have any questions regarding it feel free to ask (I recommend the #springboard channel on Discord or in the spring lobby).
my game is completely open source and probably conforms to GPLv2, of course..
Your is a good notice for me.. and solves some of my gaps in LUA code for making missions and campaign.
thx very much!!

so... because I would like learn LUA in spring, is there a SPRING LUA detailed manual for LUA noob as me? I already see wiki page, but i thing that some spring wiki page need to be improved ... for noob as me :)
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Unit in area -> destroy units

Post by gajop »

There might be some tutorials on the wiki, but none that I would think to recommend.
If you don't know programming, I don't think SpringRTS is the best place to learn it.
Lua itself is relatively simple. The main element you would need to learn are the callin/callout and widget/gadget (unsynced/synced) concepts. Maybe you could read the code of existing games, there's plenty of those.
Post Reply

Return to “Lua Scripts”