• 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 Using Item() in a Creaturescript [TFS 1.1]

imkingran

Learning everyday.
Premium User
Joined
Jan 15, 2014
Messages
1,317
Solutions
35
Reaction score
434
Hey Guys,

Is it possible to do something like this?

action script:
Code:
function onUse(player, item, fromPosition, itemEx, toPosition)
    --> Run a DB Query to store the Item's Unique ID
    db.query("UPDATE `players` SET `uniqueItem` = "..itemEx.uid.." WHERE `id`  = "..player:getGuid()..";")
return true
end


Code:
function onLogin(player)
    local item = 0
    local result_plr = db.storeQuery("SELECT `uniqueItem` FROM `players` WHERE `id` = "..player:getGuid()..";")
    if(result_plr ~= false) then
        item = Item(result.getDataInt(result_plr, "uniqueItem"))
        result.free(result_plr)
    end

    Item(item):getPosition():sendMagicEffect(CONST_ME_FIREWORK_RED)
end

What I'm trying to ask is:
Is it possible to use Item(item.uid) in a creaturescript if I store the item's unique ID in the database through the action script and then call the same unique id in the creaturescript. So far it seem as though it's not possible (per my own tests), however maybe someone with more knowledge and experience knows better.

Thanks for your time and help. :)

p.s.
If I was unclear of what I'm trying to accomplish just please let me know and I'll try to clarify what my end goal is.
 
If I was unclear of what I'm trying to accomplish just please let me know and I'll try to clarify what my end goal is.
yes plz.
Because its possible the way you trying to do it (trough database queries), but I think there is much easier way to accomplish same result with less.. work..
 
yes plz.
Because its possible the way you trying to do it (trough database queries), but I think there is much easier way to accomplish same result with less.. work..

Thanks for responding!

Could you maybe show me a simple example of the way you would go about calling Item(uid) in a creaturescript?
 
Doesn't the unique id always change? :eek:

It does!

So I tried this instead:
Code:
function onUse(player, item, fromPosition, itemEx, toPosition)
    --> Assign the item a constant UID
    Item(itemEx.uid):setAttribute(ITEM_ATTRIBUTE_UNIQUEID, player:getGuid() + 5000)
return true
end

function onLogin(player)
    local item = (player:getGuid() + 5000)
    print(Item(item):getName())
end

