ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Anyone Know the Lua for Random Numbers in ESO???? (https://www.esoui.com/forums/showthread.php?t=1142)

zireko 04/24/14 05:40 PM

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

archpoet 04/24/14 05:57 PM

math.random() returns a value that you're not capturing.

:)

zireko 04/24/14 06:11 PM

Quote:

Originally Posted by archpoet (Post 5711)
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.

archpoet 04/24/14 06:33 PM

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! :)

zireko 04/24/14 06:53 PM

2 Attachment(s)
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.

archpoet 04/24/14 07:04 PM

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)


archpoet 04/24/14 07:08 PM

Furthermore, you might have a namespace conflict.

You have an element named "LootDiceRoll" and a function named the same.

zireko 04/24/14 07:50 PM

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

archpoet 04/24/14 08:29 PM

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.

zireko 04/24/14 08:49 PM

It's ALIVE IT'S ALIVE lol ty so much for the help now time for the banana dance :banana:

archpoet 04/24/14 08:56 PM

Quote:

Originally Posted by zireko (Post 5748)
It's ALIVE IT'S ALIVE lol ty so much for the help now time for the banana dance :banana:

LOL. :) My pleasure, glad to help.

zireko 04/24/14 09:00 PM

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.

archpoet 04/24/14 09:14 PM

Quote:

Originally Posted by zireko (Post 5755)
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! :)

zireko 04/25/14 08:48 AM

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)

zireko 04/25/14 09:00 AM

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. }

Harven 04/25/14 10:06 AM

If this is your entire addon code, then you are missing OnAddOnLoaded function. You should call ZO_SavedVars:New in that function.

zireko 04/25/14 11:10 AM

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

zireko 04/25/14 11:41 AM

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())

Fathis Ules 04/25/14 11:48 AM

Quote:

Originally Posted by zireko (Post 5849)
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

zireko 04/25/14 12:02 PM

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

Fathis Ules 04/25/14 12:17 PM

ha yeah you use XML so you will need globals and a naming like MyAddonRollDice

or one good practice is to have a unique global aray named after your addon like MyAddon = {}

Lua Code:
  1. MyAddon = {}
  2.  
  3. function MyAddon.RollDice()
  4.     d("hello")
  5. end
  6.  
  7. function MyAddon.d()
  8.  
  9. end
  10.  
  11. function MyAddon.whatever()
  12.  
  13. end

So then you can call in your XML the MyAddon.RollDice()

You will be able directly call locals when you will experiment doing an UI without a single xml file, with API CreateControl, but easier to start with XML, at least you see the concept of local vs globals

zireko 04/25/14 12:45 PM

Ok I fixed the name of the global function I'm using. Now you said to enter my code after LootDice:SetHandler( "OnMoveStop", function()
I'm not sure exactly what you mean. I have this so far.

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

I'm not sure if I move the function MyAddonRollDice into the LootDice:SetHandler or what. If you can could you copy the code and fix it so that I can see exactly where I'm messing up this also helps me understand a little better. I'm more of a visual learner and there are no youtube videos on eso lua yet. So I have been learning just lua. Things get more complicated when entering it into a game lol.

Fathis Ules 04/25/14 01:07 PM

When you write


Lua Code:
  1. EVENT_MANAGER:RegisterForEvent("LootDice", EVENT_ADD_ON_LOADED, OnAddOnLoaded)

the code is looking for a local function OnAddOnLoaded and your is not declared so it will never execute

you need to write the OnAddOnLoaded as any other local function outside the handler

Lua Code:
  1. local function OnAddOnLoaded(eventCode, addon)
  2.     if addon == "LootDice" then
  3.         LootDice_SavedVariables = { "lolilol", isAddOnEnabled = true} -- you don't really need the New() as long as you write ## SavedVariables: LootDice_SavedVariables in you toc .txt file, the variable is saved automatically
  4.         d("LootDice_SavedVariables.isAddOnEnabled = "..tostring(LootDice_SavedVariables.isAddOnEnabled))
  5.     end
  6. end

you don't really need the New() as long as you write ## SavedVariables: LootDice_SavedVariables in you toc .txt file, the variable is saved automatically when you declare it LootDice_SavedVariables = {} so anything you will create inside will be saved on disconnect or reloadui LootDice_SavedVariables.option1 LootDice_SavedVariables.option2 etc..

If you try the sample function I showed you and you hit /reloadui, you will see "lolilol" written in you savedvariable file and isAddOnEnabled ^^


Also no need to name MyAddonRollDice, when I wrote MyAddon that a sample to replace with the name of your addon, like LootDiceRollDice , LootDicePrint, LootDiceShow, etc etc so that the functions are globally identifiable to belong to the addon called LootDice

SpecialK 04/28/14 11:45 PM

Note this particular flavor of Lua is Havok Script that unfortunately uses the default C library not so random "rand()" function (default for a windows Lua build) at it's core.
It only has 15bits precision and has pretty bad pseudorandom properties.
It's too bad they didn't implement a nice 53bit RNG like LuaJit now has.

At any rate if you want a better RNG you might code one in pure Lua. Just keep in mind this Lua version uses doubles (64bit floats).


All times are GMT -6. The time now is 03:09 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI