Server:Packet.writeChar Server function

From Gothic 2 Online: Wiki
Revision as of 17:05, 24 September 2017 by Patrix (talk | contribs) (Created page with "__NOTOC__{{Server function}} {{Version Info|version=0.0.3.0}} Insert char value to packet. (1 byte) == Syntax == <syntaxhighlight lang="cpp"> void Packet.writeChar(char valu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This function was added in version: 0.0.3.0

Insert char value to packet. (1 byte)

Syntax

void Packet.writeChar(char value)

Required Arguments

  • value: number in range -128...127

Example

Client

function random(min, max)
{
	return min + rand() % (max + 1 - min)
}

const LOTTERY_PACKET_ID = 1002;

addEventHandler("onCommand", function(cmd, params)
{
	if (cmd == "lottery")
	{
		packet <- Packet();

		packet.writeUInt16(LOTTERY_PACKET_ID);
		packet.writeChar(random(-127,127)); // writing random number from range -127 to 127

		packet.send(RELIABLE_ORDERED);
	}
});

Server

const LOTTERY_PACKET_ID = 1002;

addEventHandler("onPacket",function(pid, packet)
{
	if (packet.readUInt16() == LOTTERY_PACKET_ID)
	{
		local id = packet.readChar(); // reading random number generated on client-side

		if (id == 64)
		{
			local gold = Items.id("ITMI_GOLD"); // getting gold item id from items.xml file

			if (gold != -1) // if item is registered, then..
			{
				giveItem(pid, gold, 100);
				sendMessageToPlayer(pid, 0, 255, 0, "You have won 100 gold in lottery!");
			}
			else
			{
				sendMessageToPlayer(pid, 255, 0, 0, "Sorry, you have won 100 gold, but this item isn't registered :/");
			}
		}
	}
});

Related functions