This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LordIkol's profile picture

[Tables] Can I Improve This?

Started by LordIkol, 18 August 2013 - 07:54 AM
LordIkol #1
Posted 18 August 2013 - 09:54 AM
Hi everyone,

Im working on a script to control my turtle with chat commands. it is working good so far but i wonder if there is a better solution.
I use two tables at the moment cause i want the system to be flexible. so that you can easily change the commands for each action.
so this is how it looks at the moment.

the index of both tables is the same. you could say its the description of the command.
then the table tcom holds the Commands and tfunctions holds the function which is executed when the command is used.


local tcom = {}
tcom["up"] ="rauf"
tcom["down"]  = "runter"

local tfunctions = {}
tfunctions["up"] = moveUp
tfunctions["down"]  = moveDown

then i check the command with the tcom table and if it gets a match i call the function from the functions table with pcall.


for k,v in pairs(tcom) do
   if v == commands[2] then
	pcall(tfunctions[k],tonumber(commands[3]))
   end
  end

now i would like to know if this solution is good or if i could do it better in a different way. maybe just using one table.

another question is i heard using to many functions can make problems. is this true and if so what is too many?

thanks for your help and greets
Loki
MKlegoman357 #2
Posted 18 August 2013 - 10:46 AM
You could store the commands in one table like so:


local commands = {}
commands["up"] = moveUp
commands["down"] = moveDown

And for running the functions:


local com = "Your command"

if commands[com] then
  commands[com]()
end

This should also work (correct me if i'm wrong) a tiny bit faster as it is not looping through the table.
LordIkol #3
Posted 18 August 2013 - 11:24 AM
You could store the commands in one table like so:

-snip-

maybe i don't get your solution correct or i did not explain it correct but the point why im using this two tables is that i want to be able to change the command for "up" "down" etc. maybe its confusing cause in my example the key is the same like the command. will switch this. maybe another user wants german commands so with my solution he could simply change the tcom["up"] to tcom["up"] = "hoch" and the system would still work.

with your solution the command must be the key of the table. but i want that the user has the ability to change the command without editing the code.
theoriginalbit #4
Posted 18 August 2013 - 11:54 AM
How exactly do you intend on them being able to change the command without editing the code?
Bubba #5
Posted 18 August 2013 - 11:57 AM
So you want to have different language indexes for the same function? One cool thing that Lua tables have to offer is that they allow you to use tables as keys. This allows you to do things like:

local commands = {
  [{["hello"]=true,["hola"]=true}] = function()
	print("I can respond to hello in several different languages.")
  end;
}

local input = read()

for key,func in pairs(commands) do
  if key[input] then
	func()
  end
end
LordIkol #6
Posted 18 August 2013 - 12:21 PM
How exactly do you intend on them being able to change the command without editing the code?

i make a command for changing like this


local function changecommand(old,new)
for k,v in pairs(tcom) do
  if v == old then
   tcom[k] = new
   cb.tell(Master,"the command for k is now: "..new)
  end
end
end

then the player can say "turtle change rauf hoch". the function above then switches tcom["up"] = "rauf" to tcom["up"] = hoch" and the command is changed.
function in tfunctions["up"] is still the same. but the script now reacts on hoch instead of rauf. and everytime a change is made i would store the table in a file and reload this on startup if the file exists. else just take the standart.


So you want to have different language indexes for the same function? One cool thing that Lua tables have to offer is that they allow you to use tables as keys. This allows you to do things like:

local commands = {
  [{["hello"]=true,["hola"]=true}] = function()
	print("I can respond to hello in several different languages.")
  end;
}

local input = read()

for key,func in pairs(commands) do
  if key[input] then
	func()
  end
end

hm that sounds interesting. but when i get it right i would need to define a table for every command so that the user can dynamicly add his command to the corresponding table. not sure if this fits for this project but will play around with this a bit.

another thing that came to my mind is to simply change the key in the table. but i dont know if this is possible without loosing the variable it holds.
theoriginalbit #7
Posted 18 August 2013 - 12:33 PM
How exactly do you intend on them being able to change the command without editing the code?

i make a command for changing like this


local function changecommand(old,new)
for k,v in pairs(tcom) do
  if v == old then
   tcom[k] = new
   cb.tell(Master,"the command for k is now: "..new)
  end
end
end

then the player can say "turtle change rauf hoch". the function above then switches tcom["up"] = "rauf" to tcom["up"] = hoch" and the command is changed.
function in tfunctions["up"] is still the same. but the script now reacts on hoch instead of rauf. and everytime a change is made i would store the table in a file and reload this on startup if the file exists. else just take the standart.
So why don't you have the rename function just nil the old one and add the new one?

Data structure

local commands = {
  ["hello"] = function()
    print("Hello!")
  end
}

Checking commands

if commands[cmd] then
  commands[cmd]()
end

Changing commands

local function changeCommand(old, new)
  if not commands[old] then
    return print("No command with that name")
  end
  commands[new] = commands[old]
  commands[old] = nil
end
LordIkol #8
Posted 18 August 2013 - 12:50 PM
How exactly do you intend on them being able to change the command without editing the code?

i make a command for changing like this


local function changecommand(old,new)
for k,v in pairs(tcom) do
  if v == old then
   tcom[k] = new
   cb.tell(Master,"the command for k is now: "..new)
  end
end
end

then the player can say "turtle change rauf hoch". the function above then switches tcom["up"] = "rauf" to tcom["up"] = hoch" and the command is changed.
function in tfunctions["up"] is still the same. but the script now reacts on hoch instead of rauf. and everytime a change is made i would store the table in a file and reload this on startup if the file exists. else just take the standart.
So why don't you have the rename function just nil the old one and add the new one?

Data structure

local commands = {
  ["hello"] = function()
	print("Hello!")
  end
}

Checking commands

if commands[cmd] then
  commands[cmd]()
end

Changing commands

local function changeCommand(old, new)
  if not commands[old] then
	return print("No command with that name")
  end
  commands[new] = commands[old]
  commands[old] = nil
end

Thank you bit, after my last post i came to the same solution :D/>
sometimes im just thinking to fricking complicated.

thanks a lot for your help i will bother you the next days again when im stuck