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

[LUA] How to check specific items on specific tile position!

Cornex

Web Developer
Staff member
Global Moderator
Joined
Jun 15, 2008
Messages
3,444
Solutions
5
Reaction score
1,166
Location
Sweden
Hello there, i just want to release an small tutorial how to check an item on specific position. It can be on a tile, an board or what ever.

In this small tutorial we will make an tile, and an lever to pull. When you pull the lever it should check if the item is on the tile.

Lets start!

STEP 1

Try to build an area like this :

ff7ody.png


Sorry for this noob pic ;p

Lets make an new file, call it position.lua ..
Lets open it, and now we will make an small config to check what item and what you recive so start like :

Lua:
local config = {
--- Here we will put the config in next step ---
}

This is how to start the config, now we will fill it with some information, start like this:

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

STEP 2

Now we will have the configuration, no we will continue with the functions. Start like this :

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

function onUse(cid, item, fromPosition, itemEx, toPosition) --- This always added if you gonna use something ---

local pos = {x = 1000, y = 1000, z = 7} --- This is the position left from the switch ---

Now we have added the function onUse and the position of the specific tile to check.

Now continue with :

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

function onUse(cid, item, fromPosition, itemEx, toPosition) --- This always added if you gonna use something ---

local pos = {x = 1000, y = 1000, z = 7} --- This is the position left from the switch, this is called "pos"---

if getTileItemById(pos, config.item).uid > 0  then --- Here it check after the item --- 
if doRemoveItem(getTileItemById(pos, config.item).uid,1)  then --- Here if the item is right then it remove it ---

Now we have added it to check for the item, and if it remove the item

Now we continue with the switch ID's, do something like this :

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

function onUse(cid, item, fromPosition, itemEx, toPosition) --- This always added if you gonna use something ---

local pos = {x = 1000, y = 1000, z = 7} --- This is the position left from the switch, this is called "pos"---

if getTileItemById(pos, config.item).uid > 0  then --- Here it check after the item --- 
if doRemoveItem(getTileItemById(pos, config.item).uid,1)  then --- Here if the item is right then it remove it ---

--- Switch id's ---
if item.itemid == 1945 then
doTransformItem(item.uid, 1946)
elseif item.itemid == 1946 then
doTransformItem(item.uid, 1945)
end
--- Switch id's ---

Now we added basic to check the lever id's so if goes right and left and execute the scripts from both ways.

Now we will add so it gives you the item if the item are right.
Continue with this:

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

function onUse(cid, item, fromPosition, itemEx, toPosition) --- This always added if you gonna use something ---

local pos = {x = 1000, y = 1000, z = 7} --- This is the position left from the switch, this is called "pos"---

if getTileItemById(pos, config.item).uid > 0  then --- Here it check after the item --- 
if doRemoveItem(getTileItemById(pos, config.item).uid,1)  then --- Here if the item is right then it remove it ---

--- Switch id's ---
if item.itemid == 1945 then
doTransformItem(item.uid, 1946)
elseif item.itemid == 1946 then
doTransformItem(item.uid, 1945)
end
--- Switch id's ---

doPlayerAddItem(cid, config.toget) --- If the item is right and on right tile "pos" then it execute this ---

--- Add this bit to, to close the script ---
return true
end
end 
--- Add this bit to, to close the script ---

Now you have added so the player get the item, now we will continue with the msg if the item are wrong
Continue with this:

Lua:
local config = {
      item = 8930, --- The item you need to put on the specific tile --- 
      toget = 2160, --- The item you will get when you pull the lever and remove the item ---
}

function onUse(cid, item, fromPosition, itemEx, toPosition) --- This always added if you gonna use something ---

local pos = {x = 1000, y = 1000, z = 7} --- This is the position left from the switch, this is called "pos"---

if getTileItemById(pos, config.item).uid > 0  then --- Here it check after the item --- 
if doRemoveItem(getTileItemById(pos, config.item).uid,1)  then --- Here if the item is right then it remove it ---

