• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Table check inquiry for positions

  • Thread starter Thread starter Xikini
  • Start date Start date
Status
Not open for further replies.
X

Xikini

Guest
TFS 0.3.7
Code:
local locations = {
    [{x = 1, y = 1, z = 1}] = {stuff = 1, stuff 2 = 2},
    [{x = 2, y = 2, z = 2}] = {stuff = 1, stuff 2 = 2}
}

local p = getCreaturePosition(cid) 
if locations[{x = p.x, y = p.y, z = p.z}] then
    -- do stuff
end
Just curious if this would work.
Not at home, and I want a better way to do it then the below method.
Code:
local locations = {
    [1] = {position = {x = 1, y = 1, z = 1}, stuff_1 = 1, stuff_2 = 2},
    [2] = {position = {x = 2, y = 2, z = 2}, stuff_1 = 1, stuff_2 = 2}
}

local p = getCreaturePosition(cid)
for i = 1, #locations do
    if locations[i].x == p.x and locations[i].y == p.y and locations[i].z == p.z then
        -- do stuff
        break
    end
end
 
Solution
X
Well, got it working like I wanted, but a bit differently.

If someone has another method that's less 'janky', I'd love to see a short example.

LUA:
local locations = {
   ["x = 118, y = 62, z = 3"] = {stuff_1 = 1, stuff_2 = 2},
   ["x = 122, y = 62, z = 3"] = {stuff_2 = 1, stuff_2 = 2}
}

function onCastSpell(cid, var)
   local p = getCreaturePosition(cid)
   local xyz = "x = " .. p.x .. ", y = " .. p.y .. ", z = " .. p.z
   if locations[xyz] then
       print(locations[xyz].stuff_1)
   end
   return true
end
Well, got it working like I wanted, but a bit differently.

If someone has another method that's less 'janky', I'd love to see a short example.

LUA:
local locations = {
   ["x = 118, y = 62, z = 3"] = {stuff_1 = 1, stuff_2 = 2},
   ["x = 122, y = 62, z = 3"] = {stuff_2 = 1, stuff_2 = 2}
}

function onCastSpell(cid, var)
   local p = getCreaturePosition(cid)
   local xyz = "x = " .. p.x .. ", y = " .. p.y .. ", z = " .. p.z
   if locations[xyz] then
       print(locations[xyz].stuff_1)
   end
   return true
end
 
Solution
idk about less janky but you could use a format to make the string look less ugly with all of those concatenations
local xyz = ("x = %d, y = %d, z = %d):format(p.x, p.y, p.z)
the way you did is how i'd do it too
 
My way but I like stuff to look a certain way.

Code:
local locations = {
   [1] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2},
   [2] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2}
}

function onCastSpell(cid, var)
    for i = 1, #locations do
        if locations[i].pos == getCreaturePosition(cid) then
            INFO = locations[i]
        break
        end
    end
 
    if INFO then
        print(INFO.stuff1)
    end

   return true
end

This would be the finished product of it

Code:
local locations = {
   [1] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2},
   [2] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2}
}
function onCastSpell(cid, var)
    for i = 1, #locations do
        if locations[i].pos == getCreaturePosition(cid) then INFO = locations[i] break end
    end
    if not INFO return doPlayerSendCancelMessage(cid, "no no no") end
   return true
end
 
Last edited:
My way but I like stuff to look a certain way.

Code:
local locations = {
   [1] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2},
   [2] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2}
}

function onCastSpell(cid, var)
    for i = 1, #locations do
        if locations[i].pos == getCreaturePosition(cid) then
            INFO = locations[i]
        break
        end
    end
  
    if INFO then
        print(INFO.stuff1)
    end

   return true
end
O(1) > O(n)
 
Make it a 1 liner. Makes the code super clean. :cool:
LUA:
local locations = {[1] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2}, [2] = {pos = {x = 1000, y = 1000, z = 7}, stuff_1 = 1, stuff_2 = 2}} function onCastSpell(cid, var) for i = 1, #locations do if locations[i].pos == getCreaturePosition(cid) then INFO = locations[i] break end end if not INFO return doPlayerSendCancelMessage(cid, "no no no") end return true end
 
Obviously you don't want the whole code liner, but lines that you don't need spaced out shouldn't be. In my code there is no need to use 4 lines to define INFO. It still looks clean. Where as doing this:

LUA:
local p, xyz = getCreaturePosition(cid), "x = " .. p.x .. ", y = " .. p.y .. ", z = " .. p.z

Looks like a mess so making it liner isn't ideal.
 
Obviously you don't want the whole code liner, but lines that you don't need spaced out shouldn't be. In my code there is no need to use 4 lines to define INFO. It still looks clean. Where as doing this:

LUA:
local p, xyz = getCreaturePosition(cid), "x = " .. p.x .. ", y = " .. p.y .. ", z = " .. p.z

Looks like a mess so making it liner isn't ideal.
ik ik, I was just poking fun.
 
Status
Not open for further replies.
Back
Top