Jump to content

Detect when an element has been created?


Lpsd

Recommended Posts

  • MTA Team
Posted (edited)

I was wondering if there's a way to detect when an element has been created, via events.

then I found this from 2009, looks like they decided not to implement it, https://bugs.mtasa.com/view.php?id=4591#c25041

If not by handler, is it possible to replicate this kind of feature? Would be really useful imo.

 

Edited by LopSided_
Posted (edited)

You can try this; I'm not sure of the performance impact it would have though.

 


-- Table to store current elements
Elements = {}

-- Type of element you want to search for
Type = "object"

-- Factor in already created objects --
for i,v in pairs(getElementsByType(Type)) do
	Elements[v] = true
end

-- Every secound check for new elements --
setTimer ( function()
	for i,v in pairs(getElementsByType(Type)) do
		if not Elements[v] then
				Elements[v] = true
			triggerCreated(v)
		end
	end
end, 1000, 0 )

-- Output chat box the elements data if it was newly created --
function triggerCreated(element)
	outputChatBox(tostring(element).." Created") 
end

 

Edited by CodyL
  • Like 1
  • MTA Team
Posted
11 minutes ago, CodyL said:

You can try this; I'm not sure of the performance impact it would have though.

 


-- Table to store current elements
Elements = {}

-- Type of element you want to search for
Type = "object"

-- Factor in already created objects --
for i,v in pairs(getElementsByType(Type)) do
	Elements[v] = true
end

-- Every secound check for new elements --
setTimer ( function()
	for i,v in pairs(getElementsByType(Type)) do
		if not Elements[v] then
				Elements[v] = true
			triggerCreated(v)
		end
	end
end, 1000, 0 )

-- Output chat box the elements data if it was newly created --
function triggerCreated(element)
	outputChatBox(tostring(element).." Created") 
end

 

Just what I was looking for, thanks. ^_^

Posted

If you need a faster or a slower response time, lower the 1000 interval. To low may cause some performance impacts due to it looping through every thing of that object type every time that is triggered though.

  • Like 1
  • MTA Team
Posted

Small problem when trying to output/retrieve object's co-ordinates

 

-- Table to store current elements
Elements = {}

-- Type of element you want to search for
Type = "spawnpoint"

-- Factor in already created objects --
for i,v in pairs(getElementsByType(Type)) do
	Elements[v] = true
end

-- Every secound check for new elements --
setTimer ( function()
	for i,v in pairs(getElementsByType(Type)) do
		if not Elements[v] then
				Elements[v] = true
			triggerCreated(v)
		end
	end
end, 1000, 0 )
	
function triggerCreated(element)
	outputChatBox(tostring(element).." Created") 
	local x, y, z = getElementPosition ( element )
   outputChatBox("x: " .. x .. ", y: " .. y .. ", z: ".. z)
 end

 

"tostring(element)" gives me "userdata: 000002CD", or similar (the numbers/letters change each time a new element is made)

Therefore, I can't use that with getElementPosition. Any suggestions?

  • MTA Team
Posted
14 minutes ago, CodyL said:

tostring element outputs the userdata of the element

 

element is the element itself if you want remove the outputchatbox tostring(element)

Sorry, I didn't explain myself properly. I was using this:

local x, y, z = getElementPosition ( element )

However, x y & z are all 0.

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