Jump to content




[Tables] Can I Improve This?


  • You cannot reply to this topic
7 replies to this topic

#1 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 18 August 2013 - 08: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

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 18 August 2013 - 09: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.

#3 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 18 August 2013 - 10:24 AM

 MKlegoman357, on 18 August 2013 - 09:46 AM, said:

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.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 August 2013 - 10:54 AM

How exactly do you intend on them being able to change the command without editing the code?

#5 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 18 August 2013 - 10: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


#6 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 18 August 2013 - 11:21 AM

 theoriginalbit, on 18 August 2013 - 10:54 AM, said:

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.


 Bubba, on 18 August 2013 - 10:57 AM, said:

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.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 August 2013 - 11:33 AM

 LordIkol, on 18 August 2013 - 11:21 AM, said:

 theoriginalbit, on 18 August 2013 - 10:54 AM, said:

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


#8 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 18 August 2013 - 11:50 AM

 theoriginalbit, on 18 August 2013 - 11:33 AM, said:

 LordIkol, on 18 August 2013 - 11:21 AM, said:

 theoriginalbit, on 18 August 2013 - 10:54 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users