HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

unit is selected

08-10-2006, 02:06 AM#1
emjlr3
using the event a specific unit is selected

is there a way to return what player selected him

GetTriggerPlayer()

seems to return the units owner, not who selected it

im asking for use in a trigger like this

Collapse JASS:
function Trig_Upgrades_Conditions takes nothing returns boolean
    if GetTriggerUnit()==gg_unit_hars_0012 then
        call BJDebugMsg("selected")
        return GetTableBoolean("Alliance","up_on")==false        
    else
        return GetTableBoolean("Horde","up_on")==false
    endif
endfunction

function Trig_Upgrades_Actions takes nothing returns nothing
    local player p = GetTriggerPlayer()    
    local unit u = GetTriggerUnit()
    
    call BJDebugMsg("run1")
    call BJDebugMsg(GetPlayerName(p))
    
    if u==gg_unit_hars_0012 then
        call BJDebugMsg("run2")
        call SetTableBoolean("Alliance","up_on",true)
        call SetTableObject("Alliance","up_player",p)
        call SetUnitOwner( u, p, true )
        call BJDebugMsg("endrun2")
    else
        call SetTableBoolean("Horde","up_on",true)
        call SetTableObject("Horde","up_player",p)
        call SetUnitOwner( u, p, true )
    endif
    
    set u = null
endfunction

//===========================================================================
function InitTrig_Upgrades takes nothing returns nothing
    set gg_trg_Upgrades = CreateTrigger(  )
    call TriggerRegisterUnitEvent( gg_trg_Upgrades, gg_unit_hars_0012, EVENT_UNIT_SELECTED )
    call TriggerRegisterUnitEvent( gg_trg_Upgrades, gg_unit_ovln_0013, EVENT_UNIT_SELECTED )
    call TriggerAddCondition( gg_trg_Upgrades, Condition( function Trig_Upgrades_Conditions ) )
    call TriggerAddAction( gg_trg_Upgrades, function Trig_Upgrades_Actions ) 
endfunction

basically just, if the unit is selected, it is switched to be owned by t he person who selected it

i have another local trigger that will restore ownership one the unit is deselected, howver for that to be needed, this must work

and so far...it doesn't
08-10-2006, 02:11 AM#2
Wyvernoid
Hello^^
For your problem I think I'll do this:
GetOwningPlayer(GetTriggeringUnit())
Hope it helps ;-)
08-10-2006, 02:15 AM#3
emjlr3
that would return the owner of the unit selected

i need the player that selected the unit

where GetTriggerPlayer() fails
08-10-2006, 02:32 AM#4
Wyvernoid
Sorry^^
Didn't look at your question very carefully
Well I can do little now
Register 12 events?
Collapse JASS:
local integer i = 0
loop
    call TriggerRegisterPlayerSelectionEventBJ( gg_trg_TRG, Player(i), true )
    // Change true to false when deselect ^^
    i = i + 1
    exitwhen i == 11
endloop
Then you can use GetTriggeringPlayer() ;-)
08-10-2006, 02:32 AM#5
PipeDream
If only one player is allowed to select the unit at a time, you could make a quick hack with
Collapse JASS:
native GroupEnumUnitsSelected               takes group whichGroup, player whichPlayer, boolexpr filter returns nothing
to find which player has it selected. If multiples are allowed, you need a set structure with two operations:
- construct set of players with unit selected
- subtract one set from another
Fortunately forces should make this easy. The former should be no problem and you can convert GroupRemoveGroup from Blizzard.j to forces for the latter. There's also a nice wrapper for getting the selected units "GetUnitsSelectedAll"
08-10-2006, 02:57 AM#6
BDSM
Perhaps this native might prove useful in figuring out if a particular player has the unit selected:
Collapse JASS:
constant native IsUnitSelected      takes unit whichUnit, player whichPlayer returns boolean
08-10-2006, 03:06 AM#7
emjlr3
is ther a way to make it so only 1 unit can select it at a time?
08-10-2006, 03:43 AM#8
The_AwaKening
What if you disabled trigger when unit is selected, then use another event that enables that trigger again when unit is deselected.

Collapse JASS:
constant playerunitevent EVENT_PLAYER_UNIT_DESELECTED

You could of course get this all into one trigger by registering a new event in a local. Return the player as BDSM stated and register the EVENT_PLAYER_UNIT_DESELECTED to that player.

--edited to state using BDSM's method of player...Typo
08-10-2006, 03:54 AM#9
PipeDream
It's particularly easy to allow only one player to select it at a time. Stash on the unit the one player who currently has the unit selected. When you get a unit selected event, SelectUnit(u,false) for the player that's stashed on the unit. Then loop through all the players until IsUnitSelected is true, and stash that player on the unit.
---
BDSM is right about IsUnitSelected, that's a much easier way of building the sets than anything involving groups.
08-10-2006, 04:37 AM#10
Vexorian
Notice that you need to call SyncSelections() or something like that before using IsUnitSelected, so there would be a little delay in the trigger if you plan to use BDSM's plan.

