HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Host detector

06-07-2006, 05:29 AM#1
ShadowDestroyer
According to an old headline, this code should detect the host and store in the variable GetHost:
Collapse JASS:
//This function takes nothing and returns the host of the game.
function GetHost takes nothing returns player
    //This stores the Id + 1 for each player.
    call StoreInteger(GameCache(), "missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
    //Setup the TriggerSyncReady call.
    call TriggerSyncStart()
    //Sync the value of the entry for each player.
    //Each value will sync to the value of the host.
    call SyncStoredInteger(GameCache(), "missionKey", "key")
    //Wait until the Game Cache syncs the key for everyone.
    call TriggerSyncReady()
    //Return the synced value as a player.
    return Player(GetStoredInteger(GameCache(), "missionKey", "key") - 1)
endfunction
Do I have to replace anything in here? Because, importing this as written doesn't work.

Please help, it would be useful if someone gave me a correct edited code.
06-07-2006, 06:06 AM#2
Blade.dk
Well, let me quote the description of the function from The Jass Vault:

You either need to have a function called GameCache() which takes nothing and returns a gamecache variable or to replace the GameCache function with a global variable.
06-07-2006, 01:59 PM#3
ShadowDestroyer
Not sure if I understand. I am really bad at jass, because I generally use the dreaded GUI.
06-07-2006, 02:07 PM#4
Jacek
If I understand correctly, you have to make variable of tyoe Gamecache (normal global GUI variable) and replace all Gamecache() in script with udg_nameofyourvariable. So if you made variable of type Gamecache named gc, you have to replace all Gamecache() with udg_gc
06-07-2006, 02:37 PM#5
The)TideHunter(
Yea, the code does say Replace the gamecache.

This would be how it looks like:

Create a global Game Cache Variable in your trigger editor, call it GC

Now for the host function, just type this instead:

Collapse JASS:
//This function takes nothing and returns the host of the game.
function GetHost takes nothing returns player
    //This stores the Id + 1 for each player.
    call StoreInteger(udg_GC,"missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
    //Setup the TriggerSyncReady call.
    call TriggerSyncStart()
    //Sync the value of the entry for each player.
    //Each value will sync to the value of the host.
    call SyncStoredInteger(udg_GC, "missionKey", "key")
    //Wait until the Game Cache syncs the key for everyone.
    call TriggerSyncReady()
    //Return the synced value as a player.
    return Player(GetStoredInteger(udg_GC, "missionKey", "key") - 1)
endfunction
06-07-2006, 02:52 PM#6
shadow1500
Also, dont forget to initilize the GC variable, with "Game Cache - Create Game Cache" (think this is how its called)
06-07-2006, 03:06 PM#7
ShadowDestroyer
Ok, the editor now accepts my code, but I still must be doing something wrong.
Here is my trigger:
Collapse JASS:
function Trig_Detect2_Actions takes nothing returns nothing
endfunction
//This function takes nothing and returns the host of the game.
function GetHost takes nothing returns player
    //This stores the Id + 1 for each player.
    call StoreInteger(udg_GC2,"missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
    //Setup the TriggerSyncReady call.
    call TriggerSyncStart()
    //Sync the value of the entry for each player.
    //Each value will sync to the value of the host.
    call SyncStoredInteger(udg_GC2, "missionKey", "key")
    //Wait until the Game Cache syncs the key for everyone.
    call TriggerSyncReady()
    //Return the synced value as a player.
    return Player(GetStoredInteger(udg_GC2, "missionKey", "key") - 1)
endfunction

//===========================================================================
function InitTrig_Detect2 takes nothing returns nothing
    set gg_trg_Detect2 = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Detect2, 0.01 )
    call TriggerAddAction( gg_trg_Detect2, function Trig_Detect2_Actions )
endfunction
I then have another trigger after .05 seconds into the game display a dialog for GetHost, but the dialog isn't appearing.
06-07-2006, 03:08 PM#8
Jacek
Well... you have empty actions ^_^ lets say you want to return that player to variable... make variable of type player, call it host and make your script:

Collapse JASS:
function GetHost takes nothing returns player
    //This stores the Id + 1 for each player.
    call StoreInteger(udg_GC2,"missionKey", "key", GetPlayerId(GetLocalPlayer()) + 1)
    //Setup the TriggerSyncReady call.
    call TriggerSyncStart()
    //Sync the value of the entry for each player.
    //Each value will sync to the value of the host.
    call SyncStoredInteger(udg_GC2, "missionKey", "key")
    //Wait until the Game Cache syncs the key for everyone.
    call TriggerSyncReady()
    //Return the synced value as a player.
    return Player(GetStoredInteger(udg_GC2, "missionKey", "key") - 1)
endfunction

function Trig_Detect2_Actions takes nothing returns nothing
    set udg_host = GetHost()
endfunction
//This function takes nothing and returns the host of the game.
//===========================================================================
function InitTrig_Detect2 takes nothing returns nothing
    set gg_trg_Detect2 = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Detect2, 0.01 )
    call TriggerAddAction( gg_trg_Detect2, function Trig_Detect2_Actions )
endfunction
06-07-2006, 03:08 PM#9
Vexorian
Make sure the dialog works, temporarily replace GetHost() with Player(0) in that dialog
06-07-2006, 03:17 PM#10
ShadowDestroyer
Ah, I see what you mean.

The dialog definetly works when telling it to open for player 1.

The game is now crashing when the dialog appears or this trigger runs.
06-07-2006, 03:23 PM#11
Blade.dk
Sure that the gamecache is initialized?
06-07-2006, 03:27 PM#12
ShadowDestroyer
I bet that is it, what is the jass code for that, and where do i place it? I am starting to understand jass... Thanks for the help.
06-07-2006, 03:32 PM#13
Zoxc
I like to give GUI users this:
Collapse JASS:
function GetHost takes nothing returns nothing
    local gamecache g = InitGameCache("wisp.w3v")
    call StoreInteger ( g, "Wisp", "Host", GetPlayerId(GetLocalPlayer ())+1)
    call TriggerSyncStart ()
    call SyncStoredInteger ( g, "Wisp", "Host" )
    call TriggerSyncReady ()
    set udg_Host = Player( GetStoredInteger ( g, "Wisp", "Host" )-1)
    call FlushGameCache(g)
    set g = null
endfunction

Make a global player variable named "Host". Copy and place this in the custom script header. Call it using in GUI using "Custom Script: call GetHost()". If the host leaves and you need to new host, just call the function in the leave event of the old host.
06-07-2006, 03:39 PM#14
Meanie
I think this function desyncs so people might get disconnected when using this so just call the function at map start and then use this to get the host...

Collapse JASS:
    call Player(GetStoredInteger(GameCache(), "missionKey", "key") - 1)
06-07-2006, 03:41 PM#15
ShadowDestroyer
Ya, I should probably use what I was using before. How do I do what blade.dk stated?