Point message fix for multiplayer: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 87: Line 87:


These are the values that will be sent to and received by the client. <br />
These are the values that will be sent to and received by the client. <br />
You will now need to comment out the old declaration for the m_radius variable like so <source lang=cpp>   
You will now need to comment out some old declarations for some of the variables like so <source lang=cpp>   
protected:
protected:
//int m_radius;
//int m_radius;
string_t m_messageText;
string_t m_messageText;
bool m_drawText;
//bool m_drawText;
bool m_bDeveloperOnly;
bool m_bDeveloperOnly;
bool m_bEnabled;
bool m_bEnabled;
Line 108: Line 108:
END_SEND_TABLE()
END_SEND_TABLE()
</source>
</source>
Next we need to add a keyfield for the origin point we added. <br />
Under the line <source lang=cpp> DEFINE_KEYFIELD(m_bDeveloperOnly, FIELD_BOOLEAN, "developeronly"), </source>
Add the following line<source lang=cpp> DEFINE_KEYFIELD(m_vecTextOrigin, FIELD_VECTOR, "messageorigin"), </source>
Moving along, we now need to modify the Think method of the entity.
Find <source lang=cpp> void CMessageEntity::Think(void) </source>
and replace the whole method so it looks like this <source lang=cpp>
void CMessageEntity::Think(void)
{
BaseClass::Think();
SetNextThink(gpGlobals->curtime + 0.1f);
Q_snprintf(m_szMessageText.GetForModify(), sizeof(m_szMessageText), "%s", STRING(m_messageText));
DrawMessageEntities();
}
</source>
This will copy the text from the entity in the level into a char array which we can then transmit to the client.
Now find <source lang=cpp> void CMessageEntity::DrawOverlays(void) </source>
and replace the whole method to look like this <source lang=cpp>
void CMessageEntity::DrawOverlays(void)
{
if (m_bDeveloperOnly && !g_pDeveloper->GetInt())
{
m_drawText = false;
return;
}
if (!m_bEnabled)
{
m_drawText = false;
return;
}
m_drawText = true;
}
</source>
All we need to tell the client is whether or not the entity is enabled or not. <br />
The client will handle when to draw the text when the player is close enough.

Revision as of 11:36, 23 August 2019

This guide will show you how to change the point_message entity in a Source SDK 2013 multiplayer mod, to work for multiple players.

Prerequisites

If you want to understand what is going on, then I suggest you know the following. However, you can always just copy and paste the code if you want.

- A decent understanding of C++.
- A simple understanding of how network entities work in the Source engine.

With that out of the way, lets get started!

Modifying the base.fgd file

In order for some code to work later, we need to add an origin to the point_message entity. If we do not do this, the text will always be on the ground. If you are not using your own custom base.fgd file, you can find the default one in steamapps/common/Source SDK Base 2013 Multiplayer/bin.

  1. Open the base.fgd file with your text editor of choice, I am using notepad++.
  2. In the default base.fgd I have, the point_message entity is located at line 2418. If you cannot find it, use the search utility of your text editor to find point_message.

It should look like this:

@PointClass base(Targetname, Parentname) size(-8 -8 -8, 8 8 8) = point_message : 
	"An entity that displays a text message in the world, at its origin."
[
	spawnflags(flags) =
	[
		1: "Start Disabled" : 0
	]

	message(string) : "Entity Message"
	radius(integer) : "Show message radius" : 128 : "Distance the player must be within to see this message."
	developeronly(choices) : "Developer Only?" : 0 : "If set, this message will only be visible when developer mode is on." =
	[
		0 : "No"
		1 : "Yes"
	]

	// Inputs
	input Enable(void) : "Start displaying the message text, if the player is within the message radius."
	input Disable(void) : "Stop displaying the message text."
]

Under the line

radius(integer) : "Show message radius" : 128 : "Distance the player must be within to see this message."

add the following.

messageorigin(origin) : "Origin (X,Y,Z)"

