Jump to content
tazyload

Printing item name on use

Recommended Posts

I am trying to get the name of an item when it is used and have it print to the pzdebug screen but I am not having much luck.

 

Here is how I am trying to achieve this;

function SayOnUse(player, item)  local item_name = item:getName();  pzdebug(item_name);endEvents.OnUseItem.Add(SayOnUse);

 

If anyone could help get me going in the right direction I would greatly appreciate it :)

Share this post


Link to post
Share on other sites

the console window?

 

 

Edit: :( Thought I could make it work the first try :( have to mess it with. can't work ion it right now though :(.

Edited by kinyoshi

Share this post


Link to post
Share on other sites

I never got the debug window to work.

Why not write your own? As I said in the IRC yesterday you could use this as a base:

-- =============================================================================-- COORDINATE VIEWER-- by RoboMat-- -- Created: 07.08.13 - 21:31-- =============================================================================CoordViewer = {}CoordViewer.version = "0.4.0";CoordViewer.author = "RoboMat";CoordViewer.modName = "Coordinate Viewer";CoordViewer.flag = true;CoordViewer.keyPressed = false;-- -------------------------------------------------- Functions-- ----------------------------------------------------- Prints out the mod info on startup and initializes a new-- trait.function CoordViewer.init()    print("Mod Loaded: " .. CoordViewer.modName .. " by " .. CoordViewer.author .. " (v" .. CoordViewer.version .. ")");end----- Creates a small overlay UI that shows debug info if the-- P key is pressed.function CoordViewer.showDebugger()    local tManager = getTextManager();    local player = getPlayer();    if (player ~= nil) and (CoordViewer.flag) then        local screenX = 20;        local screenY = 200;        local x = player:getX();        local y = player:getY();        local z = player:getZ();        x = CoordViewer.round(x, 1);        y = CoordViewer.round(y, 1);        z = CoordViewer.round(z, 1);        tManager:DrawString(UIFont.Small, screenX, screenY, "Coordinates ", 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 10, "X: " .. x, 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 20, "Y: " .. y, 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 30, "Z: " .. z, 1, 1, 1, 1);    endend----- Checks if the P key is pressed to activate / deactivate the-- debug menu.-- @param _keyPressed - The key which was pressed by the player.--function CoordViewer.checkKey(_keyPressed)    if _keyPressed == 25 then        CoordViewer.flag = not CoordViewer.flag; -- reverse flag    endend----- Round the values.-- @param _number - The value to be rounded.-- @param _decimal--function CoordViewer.round(_number, _decimal)    local temp = _decimal and 10 ^ _decimal or 1;    return math.ceil(_number * temp - 0.5) / temp;end-- -------------------------------------------------- Game hooks-- ------------------------------------------------Events.OnGameBoot.Add(CoordViewer.init);Events.OnKeyPressed.Add(CoordViewer.checkKey);Events.OnPostUIDraw.Add(CoordViewer.showDebugger);

Share this post


Link to post
Share on other sites

I never got the debug window to work.

Why not write your own? As I said in the IRC yesterday you could use this as a base:

=============================================================================-- COORDINATE VIEWER-- by RoboMat-- -- Created: 07.08.13 - 21:31-- =============================================================================CoordViewer = {}CoordViewer.version = "0.4.0";CoordViewer.author = "RoboMat";CoordViewer.modName = "Coordinate Viewer";CoordViewer.flag = true;CoordViewer.keyPressed = false;-- -------------------------------------------------- Functions-- ----------------------------------------------------- Prints out the mod info on startup and initializes a new-- trait.function CoordViewer.init()    print("Mod Loaded: " .. CoordViewer.modName .. " by " .. CoordViewer.author .. " (v" .. CoordViewer.version .. ")");end----- Creates a small overlay UI that shows debug info if the-- P key is pressed.function CoordViewer.showDebugger()    local tManager = getTextManager();    local player = getPlayer();    if (player ~= nil) and (CoordViewer.flag) then        local screenX = 20;        local screenY = 200;        local x = player:getX();        local y = player:getY();        local z = player:getZ();        x = CoordViewer.round(x, 1);        y = CoordViewer.round(y, 1);        z = CoordViewer.round(z, 1);        tManager:DrawString(UIFont.Small, screenX, screenY, "Coordinates ", 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 10, "X: " .. x, 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 20, "Y: " .. y, 1, 1, 1, 1);        tManager:DrawString(UIFont.Small, screenX, screenY + 30, "Z: " .. z, 1, 1, 1, 1);    endend----- Checks if the P key is pressed to activate / deactivate the-- debug menu.-- @param _keyPressed - The key which was pressed by the player.--function CoordViewer.checkKey(_keyPressed)    if _keyPressed == 25 then        CoordViewer.flag = not CoordViewer.flag; -- reverse flag    endend----- Round the values.-- @param _number - The value to be rounded.-- @param _decimal--function CoordViewer.round(_number, _decimal)    local temp = _decimal and 10 ^ _decimal or 1;    return math.ceil(_number * temp - 0.5) / temp;end-- -------------------------------------------------- Game hooks-- ------------------------------------------------Events.OnGameBoot.Add(CoordViewer.init);Events.OnKeyPressed.Add(CoordViewer.checkKey);Events.OnPostUIDraw.Add(CoordViewer.showDebugger);

I'm only just starting out and have very little idea how your script works so writing my own version is a little out of my reach atm. I'm sure I'll figure it out though just going to take me a while. 

 

I see you are getting the player info with getPlayer() but I don't know how to do the same with an item as it is used, the only way I know atm is by calling the arguments inherited by the event the function is added to (at least thats how I think it works :S) like in the example I use in the first post.

 

Edit:

I don't mean to sound ungrateful btw, just frustrated by the limitations of my knowledge lol

Share this post


Link to post
Share on other sites

I haven't spent too much time looking at this, but the OnUseItem event returns the item name already, so you don't need to use getName, just print (item); 

Hope that helps.

Share this post


Link to post
Share on other sites

Oh sorry,

 

well basically all you need to do is replace the variables (x, y, z) or add new ones. But I use this more for values that are constantly changing.

 

local x = player:getX();local y = player:getY();local z = player:getZ();x = CoordViewer.round(x, 1);y = CoordViewer.round(y, 1);z = CoordViewer.round(z, 1);tManager:DrawString(UIFont.Small, screenX, screenY, "Coordinates ", 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 10, "X: " .. x, 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 20, "Y: " .. y, 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 30, "Z: " .. z, 1, 1, 1, 1);

 

May I ask why you want to print the item name? Why don't you use the print() function?

Share this post


Link to post
Share on other sites

Oh sorry,

 

well basically all you need to do is replace the variables (x, y, z) or add new ones. But I use this more for values that are constantly changing.

local x = player:getX();local y = player:getY();local z = player:getZ();x = CoordViewer.round(x, 1);y = CoordViewer.round(y, 1);z = CoordViewer.round(z, 1);tManager:DrawString(UIFont.Small, screenX, screenY, "Coordinates ", 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 10, "X: " .. x, 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 20, "Y: " .. y, 1, 1, 1, 1);tManager:DrawString(UIFont.Small, screenX, screenY + 30, "Z: " .. z, 1, 1, 1, 1);

May I ask why you want to print the item name? Why don't you use the print() function?

Basically I want to get the name of an item on its use so I can make the player say certain things depending on the item/condition of the food. I tried print for ages then someone kindly pointed out that it doesn't work on linux so have been trying to use pzdebug but it appears to not like printing variables, I can only get it to print "give 1x Bread" as part of my spawn command.

Share this post


Link to post
Share on other sites

Hehe well then I introduce you to the most simple way of debugging stuff in PZ apart from print: say() :D
 

UseATable = {}function UseATable.SayOnUse(_player, _item)  local itemName = _item:getName();  _player:Say(itemName);endEvents.OnUseItem.Add(UseATable.SayOnUse);

Share this post


Link to post
Share on other sites

 

Hehe well then I introduce you to the most simple way of debugging stuff in PZ apart from print: say() :D

 

UseATable = {}function UseATable.SayOnUse(_player, _item)  local itemName = _item:getName();  _player:Say(itemName);endEvents.OnUseItem.Add(UseATable.SayOnUse);

 

Is there such thing as _item:getName()? If so, what is item?

Share this post


Link to post
Share on other sites

Is there such thing as _item:getName()? If so, what is item?

Well _item should be the parameter passed by the Event when it is fired ;) If you are wondering about the underscore "_" it's just something I use to mark parameters so I don't confuse them with local variables.

Damn you instant email-notification! why won't you let me go to bed!?

Share this post


Link to post
Share on other sites

 

Hehe well then I introduce you to the most simple way of debugging stuff in PZ apart from print: say() :D

 

UseATable = {}function UseATable.SayOnUse(_player, _item)  local itemName = _item:getName();  _player:Say(itemName);endEvents.OnUseItem.Add(UseATable.SayOnUse);

I think I broke it because the player still remains silent, I cant get anything to trigger with the OnUseItem event, 

function SayOnUse(_player)    _player:Say("testing");endfunction AddItems(_player)    if isKeyDown(Keyboard.KEY_L) then    if _player:getInventory():contains("Bread") == false then    _player:getInventory():AddItem("Base.Bread");    pzdebug("Give 1x Bread");    end  endendEvents.OnPlayerUpdate.Add(AddItems);Events.OnUseItem.Add(SayOnUse);

Even the above, which I would assume would say "testing" every time I eat something, does nothing.

Share this post


Link to post
Share on other sites

 

Is there such thing as _item:getName()? If so, what is item?

Well _item should be the parameter passed by the Event when it is fired ;) If you are wondering about the underscore "_" it's just something I use to mark parameters so I don't confuse them with local variables.

