Author Topic: Functions and tables  (Read 3110 times)

0 Members and 1 Guest are viewing this topic.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Functions and tables
« on: 12 November, 2004, 02:35:09 »
Code: [Select]
local commandtable={
["invite"]= invite(user,data,env,"temp"),
["remove"]=removeopchat(user,data,env),
["perminvite"]=invite(user,data,env,"perm"),
-- etc etc

}
 

The question is:

Code: [Select]
if commandtable[cmd] then return commandtable[cmd] end
why does not work properly? (executes ALL commands)

can that be a prob that i dont return 1 inside my functions? or what?

help me i have 108 commands and im vomiting of elseif ;)

just asking because this script  works.
« Last Edit: 12 November, 2004, 02:45:18 by bastya_elvtars »
Everything could have been anything else and it would have just as much meaning.

PtokaX forum

Functions and tables
« on: 12 November, 2004, 02:35:09 »

Offline VidFamne

  • Triple Ace
  • **
  • Posts: 173
  • Karma: +2/-0
(No subject)
« Reply #1 on: 12 November, 2004, 10:54:04 »
**Edit**
I used to use this;
Code: [Select]
if cmd and commandtable[cmd] then return commandtable[cmd](arg1, arg2, arg3, arg4) endAnd another tip, i got from RabidWombat; use reference in commandtable instead,
like
Code: [Select]
commandtable={
["invite"]= invite,
["remove"]=removeopchat,
["perminvite"]=invite,
-- etc etc

}
Note; load the functions before the commandtable, so the table knows what to reference to.
I've used this in GeoIP-script.
« Last Edit: 12 November, 2004, 13:26:42 by VidFamne »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #2 on: 12 November, 2004, 14:37:13 »
OK, but as i mentioned, diff funcions use diff variables. So if i use commandtable[cmd](var1,var2) then it will give a nil error when i link 2 a function(var1,var2,var3)...
Everything could have been anything else and it would have just as much meaning.

Offline VidFamne

  • Triple Ace
  • **
  • Posts: 173
  • Karma: +2/-0
(No subject)
« Reply #3 on: 12 November, 2004, 22:25:26 »
Why parse only two arguments?
Try to parse as many as the max. arg's needed for a function.
If a function need less, than it choose the first args given and skipping the rest.

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
(No subject)
« Reply #4 on: 13 November, 2004, 00:16:55 »
commandtable[cmd](...)
this should work on any amount of arguments.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #5 on: 14 November, 2004, 21:07:46 »
Quote
Originally posted by plop
commandtable[cmd](...)
this should work on any amount of arguments.

plop

Code: [Select]
[customcmd5]=showtextfile(user,customtext5,customcmdlev5,env),
---------------- custom add/show/del command shit
-- guestbook stuff
[gbcmd1]=showshit(user,entries,gbname,gbfile,levgbcmd1,env),
-- (user,container,wtf,file),
[gbcmd2]=addshit(user,data,entries,gbfile,levgbcmd2,env),
-- (user,data,container,file),
[gbcmd3]=delshit(user,data,entries,gbfile,gbname,levgbcmd3,env),


what the hell should be done with this then? (altho thx 4 help)

*** EDIT ***

what about dostring(commandtable[cmd])  ?

(i know, then values should be strings)
« Last Edit: 16 November, 2004, 22:38:39 by bastya_elvtars »
Everything could have been anything else and it would have just as much meaning.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #6 on: 22 November, 2004, 02:19:11 »
Quote

what about dostring(commandtable[cmd])  ?

(i know, then values should be strings)

does not work, do you have any other ideas?

!me always helps, will he get helped out once? :baby:
Everything could have been anything else and it would have just as much meaning.

Offline chill

  • Triple Ace
  • **
  • Posts: 182
  • Karma: +1/-0
(No subject)
« Reply #7 on: 22 November, 2004, 10:28:55 »
dunno if this helps

function dummy1(arg1, arg2, arg3)
   bla bla bla
end

dummy2 = function(arg1,arg2)
  bla bla bla
end


Funcs = {}

Funcs.trigger1 = dummy1
Funcs.trigger2 = dummy2


-- to call

local trig

if Funcs[trig] then
   -- execute function
   Funcs[trig](arg1,arg2,arg3)
end

PtokaX forum

(No subject)
« Reply #7 on: 22 November, 2004, 10:28:55 »