That is all you need to do the the base.fgd.

Optionally you can change the

message(string) : "Entity Message"

line to

message(string) : "Entity Message" : "" : "Max of 128 characters!"

This will come up in hammer and will warn users of the maximum character limit we will set later. You can change the limit however if you want more or less characters.

This is what the point_message entity in the base.fgd file should look like completed.

@PointClass base(Targetname, Parentname) size(-8 -8 -8, 8 8 8) = point_message : 
	"An entity that displays a text message in the world, at its origin."
[
	spawnflags(flags) =
	[
		1: "Start Disabled" : 0
	]

	message(string) : "Entity Message" : "" : "Max of 128 characters!"
	radius(integer) : "Show message radius" : 128 : "Distance the player must be within to see this message."
	messageorigin(origin) : "Origin (X,Y,Z)"
	developeronly(choices) : "Developer Only?" : 0 : "If set, this message will only be visible when developer mode is on." =
	[
		0 : "No"
		1 : "Yes"
	]

	// Inputs
	input Enable(void) : "Start displaying the message text, if the player is within the message radius."
	input Disable(void) : "Stop displaying the message text."
]

Modifying the server side message_entity code

We need to make some changes to the default message_entity code.

  1. Open up your games.sln solution, and search in the solution explorer for message_entity.cpp
  2. Open the file up and look for the line
     DECLARE_CLASS(CMessageEntity, CPointEntity);
    
  3. Under it add the following
     DECLARE_SERVERCLASS();
    

This will set the class up for networking.

  1. Now find the line
     DECLARE_DATADESC();
    
  2. Under it add the following lines
    CNetworkString(m_szMessageText,128);
    CNetworkVar(bool, m_drawText);
    CNetworkVar(Vector, m_vecTextOrigin);
    CNetworkVar(int, m_radius);
    

These are the values that will be sent to and received by the client.

You will now need to comment out some old declarations for some of the variables like so

  
protected:
	//int			m_radius;
	string_t		m_messageText;
	//bool			m_drawText;
	bool			m_bDeveloperOnly;
	bool			m_bEnabled;

Now we need to set up the server side data-table.

Under the line

LINK_ENTITY_TO_CLASS(point_message, CMessageEntity);

Add the following

IMPLEMENT_SERVERCLASS_ST(CMessageEntity, DT_MessageEntity)

	SendPropString(SENDINFO(m_szMessageText)),
	SendPropBool(SENDINFO(m_drawText)),
	SendPropVector(SENDINFO(m_vecTextOrigin)),
	SendPropInt(SENDINFO(m_radius)),

END_SEND_TABLE()

Next we need to add a keyfield for the origin point we added.

Under the line

 DEFINE_KEYFIELD(m_bDeveloperOnly, FIELD_BOOLEAN, "developeronly"),

Add the following line

 DEFINE_KEYFIELD(m_vecTextOrigin, FIELD_VECTOR, "messageorigin"),

Moving along, we now need to modify the Think method of the entity.

Find

 void CMessageEntity::Think(void)

and replace the whole method so it looks like this

void CMessageEntity::Think(void)
{
	BaseClass::Think();
	SetNextThink(gpGlobals->curtime + 0.1f);

	Q_snprintf(m_szMessageText.GetForModify(), sizeof(m_szMessageText), "%s", STRING(m_messageText));
	DrawMessageEntities();
}

This will copy the text from the entity in the level into a char array which we can then transmit to the client.

Now find

 void CMessageEntity::DrawOverlays(void)

and replace the whole method to look like this

void CMessageEntity::DrawOverlays(void)
{
	if (m_bDeveloperOnly && !g_pDeveloper->GetInt())
	{
		m_drawText = false;
		return;
	}

	if (!m_bEnabled)
	{
		m_drawText = false;
		return;
	}

	m_drawText = true;
}

All we need to tell the client is whether or not the entity is enabled or not.
The client will handle when to draw the text when the player is close enough.