Jump to content

Detect when an element has been created?


Lpsd

Recommended Posts

  • Administrators

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_
Link to comment

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
Link to comment
  • Administrators
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. ^_^

Link to comment
  • Administrators

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?

Link to comment
  • Administrators
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.

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