--- Switch id's ---
if item.itemid == 1945 then
doTransformItem(item.uid, 1946)
elseif item.itemid == 1946 then
doTransformItem(item.uid, 1945)
end
--- Switch id's ---

doPlayerAddItem(cid, config.toget) --- If the item is right and on right tile "pos" then it execute this ---

--- Add this bit to, to close the script ---
return true
end
end 
--- Add this bit to, to close the script ---
 
--- If the item is wrong, it shows this message ---
   	doPlayerSendTextMessage(cid, MESSAGE_EVENT_ORANGE, "You dont have put the right items on the table!")
   	return true
end
--- If the item is wrong, it shows this message ---

Now you are done with the script! Now we need to add this script and put right unique id on switch.
On the switch, put unique id "41242" in mapeditor.

And now open actions.xml and add this line :

Lua:
<action uniqueid="41242" event="script" value="position.lua"/>

Now you are done, and the script should look like this :

Lua:
local config = {
	item = 8930,
	toget = 2160,
}

function onUse(cid, item, fromPosition, itemEx, toPosition)

local pos = {x = 1000, y = 1000, z = 7}

if getTileItemById(pos, config.item).uid > 0  then
if doRemoveItem(getTileItemById(pos, config.item).uid,1)  then

if item.itemid == 1945 then
doTransformItem(item.uid, 1946)
elseif item.itemid == 1946 then
doTransformItem(item.uid, 1945)
end

doPlayerAddItem(cid, config.toget)
return true
end
end  
doPlayerSendTextMessage(cid, MESSAGE_EVENT_ORANGE, "You dont have put the right items on the table!")
return true
end

I hope this will help you understand some functions in LUA scripting!
Enjoy
 
Please tab :p

Tab is an second thing to do, i don't do it here.
Just wanna make it this way :p

- - - Updated - - -

PART 3 : LETS GO DEEPER

Let talk about the config, you remember the config we done? This one :

Lua:
local config = {
	item = 8930,
	toget = 2160,
}

You can name the config to what name you want, like

Lua:
local yournameofconfig = {
	item = 8930,
	toget = 2160,
}

That will be the main name.

You can also change the pos name, change this part:

Lua:
local pos = {x = 1000, y = 1000, z = 7}

to etc:

Lua:
local mytilepos = {x = 1000, y = 1000, z = 7}

Remember, this is just an example.
Then look at this part:

Lua:
if getTileItemById(pos, config.item).uid > 0 then

If it would be an normal it should look like etc:

Lua:
getTileItemById(pos, itemId)

And you have putted the item id in the "itemId". But now we use an config for this, at the top.
So instead you put like this

Lua:
getTileItemById(pos, yournameofconfig.item)

yournameofconfig.item <-- It calls the config named "yournameofconfig" and the "item" inside it and it get the item id from there.

And remember that we changed the pos name? You can also do it with it like this:

Lua:
getTileItemById(pos, itemId)

And you have putted the item id in the "itemId". But now we use an config for this, at the top.
So instead you put like this

Lua:
getTileItemById(pos, yournameofconfig.item)

to

Lua:
getTileItemById(pos, itemId)

And you have putted the item id in the "itemId". But now we use an config for this, at the top.
So instead you put like this

Lua:
getTileItemById(mytilepos, yournameofconfig.item)

We changed the "pos" to "mytilepos" if you remember!

I hope this get you to understand abit more about the config part.
Regards
 
If i were to use a table like this
Code:
Local ctable ={ [2225] = {2472, 2463, ect}
}
It says attempt to compare number with table. How do i do it correctly?
I used this tutorial and rusty remover script to figure it out now im stumped.
http://pastebin.com/6xKFBVb5
 
if getTileItemById(pos, itemEx.itemid).uid > 0 then

Parameters of getTileItemById are position (as table with x, y, z) and itemid
Returns
item/thing table
 
I do not even know what you are trying. Check if "itemEx.itemid" is the correct id which you want to check on the tile, adjust the doRemoveItem line too.
 
Back
Top