Jump to content

[Help] Custom grenade


chris1384

Recommended Posts

Hi there! I was working on a custom grenade and got stuck into this issue:

addEventHandler("onClientProjectileCreation", root, function(creator)
	if getProjectileType(source) == 17 then
		setTimer(markerfunc, 2000, 1, source)
	end
end)

ms = 0
mst = 0.5

function markerfunc(elem)
	local x,y,z = getElementPosition(elem)
	setElementPosition(elem, 0,0,0)
	m = createMarker(x,y,z,"cylinder",1,0,255,0,255)
	col = createColSphere(x,y,z,mst*12)
	addEventHandler("onClientRender", root, markersize)
	addEventHandler("onClientColShapeHit", col, colhit)
	setTimer(function()
		removeEventHandler("onClientColShapeHit", col, colhit)
		removeEventHandler("onClientRender", root, markersize)
		destroyElement(m)
		destroyElement(col)
	ms = 0
	end, 500, 1)
end

function markersize()
	ms = ms + mst
	setMarkerSize(m, ms)
	local mx,my,mz = getElementPosition(m)
	setElementPosition(m, mx, my, mz-mst/1.3)
end

function colhit(thePlayer, matchingDimension)
	if getElementType ( thePlayer ) == "player" then
		outputChatBox(tostring(getPlayerName(thePlayer)))
	end
end

- "So.. What's the problem?" you may ask. Well, the problem is when 2 players throw a teargas simultaneously, one of the markers does not dissapear, and a debug error appears that something's wrong with the lines 18 - 21. Maybe a table is needed..? Someone?

Edited by chris1384
Link to comment
  • Moderators

If tables are too much for you, then you can also consider the function block as part of functional programming. The only thing about it, is that memory leaks can occur more often. Which happens for example if you do not remove an addEventHandler from a local function.

 

This code will create the functions `markersize` and `colhit` for every new projectile. And when ALL eventHandlers are removed, so will be the functions. (Warning: if one of the handlers remains to exist, so will remain the whole block.)

 

addEventHandler("onClientProjectileCreation", root, function(creator)
    if getProjectileType(source) == 17 then
        setTimer(markerfunc, 2000, 1, source)
    end
end)



function markerfunc(elem)
    local ms = 0
    local mst = 0.5

    local x,y,z = getElementPosition(elem)
    setElementPosition(elem, 0, 0, 0)
    
    local m = createMarker(x, y, z, "cylinder", 1, 0, 255, 0, 255)
    local col = createColSphere(x, y, z, mst * 12)

    local function markersize()
        ms = ms + mst
        setMarkerSize(m, ms)
        local mx,my,mz = getElementPosition(m)
        setElementPosition(m, mx, my, mz-mst / 1.3)
    end

    local function colhit(thePlayer, matchingDimension)
        if getElementType ( thePlayer ) == "player" then
            outputChatBox(tostring(getPlayerName(thePlayer)))
        end
    end

    addEventHandler("onClientRender", root, markersize)
    addEventHandler("onClientColShapeHit", col, colhit)
    
    setTimer(function()
        removeEventHandler("onClientColShapeHit", col, colhit)
        removeEventHandler("onClientRender", root, markersize)
        destroyElement(m)
        destroyElement(col)
    end, 500, 1)
end

 

Edited by IIYAMA
  • Thanks 1
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...