Damn you instant email-notification! why won't you let me go to bed!?

 

I mean, the event passes _item as a parameter, which is the item, so item:getName() would be like Axe:getName(), no?

 

Have to go to bed now, but will help you tomorrow tazyload. I suggest checking out the examples on the pzmods website though.

Share this post


Link to post
Share on other sites
I mean, the event passes _item as a parameter, which is the item, so item:getName() would be like Axe:getName(), no?

No it's not an axe... to the program itself it is just an InventoryItem with the stats and name of an axe. You know? The name isn't important.

 

 

I think I broke it because the player still remains silent, I cant get anything to trigger with the OnUseItem event, 

I haven't used that event yet, but maybe it doesn't give you the actual IsoPlayer but just the number of the player (0 or 1).

 

Try:

getSpecificPlayer(_player):Say("Stop! Hammertime!");

Share this post


Link to post
Share on other sites

 

I mean, the event passes _item as a parameter, which is the item, so item:getName() would be like Axe:getName(), no?

No it's not an axe... to the program itself it is just an InventoryItem with the stats and name of an axe. You know? The name isn't important. 

 

 

 

That's what Enigma said...

 

I just thought that the event would only return the name, not the full item object.

Share this post


Link to post
Share on other sites

 

 

I mean, the event passes _item as a parameter, which is the item, so item:getName() would be like Axe:getName(), no?

