+charlenni Posted February 15, 2016 Share Posted February 15, 2016 While looking to my tabs in the browser I found a short script, which I wrote as an answer on a question in a local forum. The problem is very common: you have a list of numbers, items, characters from which you want to get a random entry. Sometimes you like to have each entry once, sometimes each entry could appear often. Perhaps you want to reset this list of entries to the origianl value. But how to do this? Such list are stored in Lua into tables. I wrote a little function, which could create such a table and helps you to access the entries. The usage is very easy. To create such a table, you use the following function tabRandom = RandomObjects({101, 102, 103, 104, 105, 106}) Here I created the table with the numbers from 101 to 106, but I could have created it with items like this tabRandom = RandomObjects({itemHammer, itemNail, itemRope, itemKnife}) where itemHammer and so on are the variable names of the items. You could now access each entry with a "tabRandom[1]", "tabRandom[2]" and so on. Which is the normal way to access table entries. If you want a random entry, than you get this by entry = tabRandom:GetNext() entry = tabRandom:GetNextWithRemove() The difference between both functions is, that the later one removes the entry from the table, so that is one entry smaller. I stored the returned entry in the variable "entry". If you don't do this, you could use tabRandom:Last to access the last returned entry from the table. Additional functions are: tabRandom:IsNext() It returns "true", if there is an entry left or "false" if there is no entry. tabRandom:Add(entry) Adds an entry to the list. tabRandom:Remove(entry) Removes an entry from the list if it exists. tabRandom:Reset() Restores all entries of the original table (entries you set with the RandomObjects function). To use this RandomObjects table, you should add the content of the Lua script to the <Cartridge><Author script> part in Earwigo or save the Lua script in the folder of your Wherigo cartridge and add a require "RandomObjects" to your <Lua user directives> in Urwigo. I attach a Urwigo cartridge which demonstartes the different function, so you could see, how to use it. Perhaps someone could use it in a cartridge. And as allways, you could find the script at GitHub. Any comments are welcome. RandomObjects.zip Quote Link to comment
+Forest-Ghost Posted February 15, 2016 Share Posted February 15, 2016 Thank you for sharing this! I have been trying to work out random numbers for a clue Wherigo and this solves the problem. Thank you! Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.