Thread Tools Display Modes
12/17/14, 12:00 AM   #1
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
SellAllJunk()

If I was trying to prevent junk items from being sold like this:
(or is there an easier way to prevent any type of item junk or not junk from being sold)

Lua Code:
  1. local OrigSellAllJunk = SellAllJunk
  2. function SellAllJunk()
  3.    d("SellAllJunk")
  4. end

It works when you manually call: SellAllJunk()
But it does not work when you use the keybindStrip button for Sell All Junk, even though it looks to me like its calling the same function?

Keybind Descriptor Code from storewindow.lua:
Warning: Spoiler



Dialog Code from ingamedialogs.lua:
Warning: Spoiler


It looks like its calling:
Lua Code:
  1. callback =  SellAllJunk,

and I know my SellAllJunk() is not getting overwritten because it works when called manually. So how is the junk getting sold?

Its not going through: SellInventoryItem(...) either, this one works
Lua Code:
  1. local OrigSellInventoryItem = SellInventoryItem
  2. function SellInventoryItem(_iBagId, _iSlotId, _iQuantity)
  3. d("SellInventoryItem")
  4.     local slotData = SHARED_INVENTORY:GenerateSingleSlotData(_iBagId, _iSlotId)
  5.     if slotData.FilterIt_CurrentFilter and slotData.FilterIt_CurrentFilter ~= FILTERIT_VENDOR then
  6.         ZO_Alert(UI_ALERT_CATEGORY_ERROR, SOUNDS.NEGATIVE_CLICK, "Cannot sell item."..(tPreventionStrings[slotData.FilterIt_CurrentFilter] or "Unknown Reason "..slotData.FilterIt_CurrentFilter))
  7.         return
  8.     end
  9.     OrigSellInventoryItem(_iBagId, _iSlotId, _iQuantity)
  10. end

Anyone have any ideas or a better way to do it?
  Reply With Quote
12/17/14, 07:50 AM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
When you click button in the dialog, it calls callback function. As you can see, callback is a copy of SellAllJunk function. Dialog was defined before you have redefined SellAllJunk and it means that callback is a copy of the original function.
To make it work just update callback function after SeallAllJunk is redefined.
Lua Code:
  1. local OrigSellAllJunk = SellAllJunk
  2. function SellAllJunk()
  3.     d("SellAllJunk")
  4. end
  5.  
  6. ESO_Dialogs.SELL_ALL_JUNK.buttons[1].callback = SellAllJunk


Just a note:
If you assign vairable1 = variable2, you always creating a copy of the variable's value. This is true for, numbers, strings, ... and even for functions. There are only two exceptions - "table" and "userdata". It is because those two types of variables does not hold the value but reference (or pointer) to the object in memory. So if you assign table1 = table2, you are copying just reference to the object, not object itself.

Last edited by Garkin : 12/17/14 at 08:09 AM.
  Reply With Quote
12/17/14, 10:26 AM   #3
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by Garkin View Post
If you assign vairable1 = variable2, you always creating a copy of the variable's value. This is true for, numbers, strings, ... and not for functions, "table" and "userdata".
Fixed, assigning a function holds a reference to the function & closure.

Edit: Actually this whole value/reference distinction is kind of obsolete beyond C. Since there's no way to modify a string in-place in languages like Lua, Python, JavaScript etc., string copying could easily be implemented with counted references - not saying it is, too lazy to check now, but in my opinion it would be the right way to go.

Last edited by merlight : 12/17/14 at 10:36 AM.
  Reply With Quote
12/17/14, 02:40 PM   #4
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Sure it's useful for how a language behaves. It is correct if you look at the Lua manual that functions are stored as references/closures. However, when you assign a new function, you overwrite the reference not the function itself. For most users, treating this as pass-by-value is fine.

Consider this example:
Lua Code:
  1. foo = function() print "Original" end
  2. bar = foo
  3. foo = function() print "Second" end
  4.  
  5. foo()
  6. bar()

Output:
Code:
"C:\Program Files (x86)\Lua\5.1\lua.exe" functionRef.lua
Second
Original

Process finished with exit code 0
The copy (bar) is not updated when the original (foo) is changed, because you're overwriting the reference not the underlying function. Table and userdata are generally dereferenced to access individual members, so that behaves like pass-by-reference for common usage.

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

PS: Strings are implemented a bit like that already in most languages. See http://en.wikipedia.org/wiki/String_interning

Last edited by Sasky : 12/17/14 at 02:43 PM.
  Reply With Quote
12/17/14, 03:32 PM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
What I meant was that there's only one way of passing things in Lua/Python/..., and that is by reference. I don't know why us, who started with languages where it was important to understand the difference between passing by value or by reference, tend to drag that distinction to dynamic languages and try to explain it there. But in the end it unnecessarily confuses people who never learned C/Pascal/...
  Reply With Quote
12/17/14, 06:47 PM   #6
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
First, thank you for the help everyone that replied. I understood what everyone said, but that brings me to one more question.

Ok going by what Merlight said: this assigned callback a reference to SellAllJunk()
Lua Code:
  1. callback =  SellAllJunk,

So I would assume that, combined with what Garkin said, would mean that:
It creates the reference to the function BEFORE my SellAllJunk() is defined, AND when I define my SellAllJunk() it does not (not sure how to phrase it, but) overwrite the original SellAllJunk() in the same spot? I.E. mine will have a different reference?
EDIT:
I never thought about it before, but I guess that makes sense. It would have to have a different reference because I do realize that:
Lua Code:
  1. OrigSellAllJunk()
Is still useable, so the original function must still exist somewhere & have a different reference.

Last edited by circonian : 12/17/14 at 06:50 PM.
  Reply With Quote
12/17/14, 08:11 PM   #7
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I think you got it right, so I hope what I'm going to write doesn't add more confusion, since I'm making it up along the way...

You can think of the function as a black box which contains its compiled code and context. And variables are like pieces of paper with names on them that you label objects with. Every variable references one object, and there may be multiple labels (references) on the same object.

When the assignment OrigSellAllJunk = SellAllJunk is executed, it finds the object labelled SellAllJunk, and adds a new label OrigSellAllJunk to it. You can then move the SellAllJunk label to a different object (SellAllJunk = CustomSellAllJunk), and the original will still be accessible as OrigSellAllJunk.

Lua Code:
  1. function SellAllJunk() ... end
  2. -- is just syntax sugar for
  3. SellAllJunk = function() ... end

Meaning you're creating a new function (black box) and sticking the label SellAllJunk to it (removing it from the original).
  Reply With Quote
12/18/14, 09:08 PM   #8
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by merlight View Post
I think you got it right, so I hope what I'm going to write doesn't add more confusion, since I'm making it up along the way...

You can think of the function as a black box which contains its compiled code and context. And variables are like pieces of paper with names on them that you label objects with. Every variable references one object, and there may be multiple labels (references) on the same object.

When the assignment OrigSellAllJunk = SellAllJunk is executed, it finds the object labelled SellAllJunk, and adds a new label OrigSellAllJunk to it. You can then move the SellAllJunk label to a different object (SellAllJunk = CustomSellAllJunk), and the original will still be accessible as OrigSellAllJunk.

Lua Code:
  1. function SellAllJunk() ... end
  2. -- is just syntax sugar for
  3. SellAllJunk = function() ... end

Meaning you're creating a new function (black box) and sticking the label SellAllJunk to it (removing it from the original).
Makes sense, thanks for the help!
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » SellAllJunk()


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off