Server:Packet.readChar Server function

From Gothic 2 Online: Wiki
Jump to navigation Jump to search

This function was added in version: 0.0.3.0

Gets char value from packet. (1 byte)

Syntax

char Packet.readChar()

Returns

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

Example

Client

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

const LOTTERY_PACKET_ID = 1000;

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 = 1000;

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