Jump to content

[Question] Best way to create tables and loop through.


Ice_Cool

Recommended Posts

Hello, I have just started learning scripting around 2 weeks now after finishing basic Lua. I created few simple resources myself and happy they work. So i decided to create something more complicated (for me) below is the script of moving objects when a player hits colShape. Example: #1 works fine so i decided to create it more efficient way Example: #2 which unfortunately don't work and i am getting it wrong i believe. 

Example: #1 (working script)

local gateObjOne = createObject (988, 1023.599609375, -367.7001953125, 74.099998474121)
local gateObjTwo = createObject (988, 1051, -323.599609375, 74.099998474121, 0, 0, 270)
local gateObjThree = createObject (988, 1093.2998046875, -355.5, 74.099998474121, 0, 0, 270)

local pColShapeOne = createColRectangle (1021, -371.5, 5, 8)
local pColShapeTwo = createColRectangle (1049, -326.5, 5, 8)
local pColShapeThree = createColRectangle (1090, -358.5, 5, 8)


function prisonGate (player)
	moveObject(gateObjOne, 1000, 1029.3000488281, -367.7001953125, 74.099998474121)
end
addEventHandler("onColShapeHit", pColShapeOne, prisonGate)

addEventHandler("onColShapeLeave", pColShapeOne, function()

	moveObject(gateObjOne, 1000, 1023.599609375, -367.7001953125, 74.099998474121)
end)

addEventHandler("onColShapeHit", pColShapeTwo, function()
	moveObject(gateObjTwo, 1000, 1051, -318.39999389648, 74.099998474121)
end)

addEventHandler("onColShapeLeave", pColShapeTwo, function()

	moveObject(gateObjTwo, 1000, 1051, -323.599609375, 74.099998474121)
end)	

addEventHandler("onColShapeHit", pColShapeThree, function()
	moveObject(gateObjThree, 1000, 1093.3000488281, -350.29998779297, 74.099998474121)
end)

addEventHandler("onColShapeLeave", pColShapeThree, function()

	moveObject(gateObjThree, 1000, 1093.2998046875, -355.5, 74.099998474121)
end)	

Example: #2 (Same script, I just decided to create it more efficient way). 

local prisonGateOpen = 			-- coordinates of opened gate. 
{
	{x = 1023.599609375, y = -367.7001953125, z = 74.099998474121},
	{x = 1051, y = -323.599609375, z = 74.099998474121, pX = 0, pY = 0, pZ = 270},
	{x = 1093.2998046875,y = -355.5, z = 74.099998474121, pX = 0, pY = 0, pZ = 270}

}
-- coordinates of gate when closed.

	local prisonGateClosed = 
{
	{cx = 1029.3000488281, cy = -367.7001953125, cz = 74.099998474121},
	{cx = 1051, cy = -318.39999389648, cz = 74.099998474121},
	{cx = 1093.3000488281,cy = -350.29998779297, cz = 74.099998474121}
}

-- ColShape coordinates. 

colShapes = 
{
	{colX = 1021,colY = -371.5,colWidth = 5,colHeight = 8},
	{colX = 1049,colY = -326.5,colWidth = 5,colHeight = 8},
	{colX = 1090,colY = -358.5,colWidth = 5,colHeight = 8}
}	

-- looping to create gates. 

			for _, t in ipairs(prisonGateOpen) do  
	
				local gates = createObject(988, t.x, t.y, t.z, t.pX, t.pY, t.pZ)
			end

-- looping through to get cols. 
	
			for _, col in ipairs (colShapes) do 
	
				local createColShapes = createColRectangle (colShapes[1], colShapes[2], colShapes[3], colShapes[4])
			end

-- Defining a function to get the gets open when a player hits colShape. 

function closegate () 

		moveObject(1000, v.cx, v.cy, v.cz)	
end
addEventHandler("onColShapeHit", createColShapes, closegate)

In this example 2 however it creates objects and cols but of course it doesn't work when player hits col. 

My Question:

What is the right way to define tables in such situation? And what is the right way to loop through these? I believe i have made things more complicated by running ipairs loop again and again. 

I truly respect your time, I don't want you to go on and edit the code as i am just learning I just want you guys to explain some good practices (regarding tables and loops in such situation) in best words you can and that's it. 

Thanking you in advance for your guidance.

Edited by Ice_Cool
Link to comment
  • Moderators
1 hour ago, Ice_Cool said:

What is the right way to define tables in such situation?

 

By nesting them, so that you know which table belongs to which gate.

local gates = {
	{
		openPosition = {x = 1023.599609375, y = -367.7001953125, z = 74.099998474121},
		closePosition = {cx = 1029.3000488281, cy = -367.7001953125, cz = 74.099998474121},
		colshapes = {
			{colX = 1021,colY = -371.5,colWidth = 5,colHeight = 8},
			-- {colX = 1021,colY = -371.5,colWidth = 5,colHeight = 8} 2 colshapes?
		},
		state = "closed",
		gateElement = nil
	},
  	--[[{
    	another gate
    }]]
}



--[[
	While creating the elements, make the link between element and table.
	When you have access to the element, you have direct access to the data that belongs to the gate.
]]

local dataByColshapes = {}

local gateElement = createObject(...)
local colshapeElement = createColRectangle(...)

local gate = gates[i]

gate.gateElement = gateElement -- save the gate element also inside of this table

-- Make the link
dataByColshapes[colshapeElement] = gate

 

-- hit colshape?:

local gate = dataByColshapes[source]
if gate then
	local gateElement = gate.gateElement
	-- move gate! gate.openPosition, gate.closePosition
end

 

Link to comment
1 hour ago, Ice_Cool said:

Error 1

 It has to be like this

createColShapes = createColRectangle (col.colX, col.colY, col.colWidth, col.colHeight)

Error 2

 You forgot to add the loop and Object name

1 hour ago, Ice_Cool said:

function closegate () 

		moveObject(1000, v.cx, v.cy, v.cz)	
end
addEventHandler("onColShapeHit", createColShapes, closegate)

 

 

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