chris1384 Posted February 24, 2018 Share Posted February 24, 2018 (edited) 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 February 24, 2018 by chris1384 Link to comment
Moderators IIYAMA Posted February 24, 2018 Moderators Share Posted February 24, 2018 (edited) 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 February 24, 2018 by IIYAMA 1 Link to comment
chris1384 Posted February 25, 2018 Author Share Posted February 25, 2018 Learned a lot from your code, I didn't know how to deal with the "onClientRender" and "onClientColshapeHit" events, so I though tables were a good idea for solving this . Thanks! 1 Link to comment
Moderators IIYAMA Posted February 25, 2018 Moderators Share Posted February 25, 2018 Tables are indeed a good way to deal with this. Included optimise this code. (speed/performance) But first things first. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now