TakePlayerScreenShot

From Multi Theft Auto: Wiki
Revision as of 10:01, 26 June 2023 by Srslyyyy (talk | contribs) (Replace to predefined variables.)
Jump to navigation Jump to search

This function forces a client to capture the current screen output and send it back to the server. The image will contain the GTA HUD and the output of any dxDraw functions that are not flagged as 'post GUI'. The image specifically excludes the chat box and all GUI (including the client console). The result is received with the event onPlayerScreenShot.

Syntax

bool takePlayerScreenShot ( player thePlayer, int width, int height [, string tag = "", int quality = 30, int maxBandwidth = 5000, int maxPacketSize = 500 ] )         

OOP Syntax Help! I don't understand this!

Method: player:takeScreenShot(...)


Required Arguments

  • thePlayer: the player to get the screen capture from.
  • width: the width of the capture image.
  • height: the height of the capture image.

Optional Arguments

  • tag: A string to help identify the screen capture. The string is passed to the matching onPlayerScreenShot event for your personal convenience.
  • quality: Quality of the final JPEG image from 0 to 100. A lower value can reduce the memory used by the image considerably which will result in faster and less intrusive uploads.
  • maxBandwidth: The amount of client upload bandwidth to use (in bytes per second) when sending the image.
  • maxPacketSize: The maximum size of one packet.

Returns

Returns true if the function was successfully, false if invalid arguments are specified.

Example

Example 1

This example will take a player screenshot whenever a player takes a picture with the camera weapon and then send the picture to all clients, which will render the latest screenshot in the bottom right corner of their screen.

Click to collapse [-]
Client
local screenSizeX,screenSizeY = guiGetScreenSize() -- save the current screen dimensions
local imgtexture
local takenBy

function wepFire(weapon)
	if weapon == 43 then -- if the weapon the player just fired is the camera
		triggerServerEvent("onPlayerTakesPhoto",localPlayer)
	end
end
addEventHandler("onClientPlayerWeaponFire",localPlayer,wepFire)

function updateLatestPhoto(img)
	if imgtexture then -- clean up the old dxTextrue if there is one
		destroyElement(imgtexture)
	end
	imgtexture = dxCreateTexture(img) -- create a new dxTexture from the image data so that we can render it using dxDrawImage
	takenBy = "taken by "..getPlayerName(source) -- let's also credit the photographer
end
addEvent("updatePhoto",true)
addEventHandler("updatePhoto",root,updateLatestPhoto)

function renderPhoto()
	if imgtexture then
		local sizeX, sizeY = 320, 240
		dxDrawImage(screenSizeX-sizeX,screenSizeY-sizeY,sizeX,sizeY,imgtexture) -- render the picture as well as the name of the photographer in the bottom right corner of the screen
		dxDrawText(takenBy,screenSizeX-sizeX,screenSizeY-sizeY,screenSizeX,screenSizeY,tocolor(0,0,0))
	end
end
addEventHandler("onClientRender",root,renderPhoto)
Click to collapse [-]
Server
function requestScreenshot()
	takePlayerScreenShot(source,320,240,"cameraphoto",50) -- the player just took a picture with the camera, let's request a screenshot to see what they took a photo of
end
addEvent("onPlayerTakesPhoto",true)
addEventHandler("onPlayerTakesPhoto",root,requestScreenshot)

function incomingPlayerScreenshot(res,status,img,timestamp,tag)
	if status == "ok" and tag == "cameraphoto" then -- make sure a picture was taken successfully and that we're the ones who requested this screenshot
		triggerClientEvent("updatePhoto",source,img)  -- trigger a client event to share the image with everyone on the server
	end
end
addEventHandler("onPlayerScreenShot",root,incomingPlayerScreenshot)

Requirements

This template will be deleted.

See Also