• Make a Variable-Function, is it possible?
    9 replies, posted
Is it possible to call a function set on a certain variable? Exemple, [lua]local bitch = SetColor() bitch() bitch = Print("LOL") bitch() [/lua]
Not that way. Try: [lua] local var = function() print( "stuff" ) end var() [/lua]
I don't see why you'd do this instead of just use the function, unless you were getting input from the player, but even then you can use ifs, elseifs, and elses. However, it is possible in two ways as far as I can tell. Number 1:[lua]local FuncVar = function(red, green, blue) for k,v in pairs(player.GetAll()) do v:SetColor(red, green, blue, 128) print("All players on the server have been turned into colored ghosts.") end end FuncVar(0,0,0) -- Turns players into black ghosts.[/lua] Number 2:[lua]function FuncVar(red, green, blue) for k,v in pairs(player.GetAll()) do v:SetColor(red, green, blue, 128) print("All players on the server have been turned into colored ghosts.") end end FuncVar(0,0,0) -- Turns players into black ghosts.[/lua] Both these scripts would work, I think.
[QUOTE=gerhard2202;16868970]I don't see why you'd do this instead of just use the function, unless you were getting input from the player, but even then you can use ifs, elseifs, and elses. However, it is possible in two ways as far as I can tell. Both these scripts would work, I think.[/QUOTE] storing functions in tables can be very useful
[QUOTE=Bang Train;16870322]storing functions in tables can be very useful[/QUOTE] That's what metatables are for.
[QUOTE=Yobdren;16870366]That's what metatables are for.[/QUOTE] No, it's not. You don't store a function in a metatable, when it's going to be different for everyone of it's child tables. You're talking about making a 'prototype' table.
CrashLemon you're almost correct: [lua]local bitch = SetColor -- no (), if you do the () bitch will be what SetColor returns(probably nil in this case) bitch()-- Call your bitch, it's the same as calling SetColor() bitch = print -- lowercase p and again no () because bitch will be nil bitch("LOL") [/lua] [lua]variable = Angle[/lua] will make variable be the Angle function. you can now do variable(1,2,3) to make an angle if you do [lua]variable = Angle(1,2,3)[/lua] then variable will be the angle that the Angle(1,2,3) function returns. in this case variable.p will be 1, variable.y 2 and variable.r 3 because it's the angle. In this case you can [u]not[/u] do variable(1,2,3) I hope you understand. here's something for you to try out. [lua]bitch = print print(bitch) -- this will say function ... because it's a function lol = print("LOL!") -- this will print LOL! at first because you're calling the function but then: print(lol) -- when you print lol itself(the third line) it will say nil because the print function doesn't return anything![/lua] paste that in [url]http://www.lua.org/cgi-bin/demo[/url] press Run and you will see the output!
Yeah I do understand FTPje, thanks for the information it'll be usefull. :D Edit: I have another question, now that I have my functions, how would I make a function that accept infinite arguments? Exemple, [lua] function Add( 1, 3, 4 ) -- Do 1+3+4 so return 8 end function Add( 2, 5, 1, 1, 1, 2 ) -- Do 2+5+1+1+1+2 so return 12 end [/lua]
[QUOTE=CrashLemon;16882174]Yeah I do understand FTPje, thanks for the information it'll be usefull. :D Edit: I have another question, now that I have my functions, how would I make a function that accept infinite arguments? Exemple, [lua] function Add( 1, 3, 4 ) -- Do 1+3+4 so return 8 end function Add( 2, 5, 1, 1, 1, 2 ) -- Do 2+5+1+1+1+2 so return 12 end [/lua][/QUOTE] [code]function MultiArgs(...) for k , v in pairs(arg) do print("I called the function with "..v) end end[/code]
[QUOTE=|FlapJack|;16882673][code]function MultiArgs(...) for k , v in pairs(arg) do print("I called the function with "..v) end end[/code][/QUOTE] Alright, and if I'd like, let's say, definite args and after that you can add as much as you want, would I need to filter which arguments aren't predefined? Exemple [lua] function Google( search, where, keywords, ... ) -- This is just an exemple for k,v in pairs( arg ) do if v != where or v != search or v != keywords then -- Do stuff here end end end[/lua]
Sorry, you need to Log In to post a reply to this thread.