| View previous topic :: View next topic |
| Author |
Message |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Wed May 03, 2006 5:48 pm Post subject: function in table? |
|
|
I want to be able to store functions in tables like
| Code: | special = {1,2,3,enemies()}
if X == 2 then
special[4]
end
|
It doesn't work like described above, though. Anyone knows how to do this? _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Thu May 04, 2006 12:06 am Post subject: |
|
|
Try this:
| Code: |
special = {1,2,3,enemies}
if X == 2 then
special[4]()
end
|
_________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Fri May 05, 2006 1:30 am Post subject: |
|
|
That nearly works, but at the same time it doesn't.
Luaplayer doesn't give an error on storing 'enemies' (without quotation) in a table, but when calling special[i][4]() it gives an error: problem indexing field '?' (a nil value). I checked that i = 1 and enemies was stored on place 4. What to do? _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Fri May 05, 2006 2:16 am Post subject: |
|
|
For the two dimensional array, you need to create a table within a table. Did you do that? _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Fri May 05, 2006 2:49 am Post subject: |
|
|
| Code: |
Enemy = {}
Enemy[1] = {13,5,0,0,1,0,12,17,1,30,0,enemies}
function CalcEnemy()
i = 1
print(Enemy[i][11]) -- this works, gives 0
Enemy[i][12]() -- error here
end
function enemies()
(..) -- big ass function here
end |
Yeah, it seems I did _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Fri May 05, 2006 3:55 am Post subject: |
|
|
I see you did alright but...
The function 'enemies' should be moved above in order to declare it sooner than using it. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Fri May 05, 2006 8:51 am Post subject: |
|
|
| variable = function (param) do stuff here; end |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Fri May 05, 2006 10:23 am Post subject: |
|
|
Both function syntax work the same but the latter makes more sense if it is used as a variable. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Fri May 05, 2006 6:13 pm Post subject: |
|
|
| KawaGeo wrote: | I see you did alright but...
The function 'enemies' should be moved above in order to declare it sooner than using it. |
You're absolutely right... And I thought I wasn't a noob anymore. Works fine now, thanks 2 all _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
|