how can I determine that the unit belongs to me?
Moderator: Moderators
how can I determine that the unit belongs to me?
may be like this?
uid = mouseSelection
myTeamID=Spring.GetMyTeamID()
if myTeamID==getUnitTeam(uid) then .......
uid = mouseSelection
myTeamID=Spring.GetMyTeamID()
if myTeamID==getUnitTeam(uid) then .......
Re: how can I determine that the unit belongs to me?
what unit do you mean with "the unit"?
Code: Select all
if Spring.GetMyTeamID () == Spring.GetUnitTeam (unitID) then
--unit belongs to me
end
Re: how can I determine that the unit belongs to me?
I'm trying to upgrade the widget Smart_select.
battle_unit variable is true if mouseSelection contains at least one combat unit.
Mylist includes all constructors, mylist_comm includes commanders.
The code below leaves in mouseSelection only combat units.
The problem is that if my commander to be surrounded by other people's fighting units, the variable battle_unit is true, although in mouseSelection be only my commander.
So I added the code myTeamID == getUnitTeam (uid) in the condition.
result
[f=0002823] Error in Update(): [string "LuaUI\Widgets\unit_smart_select.lua"]:355: Bad unitID parameter in GetUnitTeam()
[f=0002823] Removed widget: SmartSelect
Code: Select all
local team = (playing and GetMyTeamID())
mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)
local battle_unit = false
Mylist includes all constructors, mylist_comm includes commanders.
Code: Select all
for i=1, #mouseSelection do
uid = mouseSelection[i]
udid = GetUnitDefID(uid)
if (mylist[udid]==false and mylist_comm[udid]==false )then
battle_unit = true
break
end
end
Code: Select all
if (battle_unit) then
tmp = {}
for i=1, #mouseSelection do
uid = mouseSelection[i]
udid = GetUnitDefID(uid)
if (mylist[udid]== false and mylist_comm[udid]==false) then
tmp[#tmp+1] = uid
end
end
mouseSelection = tmp
end
The problem is that if my commander to be surrounded by other people's fighting units, the variable battle_unit is true, although in mouseSelection be only my commander.
So I added the code myTeamID == getUnitTeam (uid) in the condition.
Code: Select all
if (mylist[udid]==false and mylist_comm[udid]==false and myTeamID==getUnitTeam(uid) )then
[f=0002823] Error in Update(): [string "LuaUI\Widgets\unit_smart_select.lua"]:355: Bad unitID parameter in GetUnitTeam()
[f=0002823] Removed widget: SmartSelect
Re: how can I determine that the unit belongs to me?
If you do: mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)
With a team argument, why do you need to test again for team later?
Can you pastebin the whole widget? I suspect something wrong outside of those particular bits.
With a team argument, why do you need to test again for team later?
Can you pastebin the whole widget? I suspect something wrong outside of those particular bits.
Re: how can I determine that the unit belongs to me?
modified widget is very useful the widgets gui_idle_builders_new and unit_auto_group_com. control becomes like a supreme commander
if in mouseSelection are builders and military units the script selects only combat units.
if in mouseSelection are builders and military units the script selects only combat units.
Code: Select all
local battle_unit = false
for i=1, #mouseSelection do
uid = mouseSelection[i]
udid = GetUnitDefID(uid)
if (mylist[udid]==false and mylist_comm[udid]==false ) then
battle_unit = true
break
end
end
if (battle_unit) then
tmp = {}
for i=1, #mouseSelection do
uid = mouseSelection[i]
udid = GetUnitDefID(uid)
if (mylist[udid]== false and mylist_comm[udid]==false) then
tmp[#tmp+1] = uid
end
end
mouseSelection = tmp
- Attachments
-
- unit_smart_select.lua
- (13.61 KiB) Downloaded 39 times
Re: how can I determine that the unit belongs to me?
You store the unit selection in widget:MousePress
You use the unit selection in widget:Update
Inbetween, a few frames may have elapsed, unit IDs may have become invalid.
Either do everyting in widget:MousePress, or everything in widget:Update.
And bring back the Spring.GetMyTeamID from widget:Initialize as well, you wouldn't want your widget to break when a player change team ingame, would you?
Or... maybe the same selection is used in many calls to widget:Update, in which case you have to find a way to trim the dead units from it.
You use the unit selection in widget:Update
Inbetween, a few frames may have elapsed, unit IDs may have become invalid.
Either do everyting in widget:MousePress, or everything in widget:Update.
And bring back the Spring.GetMyTeamID from widget:Initialize as well, you wouldn't want your widget to break when a player change team ingame, would you?
Or... maybe the same selection is used in many calls to widget:Update, in which case you have to find a way to trim the dead units from it.
-
- Moderator
- Posts: 2464
- Joined: 12 Oct 2007, 09:24
Re: how can I determine that the unit belongs to me?
Why not use 'widget:SelectionChanged(selectedUnits)' ?
Re: how can I determine that the unit belongs to me?
iirc this isn't a default spring event and it may not exist depending on the widgethandler you're usingGoogle_Frog wrote:Why not use 'widget:SelectionChanged(selectedUnits)' ?
Re: how can I determine that the unit belongs to me?
I do not understand the difference.
works:
my commander
my commander and my constructor
my commander, my constructor and my combat unit
My commander and the commander of the enemy or the constructor or building.
but does not work
My commander and another combat unit.
what is the difference between.
works:
my commander
my commander and my constructor
my commander, my constructor and my combat unit
My commander and the commander of the enemy or the constructor or building.
but does not work
My commander and another combat unit.
what is the difference between.
Re: how can I determine that the unit belongs to me?
How/in which game is it even possible to select enemy units?
Or do you mean in replays or as spectator?
Or do you mean in replays or as spectator?
Re: how can I determine that the unit belongs to me?
All experiments were conducted in BA.
My attempt to change the logic of the widget is:
If builders and combat units get together in the selection rectangle, the final group will be involve only combat units. If there are not combat units, the final group will be involve only builders.
It can simply be explained by these screenshots:
This screenshot shows that the selected group involves only the Commander.
Then the builder adds to this group.
Now here comes the first combat unit and builders eliminates.
There is a problem in this screenshot.
Despite the fact that only my builder is in the framework of selection rectangle, he is not active. I understand this is due to the fact that my ally bot builds attack plane in the aircraft factory which refers to the fighting units.
It is impossible to choose my builder if there is ally combat unit.
The last screenshot shows that the allocation works only for my combat units despite that there are other enemy units are in the frame.
My attempt to change the logic of the widget is:
If builders and combat units get together in the selection rectangle, the final group will be involve only combat units. If there are not combat units, the final group will be involve only builders.
It can simply be explained by these screenshots:
This screenshot shows that the selected group involves only the Commander.
Then the builder adds to this group.
Now here comes the first combat unit and builders eliminates.
There is a problem in this screenshot.
Despite the fact that only my builder is in the framework of selection rectangle, he is not active. I understand this is due to the fact that my ally bot builds attack plane in the aircraft factory which refers to the fighting units.
It is impossible to choose my builder if there is ally combat unit.
The last screenshot shows that the allocation works only for my combat units despite that there are other enemy units are in the frame.
Re: how can I determine that the unit belongs to me?
hm, seems like something that can not be answered without looking at the widget in detail and understanding it..
That might be some work, not sure if somebody is in the mood for that.
That might be some work, not sure if somebody is in the mood for that.
Re: how can I determine that the unit belongs to me?
Maybe the problem is that GetTeamUnits (team) returns not only my units. units = GetTeamUnits (team). variable units contains my units and allied units as they have the same team.
-
- Posts: 834
- Joined: 19 May 2009, 21:10
Re: how can I determine that the unit belongs to me?
Are you sure? The engine differs between teams and allies. One team is what one player "normally" controls. In a 2v2 there are 2 teams on each side (each team is controlled by one player) and each side has one alliance containing both teams.Broker wrote:Maybe the problem is that GetTeamUnits (team) returns not only my units. units = GetTeamUnits (team). variable units contains my units and allied units as they have the same team.
Post your setup (script.txt) and the widget else it's very hard to help when we might talk about different things.
Re: how can I determine that the unit belongs to me?
why variable batle_unit is true when other player’s combat unit is in the selection area? Despite the fact that only my builder is in the selection area.
mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)
Code: Select all
local batle_unit = false
for i=1, #mouseSelection do
uid = mouseSelection[i]
udid = GetUnitDefID(uid)
if (mylist[udid]==false and mylist_comm[udid]==false) then
batle_unit = true
break
end
end
Code: Select all
local function GetUnitsInScreenRectangle(x1, y1, x2, y2, team)
local units
if (team) then
units = GetTeamUnits(team)
else
units = GetAllUnits()
end
local left, right = sort(x1, x2)
local bottom, top = sort(y1, y2)
local result = {}
for i=1, #units do
local uid = units[i]
x, y, z = GetUnitPosition(uid)
x, y = WorldToScreenCoords(x, y, z)
if (left <= x and x <= right) and (top >= y and y >= bottom) then
result[#result+1] = uid
end
end
return result
end
Code: Select all
local playing = GetPlayerInfo(myPlayerID).spectating == false
local team = (playing and GetMyTeamID())
- Attachments
-
- _script.txt
- (1.63 KiB) Downloaded 26 times
-
- unit_smart_select.lua
- (13.71 KiB) Downloaded 25 times
-
- Posts: 834
- Joined: 19 May 2009, 21:10
Re: how can I determine that the unit belongs to me?
In Update():
That doesn't work. GetPlayerInfo(myPlayerID).spectating is always nil.
As a result:
=> playing is false
=> team is false
=> GetAllUnits() is called, instead of GetTeamUnits()
=> units not from your team are checked, too
=> combat units not from your team will trigger "batle_unit = true"
Change the line to:
Code: Select all
local playing = GetPlayerInfo(myPlayerID).spectating == false
As a result:
=> playing is false
=> team is false
=> GetAllUnits() is called, instead of GetTeamUnits()
=> units not from your team are checked, too
=> combat units not from your team will trigger "batle_unit = true"
Change the line to:
Code: Select all
local playing = select(3,GetPlayerInfo(myPlayerID)) == false
Re: how can I determine that the unit belongs to me?
it is live ! thank you.