Simplifying my "if then elseif" block using tables?

(this is under a Reload function) Basically I want this to cycle in a predictable order 1->2->3->4 down a list of damage types and the only way I got this to work is by using a long if then else statement. I was wondering how I could simplify this using tables because my previous attempts using tables have failed… Thanks



	local dtype = self.DeathType -- Shorten the name to write code easier
	if dtype == DMG_GENERIC then
		dtype = DMG_CRUSH -- Change to Crush
		choice = "crush" -- Used for chat
		chosen = true 
	elseif dtype == DMG_CRUSH then
		dtype = DMG_BULLET
		choice = "bullet"
		chosen = true
	elseif dtype == DMG_BULLET then
		dtype = DMG_SLASH
		choice = "slash"
		chosen = true
	elseif dtype == DMG_SLASH then
		dtype = DMG_BURN 
		choice = "burn"
		chosen = true
	elseif dtype == DMG_BURN then
		dtype = DMG_BLAST
		choice = "blast"
		chosen = true
	elseif dtype == DMG_BLAST then
		dtype = DMG_GENERIC 
		choice = "generic"
		chosen = true
	end



local t = {}

t[ DMG_GENERIC ] = {
    chat = "crush",
    override = DMG_CRUSH
}
// continue

local dtype = self.DeathType -- Shorten the name to write code easier
if t[ dtype ] then
	dtype = t[ dtype ].override
	choice = t[ dtype ].chat
	chosen = true 
else



I suggest you use tables in your tables, and index using dtype
[lua]local DeathType = {
[DMG_GENERIC] = {
type = DMG_CRUSH,
str = “crush”
},
[DMG_CRUSH] = {
type = DMG_BULLET,
str = “bullet”
},
– blah blah
}

local tab, chosen = DeathType[ self.DeathType ]
if tab then

local dtype = tab.type
local choice = tab.str
chosen = true

else

-- defaults

end[/lua]

Alright thanks. So there really isnt a way to just go to the next table entry in Lua?

It sounds like you want to iterate over each death-type in order, rather than just finding the info for a specific death-type.



-- Outside of all methods
local DMG_TYPES = {
    DMG_GENERIC,
    DMG_CRUSH,
    DMG_BULLET,
    ...
}

SWEP.DamageKey = 1 -- Default to the first damage type
SWEP.DamageType = DMG_TYPES[1]


-- To move to the next damage type
if DMG_TYPES[self.DamageKey + 1] then -- Check that there is a next element in the table
    self.DamageKey = self.DamageKey + 1
else
    self.DamageKey = 1 -- If there was no next element, jump back to the first
end

self.DamageType = DMG_TYPES[self.DamageKey]


Thanks! Thats what I needed