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

Solved Create an item with a unique ID

ziggy46802

Active Member
Joined
Aug 19, 2012
Messages
418
Reaction score
27
How do I go about making it so when I use a certain object, it disappears, and a new object pops up with the same unique ID as it had before.

Do I use doCreateItem in some way or doTransformItem

Im really lost on this I've been working on this script for hours and cant figure it out.
 
Last edited:
Ok, first of all, not to be rude but you really did not explain it that well so I barely understood what you said. So I'll just show you my whole crappy script so I can get more help.

Code:
local bigrock = 8634
local smallrock = 8638
local rock = 4001 --put unique id of rock here
local respawnminimum = 1 --put minimum number of minutes for rock to respawn
local respawnmaximum = 1 --put maximum number of minutes for rock to respawn
local receiveminimum = .5 --put the lowest amount of the item you want to get here
local receivemaximum = 1 --put the maximum amount of the item you want to get here
local posa = {x=1094,y=1060,z=6}

local rand = math.random(0,10000)

create = doCreateItem(bigrock,posa)
create.uid=4001 

local function restore(cid, storage)
	setPlayerStorageValue(cid, rock, 1)
		doRemoveItem(getTileItemById(posa, smallrock).uid)
		addEvent(create, 1, 2, cid)

		return true
end	

local function create(cid, item, pos)
	return true
	end

local function goto(cid)
	addEvent(restore, 1000,5000, cid)
		return true
end
	
function onUse(cid, item, frompos, item2, topos)
if item.itemid == bigrock and
		 getPlayerStorageValue(cid, rock) < 2
		then	
			doRemoveItem(getTileItemById(posa, bigrock).uid)
			addEvent(goto, 1, 5, cid)
			--setPlayerStorageValue(cid, rock, 2) --only not being used for testing purposes
			doSendMagicEffect(getPlayerPosition(cid), 13)	
			doCreatureSay(cid, "You mined it!", TALKTYPE_ORANGE_1)
			
elseif
	getPlayerStorageValue(cid, rock) == 2
		then
			doCreatureSay(cid, "This rock has already been harvested, wait for it to grow back.", TALKTYPE_ORANGE_1)		
				return true
			

			end
				return true

			end
 
if I understood the code well, the function that is going to set the unique id is 'create', since you left it almost blank.
i'm sorry i didn't explain well, but let me try it again:
----this variable is "linked" to the item created:
item = doCreateItem(ITEM_ID, COUNT, {X=10,Y=10,Z=10})
----this creates an item with id ITEM_ID and a count of COUNT on pos 10,10,10
----now, this part is trying to set the "item" unique id to 100:
item.uid=100
if you still didn't get it, the variable item is the item created by the function doCreateItem(), for example, if I create a gold coin with it, the variable "item" represents this coin. if i'm right - and I think i am - the structure of the item has one field called uid, which represents the unique id of the item. the command "item.uid=100" should set it to 100.
 
Okay I understand you now, thank you. But do I make create a local function like

local function create(cid, storage, item, pos)

The only reason I put storage in there is because I'm using a storage value for when the rock respawns

Or.... do I just do like
if isPlayer(cid)
then create
return true
end

Still a bit fuzzy on this, sorry
 
you don't need the conditional if you don't actually have a condition lol.
i mean, what is the condition that must have the value TRUE in order to create that item?
I've written a short explanation if you want to read, if not, skip it:
to ilustrate that situation better, let me use an example:

this script represents a quest:
function onUse(cid,item)
if getPlayerStorageValue(cid,1000)~=1 then
doPlayerAddItem(cid,ITEM_ID,COUNT)
setPlayerStorageValue(cid,1000,1)
else
doPlayerSendCancel(cid,"You have already done this quest.")
end
end
Following the collors i've used to make it better:
RED: Obviously, the normal quests on tibia can only be done once. we use the storage values to see if a player has done a quest already or not. the default value for storages is -1, and this particular script uses 1 representing that the player has already done it.
I wanted to put some enphasis on the IF. You can make scripts without ifs, i'll show another example later.
The red line checks the truth value of a statement. the statement is (getPlayerStorageValue() ~= 1) and ~= means different. The computer processes this string, and, let's say the value is -1 (default).
The string now is (-1 ~= 1). it's true, so the truth value of this string is true. the if command will do the 'then' part if the statement is true. now let's say the player has already done this quest, the value would be 1. (1 ~= 1) is a false statement, so the computer breaks the if and go to the elseifs or the else.
BLUE: the blue part is the control variable being updated. if the storage wasn't 1 when the player used the chest, now it is.

now, an example of how you could make a simmilar code:

function onUse(cid,item)
doPlayerAddHealth(cid,100)
doTeleportThing(cid,{x=13,y=13,z=13})
end

