Snippet GetHost 2.0

Azlier

Old World Ghost
Reaction score
461
This gets the host of a game. Remember that rather unreliable game cache syncing version? This is faster, and possible more reliable.

But, GetHost does not work on Map Initialization. Using TriggerRegisterHostDetected will fire any triggers registered to it immediately after a host is found. GetHost can be used in those triggers.

OnHostDetect is much the same, but it takes and fires a code variable instead. This action cannot be undone. The code must return a boolean, preferably false.

GetHost returns the host of a game. Once again, it doesn't work on map initialization.

Requires Event.
JASS:
library GetHost initializer Init requires Event

globals
    private unit array U
    private player Host = null
    private trigger OnDetect = CreateTrigger()
    private group Group = CreateGroup()
    private unit Enummed = null
    private Event Ev
endglobals

constant function GetHost takes nothing returns player
    return Host
endfunction

function OnHostDetect takes code c returns nothing
    call TriggerAddCondition(OnDetect, Condition(c))
endfunction

function TriggerRegisterHostDetected takes trigger t returns nothing
    call Ev.register(t)
endfunction

private function CalculateHost takes nothing returns boolean
    if Host == null then
        set Host = GetOwningPlayer(GetTriggerUnit())
        call TriggerEvaluate(OnDetect)
        call Ev.fire()
    endif
    return false
endfunction

private function FindSelected takes nothing returns boolean
    if Enummed == null then
        set Enummed = GetFilterUnit()
    endif
    return false
endfunction

private function RecalculateHost takes nothing returns boolean
    local integer i
    if GetTriggerPlayer() == Host then
        set i = 11
        set Host = null
        loop
            set Enummed = null
            call GroupEnumUnitsSelected(Group, Player(i), Filter(function FindSelected))
            if GetLocalPlayer() == Player(i) then
                call SelectUnit(Enummed, false)
                call SelectUnit(U<i>, true)
                call SelectUnit(U<i>, false)
                call SelectUnit(Enummed, true)
            endif
            exitwhen i == 0
            set i = i - 1
        endloop
    endif
    return false
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 11
    set Ev = Event.create()
    call TriggerAddCondition(t, Condition(function CalculateHost))
    loop
        set U<i> = CreateUnit(Player(i), &#039;hfoo&#039;, 10000, 10000, 270)
        call PauseUnit(U<i>, true)
        call TriggerRegisterUnitEvent(t, U<i>, EVENT_UNIT_SELECTED)
        set Enummed = null
        call GroupEnumUnitsSelected(Group, Player(i), Filter(function FindSelected))
        if GetLocalPlayer() == Player(i) then
            call SelectUnit(Enummed, false)
            call SelectUnit(U<i>, true)
            call SelectUnit(U<i>, false)
            call SelectUnit(Enummed, true)
        endif
        exitwhen i == 0
        set i = i - 1
    endloop

    set t = CreateTrigger()
    set i = 11
    loop
        call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_LEAVE)
        exitwhen i == 0
        set i = i - 1
    endloop
    call TriggerAddCondition(t, Condition(function RecalculateHost))
endfunction

endlibrary</i></i></i></i></i></i></i>
 

saw792

Is known to say things. That is all.
Reaction score
280
Missing initializer, Host variable is private, no GetHost() function, there's no GetTriggerPlayer() for regular unit events (unless that's not true and that's how it works?), and SELECTED is misspelled in the unit event line.
 

Azlier

Old World Ghost
Reaction score
461
Blah. I wish I checked it.

I hate not having WC3.

Also, GetOwningPlayer(GetTriggerUnit()) won't quite work. Does... GetTriggerPlayer work in selection cases like this?

If not, I'll need to use PlayerUnitEvents.
 

saw792

Is known to say things. That is all.
Reaction score
280
Oh and your Initializer returns boolean, and host will return null until someone leaves, I believe...:p

Could you explain how this is supposed to work?
 

Azlier

Old World Ghost
Reaction score
461
Simple, really. When selecting a unit via triggers, delay actually kicks in for the firing of the selection event. The player with the absolute lowest delay (host) will be registered first.

>host will return null until someone leaves, I believe...

Can't... stop... FAILING!
 

saw792

Is known to say things. That is all.
Reaction score
280
Yeah this really won't work, sorry Azlier. You need to use the player unit event in order to get a GetTriggerPlayer() to return an actual player. The selection thing can only really work if you are storing a different value for each player, which means that you have to use GetLocalPlayer() to select the unit locally so there's no net traffic.
 

Azlier

Old World Ghost
Reaction score
461
>The selection thing can only really work if you are storing a different value for each player,

...Hmm? If the game decides to wait, and fire all at once, it won't work. Experience shows that it doesn't do that, but if it does... I have another method to try. Azlier always has something up his sleeve!

>You need to use the player unit event in order to get a GetTriggerPlayer() to return an actual player.

So be it.
 

saw792

Is known to say things. That is all.
Reaction score
280
Hmm it depends whether SelectUnit() actually sends any net traffic... I can't be sure. Anyway let me test it :p
 

Azlier

Old World Ghost
Reaction score
461
[lJASS]SelectUnit[/lJASS] is unsynced at first, but it soon syncs itself out (I assume it takes the maximum delay time). [lJASS]SyncSelections[/lJASS] does it immediately.

I'll soon need to patch this to work in the event that the host leaves and the players have filled selection groups.
 

saw792

