Jump to content

Strange bug that i can't fix


Protagonist95

Recommended Posts

Posted

Heres code to spawn objects and add indidivual ElementData by theyr model

Parameters = {
	{1337,"data","data"}, -- works
	{1338,"data1","data1"}, -- works
	{1339,"data2",100} -- doesn't work,duno why
}

function spawnLoot() 
	for i,k in ipairs(objects) do
	obj = createObject(k[1],k[2],k[3],k[4])
	col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
	if(obj) then	--	if it exists then
		setElementData(col,"col",true)
		setElementData(obj,"loot",true)
		attachElements(col,obj,-0.7,-0.7,-0.5)
		setElementCollisionsEnabled(obj,false)
	end 
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

function setObjectParameters()
local objects = getElementsByType ( "object" )
	for i,loot in ipairs(objects) do
		if getElementData(loot,"loot") == true then
			for index,key in ipairs(Parameters) do 
				if getElementModel(loot) == key[1] then
					setElementData(loot,key[2],key[3])
					outputChatBox("ID added")
				end
			end
		end
	end
end

addEventHandler ( "onResourceStart", getRootElement(), setObjectParameters)

The problem is that it sets data only if second and third parameters are equil in Parameters table.I can't figure out why...

Posted
58 minutes ago, MR.S3D said:

 

  • function spawnLoot()
  • for i,k in ipairs(objects) do

 

objects -- where is variable?

 

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}

 

Posted (edited)
14 minutes ago, Protagonist95 said:

 


objects = {	{1337,23.15625,-5.677734375,3},	{1338,19.15625,-5.677734375,3},	{1339,15.15625,-5.677734375,3}}

 

try with this

 

Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}



function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col	
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)

		end 
	end
	local objects = getElementsByType ( "object",resourceRoot )
	for i,loot in ipairs(objects) do
		if getElementData(loot,"loot") == true then 
			for index,key in ipairs(Parameters) do 
				if getElementModel(loot) == key[1] then
					setElementData(loot,key[2],key[3])
					outputChatBox("ID added")
					break
				end
			end
		end
	end	
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

 

Edited by MR.S3D
Posted (edited)
4 minutes ago, MR.S3D said:

try with this

 


Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}



function spawnLoot() 
	for i,k in ipairs(objects) do
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)

		end 
	end
	local objects = getElementsByType ( "object",resourceRoot )
	for i,loot in ipairs(objects) do
		if getElementData(loot,"loot") == true then 
			for index,key in ipairs(Parameters) do 
				if getElementModel(loot) == key[1] then
					setElementData(loot,key[2],key[3])
					outputChatBox("ID added")
					break
				end
			end
		end
	end	
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

 

No working,btw I tried to inset the "setObjectParameters" into "spawnloot"

Edited by Protagonist95
Posted
Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}


function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

 

Posted
6 minutes ago, MR.S3D said:

Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}


function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

 

Not working.Heres whole script if it somehow helps you

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}

Parameters = {
	{1337,"data","data"},
	{1338,"data1","data1"},
	{1339,"data2",100}
}

function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

function onColEnter ( thePlayer )
	local item = getElementsWithinColShape (source, "object")
	if isElementWithinColShape (thePlayer,source) and getElementData(source,"col") ~= false then
	setElementData(thePlayer,"inLoot",true)
		for i,k in ipairs(item) do 
			for index,key in ipairs(Parameters) do
				if getElementData(k,key[3]) ~= false then
				outputChatBox(tostring(getElementData(k,key[3])))
			end
			end
		end
    end
end
addEventHandler ("onColShapeHit",getResourceRootElement(), onColEnter)

function onColLeave ( thePlayer )
        if getElementsWithinColShape ( source, "player" ) then
        	setElementData(thePlayer,"inLoot",false)
        end
end
addEventHandler ("onColShapeLeave", getRootElement(), onColLeave)

 

Posted
Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}


function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

function onColEnter ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
		local item = getElementsWithinColShape (source, "object")
		if isElementWithinColShape (thePlayer,source) and getElementData(source,"col") == true then
			setElementData(thePlayer,"inLoot",true)
			outputChatBox('Enter ColShape')			
			for i,k in ipairs(item) do 
				local data 
				for index,key in ipairs(Parameters) do
					data = getElementData(k,tostring(key[3]))
					if data ~= false then
						outputChatBox(tostring( data ))
					end
				end
			end
		end
	end	
end
addEventHandler ("onColShapeHit",resourceRoot, onColEnter)

function onColLeave ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
        if getElementsWithinColShape ( source, "player" ) then
        	setElementData(thePlayer,"inLoot",false)
			outputChatBox('Leave ColShape')
        end
	end	
end
addEventHandler ("onColShapeLeave", resourceRoot, onColLeave)


  

 

Posted
2 minutes ago, MR.S3D said:

Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",100} -- doesn't work,duno why

}

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}


function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

function onColEnter ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
		local item = getElementsWithinColShape (source, "object")
		if isElementWithinColShape (thePlayer,source) and getElementData(source,"col") == true then
			setElementData(thePlayer,"inLoot",true)
			outputChatBox('Enter ColShape')			
			for i,k in ipairs(item) do 
				local data 
				for index,key in ipairs(Parameters) do
					data = getElementData(k,tostring(key[3]))
					if data ~= false then
						outputChatBox(tostring( data ))
					end
				end
			end
		end
	end	