there is no condition to see what part of the code will be processed, so it is done in the order the commands are written.
In this example, the 'control' that the scripter uses to avoid abuse (clicking repeatedly) is teleporting the player away, i don't know any good examples of the use of this code, but you got it.

--------------------------------------------------------------------------------------------------------------

Now you have to think, what must happen in order to have this function to be processed? maybe you need 'cid' to be a player, maybe you need a timer or a storage value, it's totally up to you. Also, it can be nothing, so no if will be used.
 
Alright, so when I have this script:
Code:
local bigrock = 8634
local smallrock = 8638
local rock = 4001 --put unique id of rock here
local respawnminimum = 1000 
local respawnmaximum = 5000

local posa = {x=1094,y=1060,z=6}

local rand = math.random(0,10000)

create = doCreateItem(bigrock,1,posa)
create.uid=4001 


local function restore(cid, storage)
	setPlayerStorageValue(cid, rock, 1)
		doRemoveItem(getTileItemById(posa, smallrock).uid)
		doCreateItem(bigrock,1,posa).uid = 4001
			return true
end	

local function goto(cid)
	addEvent(restore, 1000,5000, cid)
		return true
end
	
function onUse(cid, item, frompos, item2, topos)
if item.itemid == bigrock and
		 getPlayerStorageValue(cid, rock) < 2
		then	
			doRemoveItem(getTileItemById(posa, bigrock).uid)
			addEvent(goto, 1, 5, cid)
			setPlayerStorageValue(cid, rock, 2)
			doSendMagicEffect(getPlayerPosition(cid), 13)	
			doCreatureSay(cid, "You mined it!", TALKTYPE_ORANGE_1)
			
elseif
	getPlayerStorageValue(cid, rock) == 2
		then
			doCreatureSay(cid, "This rock has already been mined, wait for it to grow back.", TALKTYPE_ORANGE_1)		
				return true
			

			end
				return true

			end
in my server, why does my server close itself out halfway through loading? Is my function goto wrong? I dont know what to write for addevent, is it (cid, timer) or something? Anyway, when I moved this file and deleted it from actions.xml my server ran just fine so..... i dont get it
 
Well dont use functions that create objects or get game object (properties or info) out of a function. You know server start loading by getting sql connection then loading the lua scripts which at this point the map wasnt loaded neither any object in game, so when you put a function that requires the game to be fully loaded server will crash because game isn't up yet.

Like this one you have in your script
Lua:
create = doCreateItem(bigrock,1,posa)  -- removing this line and putting it in a appropriate function will fix the crash
 
If you have a couple scripts malfunctioning, the server itself shouldnt close, only show a few error messages. Try doing what doggy said and put these variables as non-local inside the onUse(), if the other functions don't work just add the variables inside each of them. This is because some programs treat every function in a different instance, making the variables exclusive to that function. Maybe lua doesnt have this specific problem but if it does, add the variables to every function
 
lightblue1.lua
Code:
function onUse(cid, item, frompos, item2, topos)

function restore(cid, storage)
		doRemoveItem(getTileItemById(posa, smallrock).uid)
		doCreateItem(bigrock,1,posa).uid=4001
			return true
end	

function goto(cid)
	addEvent(restore, 1000,5000, cid)
		return true
end

bigrock = 8634
smallrock = 8638
rock = 4001
posa = {x=1094,y=1060,z=6}
rand = math.random(0,10000)

if item.itemid == bigrock then	
			doRemoveItem(getTileItemById(posa, bigrock).uid)
			doCreateItem(smallrock,1,posa)
			addEvent(goto, 1, 5, cid)
			doSendMagicEffect(getPlayerPosition(cid), 13)	
			doCreatureSay(cid, "You mined it!", TALKTYPE_ORANGE_1)		
				return true
			end
				return true
			end

Okay, this script works exactly how I want it to, except for the part about recreating the object with a unique ID. None of you guys have actually clearly told me how to do it, why beat around the bush? Just..... how do I do "docreateItem(bigrock.uid=4001,1,posa) or whatever? I tried it as you can see at the top of the script and it did not work. Can somebody PLEASE just TELL ME how to do it instead of just saying stuff close to it that I can't decipher.

- - - Updated - - -

Will somebody please just give me a straightforward answer?

I tried this:

doCreateItem(bigrock(uid=4001),1,posa)

But it did not work either, so how do you create an object with a unique ID.
 
what if you use a script that checks if there is the items you want it to switch from like small to big rock cheak for those items and if true then it sets the uid to what you want it as im noob just trying to learn would that work at all
 
Well the only problem, JohnJohn, is that I never really found out a way to set unique ID's on new items created.

But using action ID's is a much better idea for this system so you should go with that if your trying to do the same type of thing.
 
i was just asking i have never wrote a script just a copy and paste artist lol but im slowly putting togather the concept of the how to part of scripts. Any tutorials on how to become a script writer that you could suggest
 
Back
Top