Still no luck :(
 
Can I ask what are you trying to achieve? I mean, I know that you want to get the item object in the creaturescript, but for what?
 
In that case I think you can store the item in a lua table, but you'll need to create the table in a global/lib file.

Well, let me add a little more info, maybe you'll give me the same answer or maybe you'll give me another:

In the creaturescript if I print(item) it will show the sum of 5000 + player:getGuid() (as it should), so it's getting the right UID but I think it's just not possible.

The reason I say it is because I have a movements script (Teleport to open trainer), that is essentially doing the same thing (in regards to calling a UID) and it's working just fine. I think it's because there is not an "item" parameter in the function ModalWindow.

function onStepIn(player, item, position, fromPosition)
function onModalWindow(player, modalWindowId, buttonId, choiceId)

But then again, my understanding is like a drop of water in the Pacific Ocean. What do you think about that?
 
What exactly are you trying to do? Which item should it get?
In function onStepIn item is the item you walk/step on.
 
What exactly are you trying to do? Which item should it get?
In function onStepIn item is the item you walk/step on.

Player uses a Key on the item he/she wishes to upgrade.

I'm trying to save the itemEx.uid and use it in the creaturescript part of the ModalWindow.

The things that I need to do to the item in the creaturescript are:
Code:
Item(uid):setAttribute('name', "Hi")
 
With the way I told you, you can achieve that without problem (Or I least I think so o_O).

1.- Add a empty table to your global.lua.
Code:
imkingran = {}
2.- On the action script save the item/target uid to that table.
Code:
imkingran[player.getId()] = {
item = target.uid
}
3.- Check that table in your creaturescript or wherever you want to.
Code:
Item(imkingran[player:getId()].item):setAttribute("name", "hi")
Of course you need to check if the table/values exist.

And sorry about the tabbing, but seems like you can't tab in the textbox of the reply.
 
With the way I told you, you can achieve that without problem (Or I least I think so o_O).

1.- Add a empty table to your global.lua.
Code:
imkingran = {}
2.- On the action script save the item/target uid to that table.
Code:
imkingran[player.getId()] = {
item = target.uid
}
3.- Check that table in your creaturescript or wherever you want to.
Code:
Item(imkingran[player:getId()].item):setAttribute("name", "hi")
Of course you need to check if the table/values exist.

And sorry about the tabbing, but seems like you can't tab in the textbox of the reply.

Thanks, I'll try it out as soon as I get back home and let know how it went!

@Colors
______________
Post Testing:
Essentially it's the same thing as my first post. The UID is constantly changing so it returns a NIL value when I'm trying to call it again in the creaturescript.

Even when I set it again to be a constant UID in the action script:
Code:
Item(itemEx.uid):setAttribute(ITEM_ATTRIBUTE_UNIQUEID, player:getGuid() + 5000)
STORE_UPGRADE[player:getGuid()] = {item = itemEx.uid}
print(itemEx.uid) -- prints: 5009
print(Item(itemEx.uid)) -- prints: userdata: 0x41c98bb8
local storageUID = STORE_UPGRADE[player:getGuid()].item
print(storageUID) -- prints: 5009
print(Item(storageUID)) -- prints: userdata: 0x41b5de18

In the creaturescript I still get a nil value:
Code:
local item = (STORE_UPGRADE[player:getGuid()].item)
Item(item):setAttribute('name', nam)
print(item) -- prints: 5009
print(Item(item)) -- prings: nil


It's most likely me being dumb. Am I doing something wrong? :oops:
 
Last edited:
eh?
You want to say "Hi" to player when he logs in and he he has upgraded item?

Why just not set action id to item or invisible text with player id ? (if you want the upgraded item only work for the one who upgraded item)
 
eh?
You want to say "Hi" to player when he logs in and he he has upgraded item?

Why just not set action id to item or invisible text with player id ? (if you want the upgraded item only work for the one who upgraded item)

No I do not wish to say "hi" to the player when he logs in. (nor is that written anywhere in the script :p)
Setting the item's name attribue to "hi" was just an example.
I also do not want the upgraded item to work for only that player.

What I'm trying to do is convert my upgrade system to ModalWindow.
In order to do it I need to call the itemEx.uid from the action script, in my creaturescript.

A few posts above I did some printing of the values in the console and listed the resulsts:
https://otland.net/threads/using-item-in-a-creaturescript-tfs-1-1.233657/#post-2253423

The results was that when I printed:
Item(5009) in the action script it returned some userdata.
Item(5009) in the creaturescript it returned NIL. o_O
 
With the way I told you, you can achieve that without problem (Or I least I think so o_O).

1.- Add a empty table to your global.lua.
Code:
imkingran = {}
2.- On the action script save the item/target uid to that table.
Code:
imkingran[player.getId()] = {
item = target.uid
}
3.- Check that table in your creaturescript or wherever you want to.
Code:
Item(imkingran[player:getId()].item):setAttribute("name", "hi")
Of course you need to check if the table/values exist.

And sorry about the tabbing, but seems like you can't tab in the textbox of the reply.
today when i saw this post that idea just became to me but didnt write it cus my english suck and cant test it :(
 
Item(itemEx.uid):setAttribute(ITEM_ATTRIBUTE_TEXT, tostring(player:getGuid()))

if item:getAttribute(ITEM_ATTRIBUTE_TEXT) == 'whatever you want to compare it with' then
do whatever you wanted to with that. because seems you don't want to tell us, what you are trying to do.
 
Back
Top