end
addEventHandler ("onColShapeHit",resourceRoot, onColEnter)

function onColLeave ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
        if getElementsWithinColShape ( source, "player" ) then
        	setElementData(thePlayer,"inLoot",false)
			outputChatBox('Leave ColShape')
        end
	end	
end
addEventHandler ("onColShapeLeave", resourceRoot, onColLeave)


  

 

Not working :D

Posted
2 minutes ago, iPrestege said:

Try to put a string in the 100 part so it should looks like '100' .

I see your point,but thing is not in the numbers or strings.Lets assume that i would deal with large table which includes {1337,"something",true} or something like that.That wont solve the problem.

Posted
Parameters = {
{1337,"data","data"}, -- works

{1338,"data1","data1"}, -- works

{1339,"data2",'100'} -- doesn't work,duno why

}

objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}


function spawnLoot() 
	for i,k in ipairs(objects) do
		local obj
		local col
		local id = k[1]
		obj = createObject(k[1],k[2],k[3],k[4])
		col = createColCuboid(k[2],k[3],k[4],1.6,1.6,1.6)
		if(obj) then -- if it exists then
			setElementData(col,"col",true)
			setElementData(obj,"loot",true)
			attachElements(col,obj,-0.7,-0.7,-0.5)
			setElementCollisionsEnabled(obj,false)
		end 
		for index,key in ipairs(Parameters) do 
			if id == key[1] then
				setElementData(obj,key[2],key[3])
				outputChatBox("ID added")
				break
			end
		end		
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(), spawnLoot)

function onColEnter ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
		local item = getElementsWithinColShape (source, "object")
		if isElementWithinColShape (thePlayer,source) and getElementData(source,"col") == true then
			setElementData(thePlayer,"inLoot",true)
			outputChatBox('Enter ColShape')			
			for i,k in ipairs(item) do 
				local data 
				for index,key in ipairs(Parameters) do
					data = getElementData(k,tostring(key[3]))
					if data then
						outputChatBox(tostring( data ))
						break
					end
				end
			end
		end
	end	
end
addEventHandler ("onColShapeHit",resourceRoot, onColEnter)

function onColLeave ( thePlayer )
	if (isElement(thePlayer) and getElementType(thePlayer) == "player") then
        if getElementsWithinColShape ( source, "player" ) then
        	setElementData(thePlayer,"inLoot",false)
			outputChatBox('Leave ColShape')
        end
	end	
end
addEventHandler ("onColShapeLeave", resourceRoot, onColLeave)


  

 

Posted (edited)
local objects = {
	{1337,23.15625,-5.677734375,3},
	{1338,19.15625,-5.677734375,3},
	{1339,15.15625,-5.677734375,3}
}

local parameters = {
	[1337] = {"data","data"},
	[1338] = {"data1","data1"},
	[1339] = {"data2",100}
}

function spawnLoot()
	for k,v in pairs(objects) do --this can also be reworked
		local id = v[1]
		v.obj = createObject(v[1],v[2],v[3],v[4])
		v.col = createColCuboid(v[2],v[3],v[4],1.6,1.6,1.6)
		if v.obj then
			attachElements( v.col, v.obj, -0.7, -0.7, -0.5 )
			setElementCollisionsEnabled( v.obj,false )
		end
		setElementData( v.obj, tostring(parameters[id][1]), parameters[id][2] )
		addEventHandler( "onColShapeHit", v.col, onColEnter )
		addEventHandler( "onColShapeLeave", v.col, onColEnter )
	end
end
addEventHandler ( "onResourceStart", resourceRoot, spawnLoot)

function onColEnter( thePlayer, matchingDimension )
	if matchingDimension then
		setElementData( thePlayer, "inLoot", true )
		-- i don't get why not a single key
		outputChatBox("Data["..parameters[getElementModel( getElementAttachedTo( source ) )][1].."]: "..tostring(getElementData(getElementAttachedTo( source ), parameters[getElementModel( getElementAttachedTo( source ) )][1] )))
	end
end

function onColLeave( thePlayer, matchingDimension )
	if matchingDimension then
		setElementData( thePlayer, "inLoot", false )
	end
end

I rewrote the whole parameter part, using tables. Knowing the index, also the object table and logic can be improved. I would like to know if one key wouldn't be easier...

I also suggest to read the wiki about events bound to objects, since you are binding ALL to root. This is untested but it should work.

 

NOTE: DO NOT SIMPLY COPY PASTE AND SAY BYE BYE: LEARN FROM EACH SCRIPT!!!

have fun scripting :)

Edited by LoPollo
Posted

Thank you for the love of god!!!

I'm gona figure out how it works and what was the problem after I get some sleep :D

And yes,I was planning to read about Event system,because I'm new in LUA scripting

  • Like 1
Posted

Events are really nice: they are so much helpful in most situation. Better performance, easier to script and understand. Also tables are a powerful tool. Also remember that using local with a variable gives a performance boost in lua. Try to use it every time it is possible. Also functions can be local... but i'm not used to write it in the scripts so i omit this, damn. Nobody is perfect :/ Still have fun with scripting, good luck and if you have any doubt the wiki and internet are your best friend, followed by us that will help you at any specific question or problem ;)

PS: Reading too much is annoying (at least for me). It is much funnier trying already done script, and reading their codes trying to figure out how they work

  • Like 1

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