Case

Is there a "case / switch " function in Cidea, I couldn’t find one in the reference.
I need to change between different behaviors.

Nope, sorry

use if statements. ugly. but does the job too.

The best way to implement a switch statement is to use a table.
eg

local switchTable = {
  optionA = function() .... end,
  optionB = function() .... end,
  optionC = function() .... end,
  optionD = function() .... end,
}

myOption = "optionA"   -- note quotes to make myOption a string

switchTable[myOption]()

Should work with integer values too, although I’ve never tested it - however you have to remember that arrays in Lua start at one not zero

eg

local integerSwitchTable = {
   function() print("foo") end,
   function() print("bar") end,
   function() print("hello") end,
   function() print("world") end,
}

integerSwitchTable[1]() -- foo
integerSwitchTable[4]() -- world

Best way to do it is to really consider why you wanted a case statement in the first place. Your desired logic can probably be achieved some other way. In real world coding outside of Lua I seldom hit a need for case/switch statements if I’m doing reasonable OO.

Integers do work with tables, but tables don’t “start” at one. They can start at whatever number you like (you can use negatives if you like); however, the # operator gives the amount of keys starting at 1 and going up without interruption (to simplify it), so one is usually chosen as the starting point to simplify things.

True - but I was just keeping the code simple, as when you use tables like this you can think of them as arrays (which they’re not really).

Also @spacemonkey - using tables like this is great for finite state machines, which is something that anyone looking into coding AI for games should be aware of.

@alexNaumov the lua equivalent to case is

if x==a then
elseif x ==b then
elseif x==c then
end

Thank you for all your advice.

I do indeed try to code autonomous vehicles, which will be changing behavior. And I will use the table as you have advised.

Thank you very much.

@TechDojo , absolutely, but I wouldn’t use a case statement in other languages for finite state machines, I’d use something more like your table approach or whatever state → function/class/whatever the language let me do :wink:

When I used to do assembler coding years ago having a list of pointers to functions which is used as a jump table was exactly the way I used to do it.

Then storing arrays of function pointers in C gave me the same functionality. However one (ab)use of the switch statement in C is the ability to “drop through” to the next case if you don’t put a break at the end of the block, carefully used it can allow for some elegant code solutions where lot’s of nested if’s can get a bit messy (IMHO)

It’s interesting to note that a lot more modern languages allow ranges on their switch statements.

Class Selectcase


--# Main
-- SelectCase
 
 
function setup()
end
 
function draw()
Selectcase(2)
end
 
--# Selectcase
Selectcase = class()
 
 
function Selectcase:init(option)
mySelect = {self.one, self.two, self.three, self.four}
 
local myFunc = mySelect[option]
if myFunc ~= nil then
myFunc()
else
self.defaultFunc()
end
end
 
function Selectcase:one()
print("This is one")
end
function Selectcase:two()
print("This is two")
end
function Selectcase:three()
print("This is three")
end
function Selectcase:four()
print("This is four")
end
function Selectcase:defaultFunc()
print("This is the default function")
end