forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function in table?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Wed May 03, 2006 5:48 pm    Post subject: function in table? Reply with quote

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
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Thu May 04, 2006 12:06 am    Post subject: Reply with quote

Try this:
Code:

special = {1,2,3,enemies}

if X == 2 then
      special[4]()
end

_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Fri May 05, 2006 1:30 am    Post subject: Reply with quote

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
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri May 05, 2006 2:16 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Fri May 05, 2006 2:49 am    Post subject: Reply with quote

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
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri May 05, 2006 3:55 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Fri May 05, 2006 8:51 am    Post subject: Reply with quote

variable = function (param) do stuff here; end
Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri May 05, 2006 10:23 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Fri May 05, 2006 6:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group