Help on lua code error

Help on lua code error

by rdrmdr » Tue Apr 05, 2016 2:28 pm

I made this simple lua code to print on screen the link of the item that my mouse is over when the code runs:
Code: Select all
TestFrame=CreateFrame("Frame")
TestFrame:SetScript("OnUpdate",function()
if (IsShiftKeyDown())then
ChatFrame1:Clear()
DEFAULT_CHAT_FRAME:AddMessage(GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
end
end)
but I get an error if I run it with the mouse outside an container (bag). Something like aqttampt to index nil and stuff...

So I need to add an if statment here:
Code: Select all
TestFrame=CreateFrame("Frame")
TestFrame:SetScript("OnUpdate",function()
if (IsShiftKeyDown())then
if (GetMouseFocus():GetParent():GetID()==nil)then
ChatFrame1:Clear()
DEFAULT_CHAT_FRAME:AddMessage(GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
end
end
end)
but it gives me the error for some reason. I don't know why... I have tried if, if not, ~=, ==, without comparing and other stuff I can't remember.
I can't see if the mouse is over an item (with the bag opened) and I can't figure if the bag is nil (GetMouseFocus():GetParent():GetID()). Someone help me please, it must be something easy.

P.S.: It's almost identicall to the blizzard code but only actives when I press shift, not shift+left click (because I coldn't figure it out :lol:). You can see it here https://www.townlong-yak.com/framexml/1.1.2/ContainerFrame.lua#413
rdrmdr
Grunt
Grunt
 

Re: Help on lua code error

by Renew » Tue Apr 05, 2016 4:02 pm

i give you a tip: try to figure out what cause the error (read error text) - try to understand why there is this error

if you write your own addons you will see hundrets of these problems and you will only learn if you solve them yourself

so
Code: Select all
DEFAULT_CHAT_FRAME:AddMessage(GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
gives you a nil when the mouse is not on an item, string was expected

you can try to bypass this problem with an IF statement

Code: Select all
TestFrame=CreateFrame("Frame")
TestFrame:SetScript("OnUpdate",function()
     if (IsShiftKeyDown()) and  GetContainerItemLink(GetMouseFocus():GetParent():GetID(),GetMouseFocus():GetID()) ~= nil then
     ChatFrame1:Clear()
     DEFAULT_CHAT_FRAME:AddMessage(GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
     end
end)



but the code itself is pretty much useless:

1. onupdate is a script that will be executed every frame (you have 60fps - it will be run 60 times per second)
2. you clear the screen and print out an item 60 times per second
my Addons:
- Vanilla Healing Assignments - /viewtopic.php?f=63&t=23326
- Vanilla Storyline -
User avatar
Renew
Senior Sergeant
Senior Sergeant
 

Re: Help on lua code error

by rdrmdr » Tue Apr 05, 2016 4:44 pm

Renew wrote:i give you a tip: try to figure out what cause the error (read error text) - try to understand why there is this error
I have tried to figure out the error but what I understand with the small message is that it nil can't be printed. That's why I'm trying to avoid the print code with the if it's nil but comparing a nil result with nil doesn't seems to work. I understand the error, I don't understand why I can't fix it with the if...

This should work but it doesn't...
Code: Select all
if (GetMouseFocus():GetParent():GetID()~=nil)then
 DEFAULT_CHAT_FRAME:AddMessage(GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
end
because the GetMouseFocus():GetParent():GetID() is nil so the if should give false and not try to print a nil value but it gives the error: attempt to index a nil value
and the weird is that the error is on the if. I really don't get it, help me :cry:

EDIT: or if not (GetMouseFocus():GetParent():GetID())then but it's the same. I know a way to turn around this problem but then I get another one (wich I'm trying to figure out now) doing if (GetMouseFocus():GetID()~=0)then
rdrmdr
Grunt
Grunt
 

Re: Help on lua code error

by rdrmdr » Tue Apr 05, 2016 5:14 pm

Well, I didn't solved the problem but I found a way to get what I was looking for (not really, I'm inventing what I want as I create more code :lol:):
Code: Select all
TestFrame=CreateFrame("Frame")
pressing=0
TestFrame:SetScript("OnUpdate",function()
 if (IsShiftKeyDown())then
  if (GetMouseFocus():GetID()~=0)then
   if (pressing==0)then
    texture, count = GetContainerItemInfo(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID())
    if (count~=nil)then
     DEFAULT_CHAT_FRAME:AddMessage(count..GetContainerItemLink(GetMouseFocus():GetParent():GetID(), GetMouseFocus():GetID()))
     pressing=1
    end
   end
  end
 elseif (pressing==1)then
  pressing=0
 end
end)

I will explain line by line as I understand it for the people that can't understand...
1 Create a frame with the name "TestFrame"
2 Create a variable equal to 0 with the name "pressing"
3 Open the code for the OnUpdate of the frame "TestFrame" (trigger the code below 60 times per second, every frame of the game)
4 Runs the code below if the shift is being pressed
5 Runs the code below if the mouse is over a slot/item (if you have the mouse out of the slots/items you will get the value 0)
6 Runs the code below if the variable "pressing" is equal to 0 (it was when we created so no problem, this is to prevent a lot of links in the chat because you don't press the shift only one frame)
7 Create a variable "count" (the texture doesnt matter, I just coldn't figure out how to do this...) that is equal to the ammount of items in the sslot (normally 1, only more if you have items stacked)
8 Runs code below if the ammount of items is different from nothing (that way I can know if I actually have an item in that slot)
9 Add a message to the chat saying the ammount of items (stacked) and the name/link of the item (but only for you, the other players don't see it on chat)
10 Changes the value of the variable "pressing" so that it won't writte more than one time when pressing shift once (line 6 blocks it ;))
11 End the if of line 8
12 End the if of line 6
13 End the if of line 5
14 If the if on line 4 is false then try this one (if your not pressing the shift and your not holding it)
15 Then the variable "pressing" equals 0, so that you can press shift again
16 End the if on line 4
17 End the always running code below line 3

I found out that line 7 actually has a bug. If I have the mouse over one of the 4 bags but not on the slot (all exept the backpack) then it will think I have it over the slot of backpack. On bag 2 thinks I'm on slot 2 of backpack but wharever... EDIT: ...And if I have the mouse over some action buttons... That's why I was trying to figure it out, you can't go around a bug and try another way, you have to find that way because there's no other way, but for some reason all of this is so well hidden over the internet and when I try to study someone else's addon it's like 1500lines and 10files. WHY!? For example, GetContainerFreeSlots doesn't work. It may have been added on an later patch, I don't know. What I mean is that the things that exist seems to have been created by the people who make addons, because it looks like all you can do with an addon is already created, you can't do more. People say it's hard to program (maybe you don't, idk) but what's really hard is to find the way the language works, because nobody wanna help you, only hint you in the right direction. You have to read and read a lot that doesn't matter because there's no famous site with all docummentaited (bad english :S) and most of the people won't even read this or try my code, they just see something they know in the code and tell me something that isn't what I asked, like an link. Don't answer me saying this is offencive because I won't care how you misunderstood what Im trying to say. If you wanna try to help me that would be great, but I can't count on this threat because I know, at the end, I have to search like crazy. I just wanted to create a good, simple addon created especially for the Nostalrius server and that wouldn't be big, or hard to read the files (and free and that kind of stuff, I'm out)
rdrmdr
Grunt
Grunt
 


Return to Addons & macros