Thread Tools Display Modes
04/24/14, 05:40 PM   #1
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
Anyone Know the Lua for Random Numbers in ESO????

I have my code working pretty good the only line of code I can't find or figure out is how to get a random number to pop in.

I'm creating just a simple dice roller for my first addon. The math.random() doesn't seem to work so I even messed around with another random code but couldn't seem to plug it in either.

Lua Code:
  1. local Roll = 0
  2.  
  3. function LootDiceRoll()
  4.     math.random(100)
  5. end
  Reply With Quote
04/24/14, 05:57 PM   #2
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
math.random() returns a value that you're not capturing.

  Reply With Quote
04/24/14, 06:11 PM   #3
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
Originally Posted by archpoet View Post
math.random() returns a value that you're not capturing.

How would I capture it exactly I'm currently using the tutorial code and was trying to capture it in the counter box where the counter used to be.
  Reply With Quote
04/24/14, 06:33 PM   #4
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
Your question is related more so to a programming fundamental in most languages, than to the ESO API. That is: to capture the return value of any function it is typical to assign it to a variable. Lua is no different in this regard.

Something like:

Code:
local num = math.random(100)
...would suffice to assign the return value of math.random() to the variable num, however once stored in a variable it then needs to be returned from the LootDiceRoll() function you've made.

Code:
return num
... and finally, inserted into the game UI.

Code:
Roll = LootDiceRoll()
YourXMLElement:SetText( Roll )
Good Luck!
  Reply With Quote
04/24/14, 06:53 PM   #5
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
I think I understand a little better now. But I'm getting an error. I'll go head and link my xml and lua. If you can see it I can't seem to understand where my error is at. I only get the error when I click on the area that the dice roll is supposed to occur.

XML

Lua Code:
  1. <GuiXml>
  2.     <Controls>
  3.         <TopLevelControl name="LootDice" mouseEnabled="true" movable="true">
  4.             <Dimensions x="200" y="42" />
  5.             <Anchor point="CENTER" />
  6.                                  
  7.             <OnMouseDown>
  8.                 Roll = LootDiceRoll()
  9.                 LootDice:SetText(Roll)
  10.             </OnMouseDown>
  11.  
  12.             <Controls>
  13.                 <Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" />
  14.                 <Label name="$(parent)Roll" font="ZoFontWindowTitle" color="CFDCBD" wrapMode="ELLIPSIS" verticalAlignment="CENTER" text="LootDice: ">
  15.                     <AnchorFill />
  16.                 </Label>
  17.             </Controls>
  18.         </TopLevelControl>
  19.     </Controls>
  20. </GuiXml>

LUA
Lua Code:
  1. local Roll = 0
  2.  
  3. function LootDiceRoll()
  4.     local num = math.random(100)
  5.     return num
  6. end

and here are pictures of the addon and the error when I get it.
Attached Thumbnails
Click image for larger version

Name:	LootDice.jpg
Views:	560
Size:	58.2 KB
ID:	186  Click image for larger version

Name:	LootDiceError.jpg
Views:	641
Size:	94.5 KB
ID:	187  
  Reply With Quote
04/24/14, 07:04 PM   #6
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
I think I see the issue you're having.

LootDice is not the type of element that has the :SetText() method.
Labels do however (i.e. $(parent)Roll,) so try LootDiceRoll:SetText(Roll)

Oh yeah, also... move the

Code:
            <OnMouseDown>
                Roll = LootDiceRoll()
                LootDiceRoll:SetText(Roll)
            </OnMouseDown>
to a <controls> inside <label> or just do that in the Lua

Code:
LootDiceRoll:SetHandler( "OnMouseDown", function()
    local Roll = LootDiceRoll()
    LootDiceRoll:SetText(Roll)
end)

Last edited by archpoet : 04/24/14 at 07:07 PM.
  Reply With Quote
04/24/14, 07:08 PM   #7
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
Furthermore, you might have a namespace conflict.

You have an element named "LootDiceRoll" and a function named the same.
  Reply With Quote
04/24/14, 07:50 PM   #8
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
Ok I've been at this a while lol. Trying different things but nothing is seeming to work. I will link the code how I have it now. If you can could you fix the error and then link the entire code back to me exactly how I have the code here with the fix in it? This will help me better understand where exactly I'm messing up.
XML
Lua Code:
  1. <GuiXml>
  2.     <Controls>
  3.         <TopLevelControl name="LootDice" mouseEnabled="true" movable="true">
  4.             <Dimensions x="200" y="42" />
  5.             <Anchor point="CENTER" />  
  6.            
  7.             <OnMouseDown>
  8.                 Roll = LootDiceRoll()
  9.                 LootDiceRoll:SetText(Roll)
  10.             </OnMouseDown>
  11.            
  12.             <Controls>
  13.                 <Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" />
  14.                 <Label name="$(parent)Roll" font="ZoFontWindowTitle" color="CFDCBD" wrapMode="ELLIPSIS" verticalAlignment="CENTER" text="LootDice: " >
  15.            
  16.                     <AnchorFill />
  17.                
  18.                 </Label>
  19.             </Controls>
  20.         </TopLevelControl>
  21.     </Controls>
  22. </GuiXml>