No it's not an axe... to the program itself it is just an InventoryItem with the stats and name of an axe. You know? The name isn't important. 

 

 

 

That's what Enigma said...

 

I just thought that the event would only return the name, not the full item object.

 

 

No the event should return the InventoryItem - so you should be able to use all respective functions.

 

If you use print(item) it may be, that the toString() java method is called which in turn might return the actual item name, but that's just speculation.

Share this post


Link to post
Share on other sites

 

I mean, the event passes _item as a parameter, which is the item, so item:getName() would be like Axe:getName(), no?

No it's not an axe... to the program itself it is just an InventoryItem with the stats and name of an axe. You know? The name isn't important.

 

 

I think I broke it because the player still remains silent, I cant get anything to trigger with the OnUseItem event, 

I haven't used that event yet, but maybe it doesn't give you the actual IsoPlayer but just the number of the player (0 or 1).

 

Try:

getSpecificPlayer(_player):Say("Stop! Hammertime!");

Just tried this and still no luck, still its a new day so more hours for me to beat my head against this problem :D

Share this post


Link to post
Share on other sites

No wonder it doesn't work. "OnUseItem" has been removed and is no longer available as an event.

 

EDIT:

 

After talking to RobertJohnson about the issue, he told me that "OnUseItem" was not fired when the item was "used" from the inventory but only when it was dragged around. It was removed, because it was never actually used :)

 

I already posted and suggested a new OnItemUse Event in the lua requests thread ;)

Share this post


Link to post
Share on other sites

No wonder it doesn't work. "OnUseItem" has been removed and is no longer available as an event.

 

EDIT:

 

After talking to RobertJohnson about the issue, he told me that "OnUseItem" was not fired when the item was "used" from the inventory but only when it was dragged around. It was removed, because it was never actually used :)

 

I already posted and suggested a new OnItemUse Event in the lua requests thread ;)

Marvellous, I was worried I was doing something really wrong and was just too blind to see it. Glad it is being looked at too because I have a lot of use for it :D

 

Edit:

Marked as problem solved, thanks for your help everyone :)

Share this post


Link to post
Share on other sites

Also, does anyone know what the blocker is for print() not working on Linux?  Is it something they could add like a command line verbose flag for or the like?  Because I've found a lot of stuff where it isn't even my print() command that clues me into what is going on wrong, but the standard debug statements from Lua or the game itself.

Share this post


Link to post
Share on other sites

Well that's good to know :)

 

What was the original use case?  Is it something you can hang off a context window instead?

I believe it used to be used when you could interact with items by dragging them over the heart icon. How I could forget this is awesome in itself.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...