HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

GetHost Function

06-28-2006, 08:12 PM#1
Karawasa
Hey,

I have heard there is a GetHost function. I saw something over at WC3JASS.com.

Could someone clue me in on how to implement it?

Thanks
06-28-2006, 08:43 PM#2
Alevice
http://www.wc3jass.com/viewtopic.php...highlight=host

If you know a little of Jass already, GetHost() will attempt to retrieve which player is the host.

So, when using a function that requires a player, GetHost() will point to that player.

For implementing it, copy it in your custom script section of the map (top element in the trigger window, where the map name is). You will need a function that returns a gamecache trigger (code below), or replace all instances of GameCache() with an actual global variable.

Collapse JASS:
function GameCache takes nothing returns gamecache
    return udg_YouGlobalGameCacheVar
endfunction
06-28-2006, 09:00 PM#3
The)TideHunter(
Using the GetHost function is ok, but it is quite slow.
So use something like this instead:
Requires a global gamecache called GC (or udg_GC in Jass)
Collapse JASS:
function GC takes nothing returns gamecache
    if(udg_GC == null) then
        call FlushGameCache(InitGameCache("GC"))
        set udg_GC = InitGameCache("GC")
    endif
    return udg_GC
endfunction

function H2I takes handle H returns integer
    return H
    return 0
endfunction

function I2Player takes integer I returns player
    return I
    return null
endfunction

function GetHost takes nothing returns player
    local player Host = I2Player(GetStoredInteger(GC(), "Host", "Host"))
    if(GetStoredInteger(GC(), "Host", "Host") == 0) then
        call StoreInteger(GC(), "missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
        call TriggerSyncStart()
        call SyncStoredInteger(GC(), "missionKey", "key")
        call TriggerSyncReady()
        set Host =  Player(GetStoredInteger(GC(), "missionKey", "key") - 1)
        call StoreInteger(GC(), "Host", "Host", H2I(Host))
    endif
    return Host
endfunction

Or assign the GetHost function to a variable, and use the variable, instead of using it every time.
06-29-2006, 12:07 AM#4
Karawasa
Quote:
Originally Posted by The)TideHunter(
Collapse JASS:
function Cache takes nothing returns gamecache
    if(udg_Cache == null) then
        call FlushGameCache(InitGameCache("Cache"))
        set udg_Cache = InitGameCache("Cache")
    endif
    return udg_Cache
endfunction

function H2I takes handle H returns integer
    return H
    return 0
endfunction

function I2Player takes integer I returns player
    return I
    return null
endfunction

function GetHost takes nothing returns player
    local player Host = I2Player(GetStoredInteger(Cache(), "Host", "Host"))
    if(GetStoredInteger(Cache(), "Host", "Host") == 0) then
        call StoreInteger(Cache(), "missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
        call TriggerSyncStart()
        call SyncStoredInteger(Cache(), "missionKey", "key")
        call TriggerSyncReady()
        set Host =  Player(GetStoredInteger(Cache(), "missionKey", "key") - 1)
        call StoreInteger(Cache(), "Host", "Host", H2I(Host))
    endif
    return Host
endfunction

Ok I modified the code a little. The gamecache variable I use in my map is simply called "Cache." Also, can you rewrite the code to store the host as a player variable? Perhaps like Player_Host or even Host.
06-29-2006, 12:24 AM#5
Alevice
You just rename the function to anything you want, although I think the name 'GetHost' makes sense, don't you agree?
06-29-2006, 09:16 AM#6
iNfraNe
It's rather stupid to store the handle and use "I2Player" like tidehunter said. Instead, just store the number of the player and use Player(storednumber). No stupid slow returnbugs needed.
Also you get a value from the gamecache twice in the same function while there's no need for it. You say the gethost function is slow, but your function is like even slower.

Just get the host at elapsed gametime (map init might bug?) and store it into a global player variable, or global integer variable, whichever suites you best.

Anyway, I've tested this function and it bugs when the original host leaves (or any player at all). It just returns the player that syncs the value the fastest, which might not always be the host.
06-29-2006, 11:49 AM#7
Chuckle_Brother
Note that GetHost has some bugs to it. In some very uncommon but still possible situations it will fail to retrieve the appropriate value for it. I talked to Tennis(guy who doned wrote this here function) and he said that you should probably be careful when using it, since if it fails to get the proper player value, well that won't be good.

So adding a contingency:
Collapse JASS:
set udg_Host = GetHost()
if udg_Host == null then
  set udg_Host = Player(0)
endif
06-29-2006, 12:13 PM#8
iNfraNe
What good would that do? if it fails it will just return a different player than the host.. not null...
06-29-2006, 12:27 PM#9
Chuckle_Brother
No thats the thing, I have found instances where it returned null, instead of an erroneous player. It is just blank, which is very odd.

So just defaulting it to a player in the odd case that it does happen is fine is it not? Cause that was the issue, not the wrong player, but a completely invalid player.
06-29-2006, 01:26 PM#10
SFilip
the gethost function has a little bug. sometimes it might happen that, at the beginning of the game some other player has a good responce and gets marked as the host.
phyrex1an @ thehelper fixed this by making it check the responce 5 times and get the average time for everyone.
http://www.thehelper.net/forums/show...76&postcount=8
i tested it a bit and it worked fine so far :)
06-29-2006, 01:40 PM#11
iNfraNe
Quote:
Originally Posted by Chuckle_Brother
No thats the thing, I have found instances where it returned null, instead of an erroneous player. It is just blank, which is very odd.

So just defaulting it to a player in the odd case that it does happen is fine is it not? Cause that was the issue, not the wrong player, but a completely invalid player.
But I dont see how this could happen, the gamecache has a value for each player. Syncing it will never make it return null?
If you understand why, please, explain :)
06-29-2006, 01:47 PM#12
Tim.
Yar, as had been said, only run GetHost() once or twice when the map starts, should the host leave the game when you run GetHost(), you'll be hitting a fatal error.
06-29-2006, 01:50 PM#13
iNfraNe
Thats not true at all tim :P it will just not return the correct player most of the times.
06-29-2006, 03:04 PM#14
The)TideHunter(
Quote:
Originally Posted by iNfraNe
It's rather stupid to store the handle and use "I2Player" like tidehunter said. Instead, just store the number of the player and use Player(storednumber). No stupid slow returnbugs needed.
Also you get a value from the gamecache twice in the same function while there's no need for it. You say the gethost function is slow, but your function is like even slower.

I was thinking for just storing the player number, but i dident have time to get confused about the -1 with Player() or GetPlayerId(), i never really looked at them.
And i called the function twice because i felt like doing so. Using it twice isent a great deal.
Saying that function is slower than the normal GetHost function used two times is rediculous, you are way to overdramatic, test them and look at the difference after a few tries. You are completly wrong.
06-29-2006, 04:01 PM#15
iNfraNe
Please... Ofcourse GetHost normally is slower, but thats just because it syncs, its like waiting. Your function slurps up processing time.
Dont try to defend your script please, just admit that you made some errors, im not offending you in any way, its just that your script sucked..
The only one who is completely wrong (and not the first time) is you.

(p.s. dont flame me please :))