View Single Post
01-14-22, 10:10 AM   #7
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
That would indeed be the simplest way.

Lua tables come in 2 varieties, array style tables where you have a sequential list of values with an implicit numeric key (these can be easily sorted using table.sort) and hash style tables where keys are arbitrary (don't have to be sequential numbers)

Both have their uses and are better to use in different scenarios.

An array style table has the form of
Code:
{"oranges", "apples", "kiwis"}
which is equivalent to
Code:
{[1]="oranges", [2]="apples", [3]="kiwis"}
where we've done nothing more than show the implicit key part of the [key]=value pairs of the table.

The downside of an array style table is that while it is very easy to add new items simply
Code:
table.insert(mytable,item)
and it is also very easy to sort them in-place simply
Code:
table.sort(mytable)
you need to write code that traverses the whole table to find if an item is already present and its key or index if you wanted to remove said item with
Code:
table.remove(mytable, index)
What you would likely want to do is write some code that finds and returns the index of a specific item (player name in your case) in your table so you do not add duplicates and more importantly you can remove players that left the group (or turned their announcing option OFF)

Then when you get a message from a player that joined the group or turned their announce ON you'd simply.
1. Check they're not already in your table.
2. Add them with table.insert
3. Sort the table alphabetically with table.sort
4. Pick mytable[1] (the first element) as the one that announces.

Since your table only contains players with the addon and announce option ON there's no further communication needed (inter-addon messages) each instance of the addon in the group can decide who should announce and do it or mute itself.

You do the opposite if a player leaves or turns their announce to OFF (and sends other instances of the addon in the group a message saying so)
1. Check if they were already in your table with your function that returns their index.
2. Remove them with table.remove(mytable, index)
3. Resort the remaining entries.
4. Pick mytable[1] again as the announcer.

Last edited by Dridzt : 01-14-22 at 04:33 PM.
  Reply With Quote