LUA
Lua Code:
  1. function LootDiceRoll()
  2.     local num = math.random(1,100)
  3.     return num
  4. end
  Reply With Quote
04/24/14, 08:29 PM   #9
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
Yep, namespace conflict: confirmed.

Basically I copied and pasted your code as written and just changed the name of the function to RollDice() in both the lua and the xml and it worked no issue.



Code:
function RollDice()
    local num = math.random(1,100)
    return num
end
Code:
            <OnMouseDown>
                Roll = RollDice()
                LootDiceRoll:SetText(Roll)
            </OnMouseDown>
In my opinion this is not an issue in strict Lua, (nor do other strongly typed interpreted languages suffer from this issue.)
I feel like this arises because the Game compiles the Lua and the XML elements into globally scoped lexical objects. Which in context makes sense why they would apparently conflict on names.

Last edited by archpoet : 04/24/14 at 08:35 PM.
  Reply With Quote
04/24/14, 08:49 PM   #10
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
It's ALIVE IT'S ALIVE lol ty so much for the help now time for the banana dance
  Reply With Quote
04/24/14, 08:56 PM   #11
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
Originally Posted by zireko View Post
It's ALIVE IT'S ALIVE lol ty so much for the help now time for the banana dance
LOL. My pleasure, glad to help.
  Reply With Quote
04/24/14, 09:00 PM   #12
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
Now I got to figure out how to make it where when you move it somewhere it will save to that location. That way people don't have to move it every time the load up the game or reload the ui. Any ideas where I should start on that. Links are welcomed. O and thank you for all the help again.
  Reply With Quote
04/24/14, 09:14 PM   #13
archpoet
Cunning Linguist
 
archpoet's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 19
Originally Posted by zireko View Post
Now I got to figure out how to make it where when you move it somewhere it will save to that location. That way people don't have to move it every time the load up the game or reload the ui. Any ideas where I should start on that. Links are welcomed. O and thank you for all the help again.
First of all you'll need to setup ZO_SavedVars to save and reload the coordinates.. some info about that here:
http://wiki.esoui.com/AddOn_Quick_Questions

Then you'll want to set a default position on the screen, and OnLoad position the addon there.

Next you'l want to:
Code:
LootDice:SetHandler( "OnMoveStop", function()
     -- overwrite default position with the updated position of your addon here
end)
Then when you load the SavedVars again next time instead of using the default positioning, it will load and be positioned in the saved location.

So in summary:

1. Setup Defaults
2. Load SavedVars
3. Do Positioning, Processing, etc.
4. ??
5. Profit.

Happy to Help!
  Reply With Quote
04/25/14, 08:48 AM   #14
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
K I think I have it but the game is in Maintenance currently. Here is the code I have so far.

Lua Code:
  1. LootDice:SetHandler( "OnMoveStop", function()
  2.      -- overwrite default position with the updated position of your addon here
  3. end)
  4.  
  5. local savedVars = ZO_SavedVars:New(savedVariableLootDice, 1, defaults)
  6.  
  7. local defaults =
  8. {
  9.     offsetX = 0,
  10.     offsetY = 0,
  11. }
  12.  
  13. function RollDice()
  14.     local num = math.random(1,100)
  15.     return num
  16. end
  17.  
  18. EVENT_MANAGER:RegisterForEvent("LootDice", EVENT_ADD_ON_LOADED, OnAddOnLoaded)
  Reply With Quote
04/25/14, 09:00 AM   #15
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
K I just got into the game and I know something is wrong I flipped the script around and that made my addon work again but it still doesn't save it and I get an error. Here is what the script I have looks like now.

Lua Code:
  1. function RollDice()
  2.     local num = math.random(1,100)
  3.     return num
  4. end
  5.  
  6. EVENT_MANAGER:RegisterForEvent("LootDice", EVENT_ADD_ON_LOADED, OnAddOnLoaded)
  7.  
  8. LootDice:SetHandler( "OnMoveStop", function()
  9.      -- overwrite default position with the updated position of your addon here
  10. end)
  11.  
  12. local savedVars = ZO_SavedVars:New(savedVariableLootDice, 1, defaults)
  13.  
  14. local defaults =
  15. {
  16.     offsetX = 0,
  17.     offsetY = 0,
  18. }
  Reply With Quote
