• 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!

help in using tables in scripting

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Reaction score
186
i want to ask somthn if i want to use table style in scriptin somthng that tp according to unique id
shoud it looks lke that
Code:
idtable = {
         id = 1000
		 id1 = 2000
}
placetable = {
         place = {x= 1000, y=1000, z=7}
		 place1 = {x= 1002, y=1001, z=7}
}
function onStepIn(cid, item, position, fromPosition)
   if isInArray(idtable, item.uid) then
if untill here is right how to right in
Code:
doTeleportThing(cid, pos)
to get the values from th place table.

i can't define what i need exactly but if someone understood me would be glad if he helped :)
 
LUA:
idtable = {
         id = 1000,
	 id1 = 2000
}
placetable = {
         place = {x= 1000, y=1000, z=7},
	 place1 = {x= 1002, y=1001, z=7}
}
function onStepIn(cid, item, position, fromPosition)
   if isInArray(idtable, item.uid) then

LUA:
doTeleportThing(cid, placetable.place)

placetable.place
placetable.place1
etc...

Is that what you mean? ;p
 
and another thing when i write the script like that
does it get in order, i will try to oluustrate more
i mean when i write

Code:
idtable = {
         id = 1000,
         id1 = 2000
}
placetable = {
         place = {x= 1000, y=1000, z=7},
         place1 = {x= 1002, y=1001, z=7}
}
function onStepIn(cid, item, position, fromPosition)
   if isInArray(idtable, item.uid) then 
      doTeleportThing(cid, plactable.place)

does this means when the id is 1000 it will tp player to <place>
and when id is 2000 it will tp player to <place1>
or i need to write another if after it??
 
Your script wont work because it will always teleport to same place = placetable.place - but you need to get correct index first.

Look at these examples which do same, but in different ways: (should help to understand it)

1. Worst one, as we need to loop over whole table to get correct position.
Code:
-- first example
-- worst one, as we need to loop over whole table to get correct position

local positions = {
	{uid = 1000, pos = {x= 1000, y=1000, z=7}},
	{uid = 2000, pos = {x= 1002, y=1001, z=7}}
}

function onStepIn(cid, item, position, fromPosition)

	-- we need to loop over whole table to search our uid and get correct index
	-- worst way to do script like this, but this is just example

	local i = 0
	for _, t in ipairs(positions) do
		i = i + 1
		if(item.uid == t.uid) then
			-- we found uid in table
			local tmp = positions[i]
			doTeleportThing(cid, tmp.pos)
			break
		end
	end

	return TRUE
end

2. For me best way to do this.
Code:
-- last example
-- fastest(?) way to do this

local positions = {
	-- uid = pos
	[1000] = {x= 1000, y=1000, z=7},
	[2000] = {x= 1002, y=1001, z=7}
}

function onStepIn(cid, item, position, fromPosition)
	-- get position from table by unique id
	local myPos = positions[item.uid]

	if(not myPos) then -- check if positions exists (this is not really required - just in case you declare something wrong in movements.xml)
		return TRUE -- just exit script, it won't continue executing
	end

	doTeleportThing(cid, myPos) -- teleport player to dest
	return TRUE
end
 
No, in my second example I assigned variable (position) at specified index, for example:
positions[1000] = {x= 1000, y=1000, z=7}

so we just used: doTeleportThing(cid, positions[1000])

But if you want to declare more things there, than you need to insert TABLE inside TABLE

Working example:
Code:
local teleports = {
	-- uid = {position={}, level=x, effect=x}
	[1000] = {
		position = {x= 1000, y=1000, z=7},
		level = 30, -- remove this line to ignore level
		effect = 20 -- remove this line to ignore effect
	},
	[2000] = {
		position = {x= 1002, y=1001, z=7},
		level = 50,
		effect = 10
	}
}

function onStepIn(cid, item, position, fromPosition)
	-- get table with attributes from 'teleports' table by unique id
	local options = positions[item.uid]

	if(not options) then -- check if positions exists (this is not really required - just in case you declare something wrong in movements.xml)
		return TRUE -- just exit script, it won't continue executing
	end

	if(getPlayerLevel(cid) < options.level) then -- check if player have level
		doPlayerSendCancel(cid, "Sorry, your level is to low for this teleport.")
		return TRUE
	end

	doTeleportThing(cid, options.pos) -- teleport player to dest
	if(options.effect) then -- check if effect is specified
		doSendMagicEffect(getCreaturePosition(cid), options.effect)
	end
	return TRUE
end

Dunno, maybe you'll learn something from this, or I'm explaining not enought;d (For me its so clear, and have sense! so maybe it won't be understandable for everyone)
 
why do i have to write return true?
and for the effect , level ,position do i have to write them like that i mean must they refers to what they do cant i just call them lol ,nob ,fuck (just for example to illustrate what i mean)?
 
Last edited:
humm and can i ask somthng why do you make each if statemnt and end it why cant you make kike
if balablabal then
elseif blblbl then

then end all at the end
 
Last edited:
yeah you can name it as you want, I named it according what they do and I prefer simple names (I could also name it for example: newPosition, levelToTeleport, effectType but I hate that)

And about these nested 'if's - well I like to have it organized this way. Code execution goes from up to down, and I like to 'sort' (throw out) some exceptions at time.

Of course this code could look like this :thumbup:
Code:
function onStepIn(cid, item, position, fromPosition)
	-- get table with attributes from 'teleports' table by unique id
	local options = positions[item.uid]

	if(options ~= nil) then -- check if positions exists (this is not really required - just in case you declare something wrong in movements.xml)
		if(getPlayerLevel(cid) >= options.level) then -- check if player have level
			doTeleportThing(cid, options.pos) -- teleport player to dest
			if(options.effect) then -- check if effect is specified
				doSendMagicEffect(getCreaturePosition(cid), options.effect)
			end
		else
			doPlayerSendCancel(cid, "Sorry, your level is to low for this teleport.")
		end
	end

	return TRUE
end
 
Back
Top