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:	572
Size:	58.2 KB
ID:	186  Click image for larger version

Name:	LootDiceError.jpg
Views:	651
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

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