Is known to say things. That is all.
Reaction score
280
I just tested it in single player, it returns Player 16 as the host. Stand by for bnet test.
 

Azlier

Old World Ghost
Reaction score
461
That can't be good. Player 16 doesn't exist. :p

I know what you mean, however.

GetTriggerPlayer returns the owner of the unit?

How useless. I'll need to resort my other method. Tomorrow... tomorrow...
 

Azlier

Old World Ghost
Reaction score
461
Ah. Other method it is, then. I hate Blizzard.
 

Azlier

Old World Ghost
Reaction score
461
This is totally untested. It gets the player in Single Player, however. So I'll assume it works, but I still need real tests.

JASS:
library GetHost initializer Init

globals
    private unit array U
    private player Host = null
endglobals

constant function GetHost takes nothing returns player
    return Host
endfunction

private function CalculateHost takes nothing returns boolean
    if Host == null then
        set Host = GetOwningPlayer(GetTriggerUnit())
    endif
    return false
endfunction

private function RecalculateHost takes nothing returns boolean
    local integer i = 11
    set Host = null
    loop
        if GetLocalPlayer() == Player(i) then
            call SelectUnit(U<i>, true)
            call SelectUnit(U<i>, false)
        endif
        exitwhen i == 0
        set i = i - 1
    endloop
    return false
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 11
    call TriggerAddCondition(t, Condition(function CalculateHost))
    loop
        set U<i> = CreateUnit(Player(i), &#039;hfoo&#039;, 10000, 10000, 270)
        call PauseUnit(U<i>, true)
        call TriggerRegisterUnitEvent(t, U<i>, EVENT_UNIT_SELECTED)
        if GetLocalPlayer() == Player(i) then
            call SelectUnit(U<i>, true)
            call SelectUnit(U<i>, false)
        endif
        exitwhen i == 0
        set i = i - 1
    endloop

    set t = CreateTrigger()
    set i = 11
    loop
        call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_LEAVE)
        exitwhen i == 0
        set i = i - 1
    endloop
    call TriggerAddCondition(t, Condition(function RecalculateHost))
endfunction

endlibrary</i></i></i></i></i></i></i>
 
Reaction score
341
This is totally untested. It gets the player in Single Player, however. So I'll assume it works, but I still need real tests

Tested and it worked (offline).

Code:
Untitled Trigger 001
    Events
        Time - Elapsed game time is 0.00 seconds
    Conditions
    Actions
        Custom script:   local string host = GetPlayerName(GetHost())
        Custom script:   call BJDebugMsg(host)
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Ofc it returns the player on single player mode, but that doesn't mean it works.
Also using GetLocalPlayer with SelectUnit is perfectly safe, and a lame way to synch local datas.

If it caused server splits, the gui functions wouldn't used it.
 

Azlier

Old World Ghost
Reaction score
461
I assume it works, but the first post will not be updated without clear results through testing online.

EDIT:

I am sorry to say that this does [del]not[/del] work. Yay. Updating first post.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Fixed this for the almighty Aziler.

JASS:
library GetHost initializer Init //Not yet syntax checked.

globals
    private unit array U
    private player Host = null
    private trigger OnDetect = CreateTrigger()
    private group Group = CreateGroup()
    private unit Enummed = null
endglobals

constant function GetHost takes nothing returns player
    return Host
endfunction

function OnHostDetect takes code c returns nothing
    call TriggerAddCondition(OnDetect, Condition(c))
endfunction

private function CalculateHost takes nothing returns boolean
    if Host == null then
        set Host = GetOwningPlayer(GetTriggerUnit())
        call TriggerEvaluate(OnDetect)
    endif
    return false
endfunction

private function FindSelected takes nothing returns boolean
    if Enummed == null then
        set Enummed = GetFilterUnit()
    endif
    return false
endfunction

private function RecalculateHost takes nothing returns boolean
    local integer i = 11
    set Host = null
    loop
        set Enummed = null
        call GroupEnumUnitsSelected(Group, Player(i), Filter(function FindSelected))
        if GetLocalPlayer() == Player(i) then
            call SelectUnit(Enummed, false)
            call SelectUnit(U<i>, true)
            call SelectUnit(U<i>, false)
            call SelectUnit(Enummed, true)
        endif
        exitwhen i == 0
        set i = i - 1
    endloop
    return false
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 11
    call TriggerAddCondition(t, Condition(function CalculateHost))
    loop
        set U<i> = CreateUnit(Player(i), &#039;hfoo&#039;, 10000, 10000, 270)
        call PauseUnit(U<i>, true)
        call TriggerRegisterUnitEvent(t, U<i>, EVENT_UNIT_SELECTED)
        set Enummed = null
        call GroupEnumUnitsSelected(Group, Player(i), Filter(function FindSelected))
        if GetLocalPlayer() == Player(i) then
            call SelectUnit(Enummed, false)
            call SelectUnit(U<i>, true)
            call SelectUnit(U<i>, false)
            call SelectUnit(Enummed, true)
        endif
        exitwhen i == 0
        set i = i - 1
    endloop

    set t = CreateTrigger()
    set i = 11
    loop
        call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_LEAVE)
        exitwhen i == 0
        set i = i - 1
    endloop
    call TriggerAddCondition(t, Condition(function RecalculateHost))
endfunction

endlibrary</i></i></i></i></i></i></i>
 

Azlier

Old World Ghost
Reaction score
461
Many thanks. Updating first post now.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top