Jump to content
Einlander

Simple table enumeration script

Recommended Posts

I started playing around with the networking in Leadwerks and found that I needed enums. So I wrote a function that will allow you to enumerate a table.

 

function enumerate(array,indexstart) -- indexstart is optional
	if array == nil then return nil end
	local array2 = {}
	local count = tonumber(indexstart) ~= nil and tonumber(indexstart) or 0
	for key,value in pairs(array) do
		array2[value] = count
		count = count + 1		
	end
	return array2
end

This will take an array of strings and output a named table array.

 

enum_netchat =  enumerate({
  "client",
  "team",
  "all",
  "servertoclient",
  "servertoteam",
  "servertoall"
},1)

 

This is the simplest way to make an enum

	enum_netchat.all
	

Share this post


Link to post

So this results in the below?

enum_netchat = {    
    client = 1,
    team = 2,
    all = 3,
    servertoclient = 4,
    servertoteam = 5,
    servertoall = 6
}

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...