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
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top