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:
|
math.random() returns a value that you're not capturing.
:) |
Quote:
|
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) Code:
return num Code:
Roll = LootDiceRoll() |
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:
LUA Lua Code:
and here are pictures of the addon and the error when I get it. |
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> Code:
LootDiceRoll:SetHandler( "OnMouseDown", function() |
Furthermore, you might have a namespace conflict.
You have an element named "LootDiceRoll" and a function named the same. |
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:
LUA Lua Code:
|
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() Code:
<OnMouseDown> 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. |
It's ALIVE IT'S ALIVE lol ty so much for the help now time for the banana dance :banana:
|
Quote:
|
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.
|
Quote:
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() So in summary: 1. Setup Defaults 2. Load SavedVars 3. Do Positioning, Processing, etc. 4. ?? 5. Profit. Happy to Help! :) |
K I think I have it but the game is in Maintenance currently. Here is the code I have so far.
Lua Code:
|
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:
|
If this is your entire addon code, then you are missing OnAddOnLoaded function. You should call ZO_SavedVars:New in that function.
|
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:
|
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:
|
Quote:
Lua Code:
Assuming LootDice is an existing control of course Also think when you declare something Lua Code:
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:
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:
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 |
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:
XML Lua Code:
TXT Lua Code:
|
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:
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 |
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:
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. |
When you write
Lua Code:
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:
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 |
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