Game UserMessages (Source)

From AlliedModders Wiki
Revision as of 07:22, 3 November 2026 by Luki1412 (talk | contribs)
Jump to: navigation, search

Description

In addition to events, Source games use UserMessages to send messages from the server to clients.

There are two types of UserMesssage: BitStream UserMessages and Protocol Buffer UserMessages. BitStream UserMessage names can be retrieved using the Metamod: Source "meta game" command. However, this will not give you the type and order of the arguments being sent to the client.

Protocol Buffer UserMessages are well-documented in .proto files included in the game SDKs.

Games that use BitStream UserMessages

Games that use Protobuf UserMessages

Tutorial on the subject: https://forums.alliedmods.net/showthread.php?t=80256

Fade Flags

These may not be correct...

FFADE_IN            0x0001        // Just here so we don't pass 0 into the function
FFADE_OUT           0x0002        // Fade out (not in)
FFADE_MODULATE      0x0004        // Modulate (don't blend)
FFADE_STAYOUT       0x0008        // ignores the duration, stays faded out until new ScreenFade message received
FFADE_PURGE         0x0010        // Purges all other fades, replacing them with this one

Fade Function

Example Fade function (be sure to define the Fade Flags!)

This Fades the clients screen to a specified color, and stays until you reset the color to {0,0,0,0}

To modify it to Fade the screen for a certain amount of time, remove the STAYOUT flag, and pass a value to "fade & hold"

PerformFade(target, 500, {0, 128, 255, 51})
PerformFade(client, duration, const color[4]) {
	new Handle:hFadeClient=StartMessageOne("Fade",client)
	BfWriteShort(hFadeClient,duration)	// FIXED 16 bit, with SCREENFADE_FRACBITS fractional, milliseconds duration
	BfWriteShort(hFadeClient,0)		// FIXED 16 bit, with SCREENFADE_FRACBITS fractional, milliseconds duration until reset (fade & hold)
	BfWriteShort(hFadeClient,(FFADE_PURGE|FFADE_OUT|FFADE_STAYOUT)) // fade type (in / out)
	BfWriteByte(hFadeClient,color[0])	// fade red
	BfWriteByte(hFadeClient,color[1])	// fade green
	BfWriteByte(hFadeClient,color[2])	// fade blue
	BfWriteByte(hFadeClient,color[3])	// fade alpha
	EndMessage()
}

HudMsg Function

This does not work in CS:S.

This Draws a text Message to a specified players screen. This is just for educational purposes and there is a much easier way of doing this with native functions here: ShowHudText & SetHudTextParams

PerformHudMsg(client, "This is a Test")
PerformHudMsg(client, const String:szMsg[]) {
	new Handle:hBf = StartMessageOne("HudMsg", client)
	BfWriteByte(hBf, 3) //channel
	BfWriteFloat(hBf, 0.0); // x ( -1 = center )
	BfWriteFloat(hBf, -1); // y ( -1 = center )
	// second color
	BfWriteByte(hBf, 0); //r1
	BfWriteByte(hBf, 0); //g1
	BfWriteByte(hBf, 255); //b1
	BfWriteByte(hBf, 255); //a1 // transparent?
	// init color
	BfWriteByte(hBf, 255); //r2
	BfWriteByte(hBf, 0); //g2
	BfWriteByte(hBf, 0); //b2
	BfWriteByte(hBf, 255); //a2
	BfWriteByte(hBf, 0); //effect (0 is fade in/fade out; 1 is flickery credits; 2 is write out)
	BfWriteFloat(hBf, 1.0); //fadeinTime (message fade in time - per character in effect 2)
	BfWriteFloat(hBf, 1.0); //fadeoutTime
	BfWriteFloat(hBf, 15.0); //holdtime
	BfWriteFloat(hBf, 5.0); //fxtime (effect type(2) used)
	BfWriteString(hBf, szMsg); //Message
	EndMessage();
}

HookUserMessage Fade

Some sample code to hook a UserMessage, In this case Fade. You cannot send other UserMessages inside of a UserMessage Hook. Many simple functions such as PrintToChat call UserMessages.

public OnPluginStart() {
    HookUserMessage(GetUserMessageId("Fade"),HookFade,true)
}
public Action:HookFade(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init) {
	new duration = BfReadShort(bf)
	new holdtime = BfReadShort(bf)
	BfReadShort(bf) 	//You must read all of the bf values, even If you only want the last value such as this one.
	new r = BfReadByte(bf) 	//You do NOT need to store their value though.
	new g = BfReadByte(bf)
	new b = BfReadByte(bf)
	new a = BfReadByte(bf)
	
	PrintToServer("Duration: %i, HoldTime: %i, rgba: %i %i %i %i",duration,holdtime,r,g,b,a)
}