Jump to content

wiki examples


haybail

Recommended Posts

i did some examples for the incomplete wiki functions but I didn't have an account on there. would someone like to do the honors?

i didnt like to see them missing and when they were it was an inconvenience cause you couldn't copy past the addEvent ;)

This example outputs the name of the element to chat

function onClientElementStreamIn () 
    outputChatBox ( getElementType ( source ) ) 
end 
addEventHandler ( "onClientElementStreamIn", getRootElement(), onClientElementStreamIn ) 

This example destroys the element

function onClientElementStreamOut () 
    destroyElement ( source ) 
    outputChatBox ( "element destroyed" ) 
end 
addEventHandler ( "onClientElementStreamOut", getRootElement(), onClientElementStreamOut ) 

This example outputs BOOM when a client resource is started

function onClientResourceStart ( startedResource ) 
    outputChatBox ( "BOOM" ) 
end 
addEventHandler ( "onClientResourceStart", getRootElement(), onClientResourceStart ) 

This example throws a player into the ocean

function onClientResourceStop ( stoppedResource ) 
    -- check if the resource stopped is this one 
    if stoppedResource == getThisResource ( ) then 
        setElementPosition ( getLocalPlayer(), 60, 165, 5 ) 
    end 
end 
addEventHandler ( "onClientResourceStop", getRootElement(), onClientResourceStop ) 

This example outputs information about the change but its broken

function onElementDataChange ( theName, theOldValue ) 
    outputChatBox ( "element data of " .. theName .. " has been changed" ) 
    outputChatBox ( "old value was " .. theOldValue ) 
end 
addEventHandler ( "onElementDataChange", getRootElement(), onElementDataChange ) 
  
function onCommandTest ( playerSource ) 
    if not button then 
        button = createVehicle ( 429, 0, 0, 5 ) 
        setElementData ( button, "type", 0 ) 
    end 
    outputChatBox ( getElementData ( button, "type" ) ) 
    setElementData ( button, "type", 800 ) 
    outputChatBox ( getElementData ( button, "type" ) ) 
end 
addCommandHandler ( "test", onCommandTest ) 

This example creates a marker or destorys an element when a player clicks but its broken

function onPlayerClick ( mouseButton, buttonState, clickedElement, worldPosX, worldPosY, worldPosZ ) 
    -- check if the click is left click 
    if mouseButton == "left" then 
        -- check if state is down and not up too 
        if buttonState == "down" then 
            -- if a element is clicked 
            if clickedElement then 
                -- destroy it 
                destroyElement ( clickedElement ) 
            -- else 
            else 
                -- create a marker where the player clicked 
                createMarker ( worldPosX, worldPosY, worldPosZ, "arrow", 2.0 ) 
            end 
        end 
    end 
    -- output who clicked 
    outputChatBox ( getClientName ( source ) .. " clicked" ) 
end 
addEventHandler ( "onPlayerClick", getRootElement(), onPlayerClick ) 

This example attempts to center a player if he hits a cylinder

createMarker ( 0, 0, 3.11, "cylinder" ) 
  
function onPlayerMarkerHit ( markerHit, matchingDimension ) 
    -- if the marker is a circle 
    if getMarkerType ( markerHit ) == "cylinder" then 
        -- get the spot of the marker 
        local x,y,z = getElementPosition ( markerHit ) 
        -- put the player at that spot 
        setElementPosition ( source, x, y, z ) 
        -- freeze the player so he doesnt walk out too quick 
        toggleAllControls ( source, false, true, false ) 
        -- unfreeze the player so he can continue 
        setTimer ( toggleAllControls, 750, 1, source, true, true, false ) 
    end 
    -- announce the player who hit 
    outputChatBox ( getClientName ( source ) .. " hit" ) 
end 
addEventHandler ( "onPlayerMarkerHit", getRootElement(), onPlayerMarkerHit ) 

This example outputs information when a player leaves

function onPlayerMarkerLeave ( markerLeft, matchingDimension ) 
    -- get the colour 
    local r,g,b,a = getMarkerColor ( markerLeft ) 
    -- output the results 
    outputChatBox ( "red: " .. r .. " green: " .. g .. " blue: " .. b .. " alpha: " .. a ) 
    outputChatBox ( "marker count: " .. getMarkerCount() .. " marker icon: " .. getMarkerIcon ( markerLeft ) .. " marker size: " .. getMarkerSize ( markerLeft ) ) 
    -- if the marker has a next in line like a checkpoint or a plane ring 
    if getMarkerTarget ( markerLeft ) then 
        -- output its position 
        local x,y,z = getMarkerTarget ( markerLeft ) 
        outputChatBox ( "next marker at " .. x .. " " .. y .. " " .. z ) 
    end 
    outputChatBox ( "marker type: " .. getMarkerType ( markerLeft ) .. " player who left: " .. getClientName ( source ) ) 
    -- finish 
end 
addEventHandler ( "onPlayerMarkerLeave", getRootElement(), onPlayerMarkerLeave ) 

This example outputs a message when a player is no longer targeting an element

function onPlayerTarget ( targettedElement ) 
    -- check if the player is looking away 
    if not targettedElement then 
        outputChatBox ( "you were looking at something", source ) 
    end 
end 
addEventHandler ( "onPlayerTarget", getRootElement(), onPlayerTarget ) 

This example throws a player out of his car and tells him to try again if he tries to pickup something whilst in a vehicle

function onPlayerPickupHit ( pickupHit, matchingDimension ) 
    -- check if the player is in a vehicle 
    if isPlayerInVehicle ( source ) then 
        -- throw the player out 
        removePlayerFromVehicle ( source ) 
        -- tell him to try again 
        outputChatBox ( "try again" ) 
        -- cancel the event so he doesnt get it 
        cancelEvent () 
    end 
end 
addEventHandler ( "onPlayerPickupHit", getRootElement(), onPlayerPickupHit ) 

This example outputs to everyone what weapon a player picked up if any

function onPlayerPickupUse ( thePickupToUse ) 
    -- if the pickup is a weapon 
    if getPickupType ( thePickupToUse ) == 2 then 
        -- get the weapons id 
        local id = getPickupWeapon ( thePickupToUse ) 
        -- if its got one 
        if id then 
            -- get its name and output it 
            outputChatBox ( getClientName ( source ) .. " picked up a " .. getWeaponNameFromID ( id ) ) 
        end 
    end 
end 
addEventHandler ( "onPlayerPickupUse", getRootElement(), onPlayerPickupUse ) 

This example checks if a player knows his account

function onCommandLogin ( playerSource, commandName, arg1, arg2 ) 
    -- check if a player has given his arguments 
    if arg1 and arg2 then 
        -- put the arguments in arg1 for username arg2 for password 
        if getAccount ( arg1, arg2 ) then 
            outputChatBox ( "player knows his account" ) 
        else 
            outputChatBox ( "player doesnt know his account?" ) 
        end 
    end 
end 
addCommandHandler ( "login2", onCommandLogin ) 

http://development.mtasa.com/index.php? ... ds_Example

Link to comment
if you got an account on mtabeta.com than you have one on the wiki to :wink:

But if you got serial problems you cant login at the wiki because it looks if you have a serial for safety reasons.

In my case i was able to edit the wiki all the time but then my serial on mtabeta.com vanished ;)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...