| 08-08-2006, 04:23 PM | #1 |
How to find out who is the host A tutorial by: ragingspeedhorn Note: I did not discover this code, the original author is "Tennis" from www.wc3jass.com, I just made this tutorial so people who isent that familiar with jass (like myself) still can implement this (thanks to Zoxc for helping with this) Many maps uses the "player1 (red) = guy who decides everything", that is annoying because the host might not want to play as red but still wants to control the stuff which the host sometimes can do (as an example, difficulty settings, player 1 (red) is in a large number of maps able to set the difficulty of the game through a dialog or something similar). But what most people probably didn't knew is that with just a very small amount of custom scripts (Jass) and 1 variable you can detect who is the host. So here it goes, the 3 easy steps on how to create the variable and make the custom scripts. Step 1: The Variable. Create a new variable, name it "Host", set the variable type to "Player (player)", do not check out the Aray box and leave the initial value at none like I have done in the picture below. Step 2: Custom Script Header. Right, you have your variable now it is time to get the hardest part done. In the Trigger Editor you click at the top at the maps name like if it was any other trigger, in there you paste these lines of code (Make sure you paste it at the top): JASS:function GetHost takes nothing returns nothing local gamecache g = InitGameCache("Map.w3v") call StoreInteger(g, "Map", "Host", GetPlayerId(GetLocalPlayer ())+1) call TriggerSyncStart() call SyncStoredInteger(g, "Map", "Host" ) call TriggerSyncReady() set udg_Host = Player( GetStoredInteger(g, "Map", "Host" )-1) call FlushGameCache(g ) set g = null endfunction Step 3: Getting The Host. Now that you have the most of the work done you just need to make a last simple trigger where you set the event to Map initialization, conditions you leave untouched and in the actions you make a custom script like this: "Custom script: call GetHost()". Now it has checked who is the host and stored it in the variable, for further use you just need to address everything to the "Host" variable like you would usually do to Player 1 (red), it is as simple as that. Final note: This is not 100% stable, the GetHost() can fail sometimes and select another player, however that is quite rare. |
| 08-08-2006, 08:21 PM | #2 |
Nice tut, it can really help alot of people who are unawair. The only thing i can add, just a feature, is running the GetHost functions 5-10 times, and get the average, to make sure it dosent return the non-host ID, thats not needed though. |
| 08-08-2006, 08:28 PM | #3 |
Well that could be implemented yeah but personaly I have never had any failure with the script picking a fake host in a map that I played maybe 70-100 times ![]() EDIT: Thanks for the positive review btw |
| 08-08-2006, 09:31 PM | #4 |
people with slow connection (such as myself) might encounter problems since at the very beginning someone might be faster and get the host status. anyway average host script won't change your tutorial much (just the command you call and the map header part). here's the code: http://www.thehelper.net/forums/show...76&postcount=8 |
| 08-08-2006, 09:41 PM | #5 | |
Quote:
The guy mentions the script he shows being untested, are you 100% sure it works? Because I wouldn't like to update the tutorial and then get something out of it that does not work ![]() |
| 08-09-2006, 08:57 AM | #6 |
Also, the guy in the script uses it a few times in a loop, which i think is bad. The GetHost function is slow, cause is syncs, using it in a loop is just asking for lag. I'v never experienced any problems either with GetHost, its a risk, but very small. |
| 08-09-2006, 11:47 PM | #7 |
Great Job on the tutorial. I like this since I don't like being a red for host. ![]() |
| 08-15-2006, 08:17 AM | #8 |
Looks good. I'll fix a typo and approve this. |
| 08-15-2006, 12:01 PM | #9 | |
Quote:
Thanks for the approval Mike ![]() |
| 07-10-2007, 07:46 PM | #10 |
Just a word of warning to anyone reading this tut. I tried this on a LAN with two computers and it *always* selected the non-host as the host. Even with an average check. The non-host comp was slightly faster than the host comp. GetHost relies on the comps being similar and there being sufficient network lag. |
