• Mkaing a function using methods
    7 replies, posted
Simple question; how do you create a function that gets an argument with a method? IE being able to pass it like [lua]x:F()[/lua] instead of like [lua]F(x)[/lua]
I'm pretty sure you do it just as you did with the first code snippet. You would then use self to access the argument.
[QUOTE=n00berific;25042757]I'm pretty sure you do it just as you did with the first code snippet. You would then use self to access the argument.[/QUOTE] I didn't get any of that Are you saying I can just do [lua] function x:F() //doshitwithXhere end [/lua] and be able to call my function like [lua] x:F() [/lua] ?
Alright, say you have just created a variable. For these purposes I'm just going to make a table with a few strings. [lua]local x = {"This", "is", "a", "table"}[/lua] Now you want to use this table and pass it to another function which has previously been created. [lua]function x:F() PrintTable(self) end[/lua] I'm not entirely sure if this what you're asking at this point, but you could do something like x:F() and call the function passing x as the first argument. EDIT: Yeah, you got to it before me. Yes, pretty much.
[b][url=http://wiki.garrysmod.com/?title=G.FindMetaTable]G.FindMetaTable [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[QUOTE=PortalGod;25043081][b][url=http://wiki.garrysmod.com/?title=G.FindMetaTable]G.FindMetaTable [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b][/QUOTE] That works, danke schön
For the reference, you can use methods on any table or table based type. [lua]local t = {} function t:What() end local t = _R.Player function t:What() end[/lua] Both are valid.
Sorry, you need to Log In to post a reply to this thread.