04/25/14, 10:06 AM   #16
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
If this is your entire addon code, then you are missing OnAddOnLoaded function. You should call ZO_SavedVars:New in that function.
  Reply With Quote
04/25/14, 11:10 AM   #17
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
Ok I took and cleaned up the code and have it looking nice. Now I'm getting an error. Here is the code so far if you can see where I'm messing up let me know. Please show code back to me since I'm new it helps me out a lot because I'm still learning.

Lua Code:
  1. function RollDice()
  2.     local num = math.random(1,100)
  3.     return num
  4. end
  5.  
  6. EVENT_MANAGER:RegisterForEvent("LootDice", EVENT_ADD_ON_LOADED, OnAddOnLoaded)
  7.  
  8. LootDice:SetHandler( "OnMoveStop", function())
  9.     local function OnAddOnLoaded(eventCode, LootDice)
  10.     if(LootDice == "<<LootDice>>") then
  11.         local savedVars = ZO_SavedVars:New(LootDice_SavedVariables, 1)
  12.     end
  13. end
  Reply With Quote
04/25/14, 11:41 AM   #18
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
I've been playing around with it a lot. I think this is the code that is messed up but I'm not sure how to fix it exactly.

Lua Code:
  1. LootDice:SetHandler( "OnMoveStop", function())
  Reply With Quote
04/25/14, 11:48 AM   #19
Fathis Ules
Thief Guild Master
 
Fathis Ules's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 42
Originally Posted by zireko View Post
I've been playing around with it a lot. I think this is the code that is messed up but I'm not sure how to fix it exactly.

Lua Code:
  1. LootDice:SetHandler( "OnMoveStop", function())
It is

Lua Code:
  1. LootDice:SetHandler( "OnMoveStop", function() --[[ CODE GOES HERE]] end)

Assuming LootDice is an existing control of course

Also think when you declare something

Lua Code:
  1. function RollDice()
  2.     local num = math.random(1,100)
  3.     return num
  4. end

it is called a global function and may conflict with other global function used by other addons or the client itself

Example if you write

Lua Code:
  1. function d()
  2. end

you will overwritte the d() global function and many message of the client or addons won't be processed anymore

it is better to default to local unless you really want the function accessible globally like that

Lua Code:
  1. local function RollDice()
  2.     local num = math.random(1,100)
  3.     return num
  4. end

And if you really want it accessible globally usually you put a better naming like MyAddonRollDice so you are sure there is near 0 confict with it

Last edited by Fathis Ules : 04/25/14 at 11:54 AM.
  Reply With Quote
04/25/14, 12:02 PM   #20
zireko
 
zireko's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 63
When I set it to local function instead of just function it gives and error but when its a global it works for some odd reason. I'm testing and moving code all around but here is the entire code lua, xml, and txt. So you can see exactly what I'm working with.

LUA - I have the save part of the code commented out so I could test the local.

Lua Code:
  1. local function RollDice()
  2.     local num = math.random(1,100)
  3.     return num
  4. end
  5.  
  6.  
  7. --[[
  8. LootDice:SetHandler( "OnMoveStop", function()
  9.     local function OnAddOnLoaded(eventCode, LootDice)
  10.     if(LootDice == "<<LootDice>>") then
  11.         local savedVars = ZO_SavedVars:New(LootDice_SavedVariables, 1)
  12.     end
  13. end)
  14.  
  15. EVENT_MANAGER:RegisterForEvent("LootDice", EVENT_ADD_ON_LOADED, OnAddOnLoaded)
  16. ]]--

XML

Lua Code:
  1. <GuiXml>
  2.     <Controls>
  3.         <TopLevelControl name="LootDice" mouseEnabled="true" movable="true">
  4.             <Dimensions x="150" y="42" />
  5.             <Anchor point="CENTER" />  
  6.            
  7.             <OnMouseDown>
  8.                 Roll = RollDice()
  9.                 LootDiceRoll:SetText(Roll)
  10.             </OnMouseDown>
  11.            
  12.             <Controls>
  13.                 <Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" />
  14.                 <Label name="$(parent)Roll" font="ZoFontWindowTitle" color="CFDCBD" wrapMode="ELLIPSIS" verticalAlignment="CENTER" horizontalAlignment="CENTER" text="LootDice" >
  15.            
  16.                     <AnchorFill />
  17.                
  18.                 </Label>
  19.             </Controls>
  20.         </TopLevelControl>
  21.     </Controls>
  22. </GuiXml>

TXT

Lua Code:
  1. ## Title: LootDice - By Rylew
  2. ## APIVersion: 100003
  3. ## Description: A simple Loot Dice for group runs.
  4. ## Version: 1.0
  5. ## SavedVariables: LootDice_SavedVariables
  6.  
  7. LootDice.lua
  8. LootDice.xml
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Anyone Know the Lua for Random Numbers in ESO????


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