Jump to content

Help - variables


Tunner

Recommended Posts

Hello there,

Id like to have something like "If player hits marker1, destroy object1" "If player hits marker2, destroy object2" - but in one function. Something like "if player hits markerX, destroy objectX" where X is a number that the system has to check and then destroy the apropriate object (with the same number).

Without this function the code will be HUGE (even more than it is now :D).

Thanks in advance

Link to comment
local marker = createMarker ( float x, float y, float z ) 
addEventHandler ("onColShapeHit", marker, 
function( hitElement, getLocalPlayer ) 
if( getLocalPlayer(hitElement)) then 
destroyElement(hitElement) 
end 
end 
  

this is just a quick throw together not sure if works but it would use same variables

Link to comment

local posTable = { 
    --{{markerx, markery, markerz}, {objx, objy, objz}} 
    {{100,100,100},{100,10,120}}, -- just example 
} 
  
function loadElements() 
    for i,v in ipairs(posTable) do 
        createMarkerAndObj(v[1],v[2]) 
    end 
end 
addEventHandler("onResourceStart", root, loadElements) 
  
function createMarkerAndObj(mark,obj) 
    local mx,my,mz = mark[1],mark[2],mark[3] 
    local ox,oy,oz = obj[1],obj[2],obj[3] 
    local mark = createMarker(mx,my,mz,"cylinder",1,255,0,0) 
    local obj = createObject(objecID,ox,oy,oz) 
    setElementData(obj, "destroySource", mark) 
    addEventHandler("onMarkerHit", mark, function(elem,dim) 
        if elem and dim and getElementType(elem) == "player" then 
            for i,v in ipairs(getElementsByType("object")) do 
                if getElementData(v, "destroySource") == source then 
                    destroyElement(v) 
                    break 
                end 
            end 
        end 
    end) 
end 

Not sure if this is what you was looking for, but it creates as many objects and markers as you put into the posTable (in given format), then when you hit a marker it finds object with the marker's data and destroys it

Link to comment

Thanks for the early replies. Im kinda confused now that ive seen the codes of yours. I will put part of the script here so we all understand each other. But I think the last code is what im looking for.

function blabla (markerHit, matchingDimension)

if (markerHit) == marker1 then

if (matchingDimension) then

setElementPosition (source.....)

destroyElement (marker1)

destroyElement (object1)

triggerClientEvent ("...", getRootElement ())

end

end

end

addEventHandler ( "onPlayerMarkerHit", getRootElement(), blabla)

function blabla (markerHit, matchingDimension)

if (markerHit) == marker2 then

if (matchingDimension) then

setElementPosition (source.....)

destroyElement (marker2)

destroyElement (object2)

triggerClientEvent ("...", getRootElement ())

end

end

end

addEventHandler ( "onPlayerMarkerHit", getRootElement(), blabla)

i need the script to detect anything which begins with "marker" or "object" and to do what i want with corresponding marker or object (if 1, then 1 etc.) automatically.

PS Dont mind the setElementPosition etc. im interested in parts of code about markers and objects.

Link to comment

why does the markers have to have a number? does it trigger a different event based on that?

let me know what will have to happen on the marker hit so I can edit my script based on that to make it work for your needs

EDIT: heres something, let me know if the markers have to have different type and size and color etc so I can fix it

  
local posTable = { 
    --{markerID,{markerx, markery, markerz}, {objx, objy, objz}} 
    {1,{100,100,100},{100,10,120}}, -- just example 
    {2,{100,100,100},{100,10,120}}, 
    {3,{100,100,100},{100,10,120}}, 
} 
  
function loadElements() 
    for i,v in ipairs(posTable) do 
        createMarkerAndObj(v[1],v[2],v[3]) 
    end 
end 
addEventHandler("onResourceStart", root, loadElements) 
  
function createMarkerAndObj(id,mark,obj) 
    local mx,my,mz = mark[1],mark[2],mark[3] 
    local ox,oy,oz = obj[1],obj[2],obj[3] 
    local mark = createMarker(mx,my,mz,"cylinder",1,255,0,0) 
    local obj = createObject(objecID,ox,oy,oz) 
    setElementData(obj, "destroySource", mark) 
    setElementData(mark, "markerID", id) 
    addEventHandler("onMarkerHit", mark, function(elem,dim) 
        if elem and dim and getElementType(elem) == "player" then 
            local id = getElementData(source, "markerID") or 0 
            if id == 1 then 
                -- trigger the event for marker 1 
            elseif id == 2 then 
                -- trigger the event for marker 2 
            elseif id == 3 then 
                -- and so on.. 
            end 
             
            for i,v in ipairs(getElementsByType("object")) do 
                if getElementData(v, "destroySource") == source then  
                    destroyElement(v) -- destroys the object with same ID than the marker 
                    break 
                end 
            end 
             
            destroyElement(source) 
        end 
    end) 
end 
  

Edited by Guest
Link to comment

Well the marker and objects dont have to have any numbers, its just how i wanted to demonstrate it. I also thought it would be possible to tell the script to automatically assign markers and objects to each other based on the name in which i would just change the number.

The principle: Theres a marker at for ex. 1,1,1 and an object (visual effect) at 1,0.5,1; Player hits the marker, gets teleported to 30,30,30 (theres nothing at all, just some piece of land) and the marker with the object described above are destroyed - they are no longer needed.

Hope its more clear now.

As for the type etc, dont mind that. I will edit it so it suits my needs. But its really close to what I want.

Link to comment
  • Moderators
The principle: Theres a marker at for ex. 1,1,1 and an object (visual effect) at 1,0.5,1; Player hits the marker, gets teleported to 30,30,30 (theres nothing at all, just some piece of land) and the marker with the object described above are destroyed - they are no longer needed.

So like this:

local marker1 = createMarker(1, 1, 1) 
local marker2 = createMarker(-10, -10, 1) 
  
function markerHitListener( hitElement ) 
    if source == marker1 then 
        --teleporting the element (can be a vehicle, a player etc): 
        setElementPosition(hitElement, 30, 30, 30) 
        --add code here to remove the visual effect 
        destroyElement(source) --destroying the marker 
    elseif source == marker2 then 
        setElementPosition(hitElement, -30, -30, 30) 
        --add code here to remove the visual effect 
        destroyElement(source) 
    end 
end 
addEventHandler("onMarkerHit", root, markerHitListener) 

Link to comment
  • Moderators

Well, yeah, but you will still get a lot of elseif (or maybe the visual effect modification can be calculated from the number or id of the marker, then you can do 1 function for all markers. This way, you won't get any elseif.

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...