You can simply use the Player event instead...
08-10-2006, 05:17 AM#11
PipeDream
That would be too easy.
---
I don't see why you would use SyncSelections-you're triggering off the select event.
08-10-2006, 12:00 PM#12
emjlr3
i hadnt thought of this before

obviously the unit is selected native works, my debug messages show that

I think what I will do is pretty much what was suggested

once the unit is selected, loop threw the players that has it selected, which should only be one(hopefully), then disabled the trigger unitl the unit is unselected by that player, which brings me to the next problem

again, the native, unit is unselected, u cant register what player that is for

should I just use a timer(like .03) and stop it when the unit is not longer selected, that is the only thing I can think to do for that

er unless u can register event unit unselected for a single player, then its not an issue
08-10-2006, 01:06 PM#13
Vexorian
I don't know, you can try BDSM's plan, test it in multiplayer, and if it desyncs add the call to SyncSelections() before checking IsUnitSelected()
08-10-2006, 03:38 PM#14
emjlr3
hmm, well u cant use a player unselects a unit, so looks like i will need a timer to do it

**let u see what ive got, i think it may work

Collapse JASS:
function up_player_Conds takes nothing returns boolean
    return IsUnitSelected(bj_ghoul[55],GetFilterPlayer())
endfunction

function up_player takes unit u returns player
    local boolexpr b = Condition(function up_player_Conds)
    local force f = CreateForce()
    local player p    
    
    set bj_ghoul[55] = u
    call ForceEnumAllies(f,GetOwningPlayer(u),b)
    call DestroyBoolExpr(b)
    set b = null
    set p = ForcePickRandomPlayer(f)
    call DestroyForce(f)
    set f = null
    set bj_randDistChance[55]=GetPlayerId(p)
    
    return p
endfunction

function Trig_Upgrades_Conditions takes nothing returns boolean
    if GetTriggerUnit()==gg_unit_hars_0012 then        
        if IsPlayerAlly(up_player(GetTriggerUnit()),Player(0)) then
            return GetTableBoolean("Alliance","up_on")==false  
        endif
        return false
    else
        if IsPlayerAlly(up_player(GetTriggerUnit()),Player(4)) then
            return GetTableBoolean("Horde","up_on")==false
        endif
        return false
    endif
endfunction

function unselect_effects takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local string s = GetAttTable(t)
    local unit u = GetTableUnit(s,"u")
    local player p = GetTablePlayer(s,"p")
    
    if IsUnitSelected(u,p)==false then
        if u==gg_unit_hars_0012 then
            call SetUnitOwner(u,Player(0),true)
            call SetTableObject("Alliance","up_player",Player(0))
            call SetTableBoolean("Alliance","up_on",false)
        else
            call SetUnitOwner(u,Player(4),true)
            call SetTableObject("Alliance","up_player",Player(4))
            call SetTableBoolean("Horde","up_on",false)
        endif
        call Clearst(s,t)
    endif
    
    set u = null    
endfunction

function Trig_Upgrades_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = Player(bj_randDistChance[55]) 
    local timer unselect = CreateTimer()
    local string s = GetAttTable(unselect)       
    
    if u==gg_unit_hars_0012 then        
        call SetTableBoolean("Alliance","up_on",true)
        call SetTableObject("Alliance","up_player",p)
        call SetUnitOwner( u, p, true )
         
        call SetTableObject(s,"p",p)
        call SetTableObject(s,"u",u)
        call TimerStart(unselect,.03,true,function unselect_effects)       
    else
        call SetTableBoolean("Horde","up_on",true)
        call SetTableObject("Horde","up_player",p)
        call SetUnitOwner( u, p, true )
        
        call SetTableObject(s,"p",p)
        call SetTableObject(s,"u",u)
        call TimerStart(unselect,.03,true,function unselect_effects)
    endif
    
    set u = null
endfunction

//===========================================================================
function InitTrig_Upgrades takes nothing returns nothing        
    set gg_trg_Upgrades = CreateTrigger(  )
    call TriggerRegisterUnitEvent( gg_trg_Upgrades, gg_unit_hars_0012, EVENT_UNIT_SELECTED )
    call TriggerRegisterUnitEvent( gg_trg_Upgrades, gg_unit_ovln_0013, EVENT_UNIT_SELECTED )
    call TriggerAddCondition( gg_trg_Upgrades, Condition( function Trig_Upgrades_Conditions ) )
    call TriggerAddAction( gg_trg_Upgrades, function Trig_Upgrades_Actions )
endfunction

.03 should be fast enough to catch a player if they unselect the building, before they can reselect it, it wouldnt matter anyway, this should be pretty well i should think
08-10-2006, 05:50 PM#15
PipeDream
Yes, you can register DESELECTED. see common.j