.:HyPeX:. Posted September 11, 2013 Share Posted September 11, 2013 Hi guys, i'm trying to make my antispam a little yet more advanced, but i'm finding problems to work this out (no idea how to properly use returns), can someone tell me how to use it properly? antiSpam = {} antiSpamTimer = {} function AntiSpam (message, messageType) if messageType == 0 and isTimer(antiSpam[source]) then if isTimer(antiSpamTimer[source]) then killTimer(antiSpamTimer[source]) cancelEvent () antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 300000, 1, source) else return cancelEvent () killTimer(antiSpam[source]) antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 10000, 1, source) outputChatBox("[AntiSpam]: Stop Flooding, Every new messange will restart your time muted!", source, 0, 170, 255) outputChatBox("[AntiSpam]: Type /sleft to get your time left muted.", source, 0, 170, 255) antiSpamTimer[source] = setTimer(function(source) antiSpamTimer[source] = nil end, 120000, 1, source) else antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 1000, 1, source) end end end addEventHandler("onPlayerChat", getRootElement(), AntiSpam) Link to comment
Castillo Posted September 11, 2013 Share Posted September 11, 2013 The question is, what do you think "return" does? Link to comment
.:HyPeX:. Posted September 11, 2013 Author Share Posted September 11, 2013 The question is, what do you think "return" does? make the script go back and continue like that line doesnt exist (the if in this case) Link to comment
bandi94 Posted September 11, 2013 Share Posted September 11, 2013 "return" alone , jump's back to the parent , so the code after it will not be runned. "return" with an argument like : "return number" , will jump back with the argument. Like data = functionReturn() will hold the data from the argument witch was in the return , in this case the variable "number" Link to comment
.:HyPeX:. Posted September 11, 2013 Author Share Posted September 11, 2013 Wich parent will it return to? the function, or the if before? and if if i have to use the argument, can you explain me better or show me an example? i didnt understand much how to use it. Link to comment
bandi94 Posted September 11, 2013 Share Posted September 11, 2013 Type 1 function getNumber() local number = functionReturn() outputChatBox(number) end function functionReturn() return 5 -- this will return argument "5" to his parent in this case is "function getNumber()" and it will outputChatBox 5 . end Type 2 function Nothing() --code blablabla return -- in this case the parent is "nil" bk this function was not called from another function like in "Type 1" so this will return to the RootNode witch is actually the game , so this will just stop executing the code after the "return" end Link to comment
.:HyPeX:. Posted September 11, 2013 Author Share Posted September 11, 2013 i got some short questions: 1#: why it is number 5? 2#: i can make it just return to the if, or when returning to the function it will ignore the return and follow? lets say on my script, i want to return just like ignoring the last if (lets say it should return to "if messageType == 0..") function AntiSpam (message, messageType) if messageType == 0 and isTimer(antiSpam[source]) then if isTimer(antiSpamTimer[source]) then killTimer(antiSpamTimer[source]) cancelEvent () antiSpam[source] = setTimer(function(source) antiSpam[source] = nil end, 300000, 1, source) else return Link to comment
bandi94 Posted September 11, 2013 Share Posted September 11, 2013 1. number "5" is just an exampel , "return" can return everything from a number , tabel , string , ...... 2. nope it's not possible "return" alone like in your code will end the whole "AntiSpam" function , it will not return to "if messageType == 0..". "return" can return arguments only for another function from where the current function was called like i showed to you , and not to the function is in it. I think you miss understand "return" function. "return" function can return only arguments stored like : tableA = {} return tableA local number = "5" return number local stringA = "Test" return stringA and not to commands like "if" Link to comment
.:HyPeX:. Posted September 11, 2013 Author Share Posted September 11, 2013 So then, is there a way to make what i want with my script? since return will not do it.. Link to comment
ixjf Posted September 12, 2013 Share Posted September 12, 2013 bandi94, that's called caller, not parent. The return keyword halts the function and returns back to the caller, with or without any data. Link to comment
.:HyPeX:. Posted September 13, 2013 Author Share Posted September 13, 2013 bandi94, that's called caller, not parent. The return keyword halts the function and returns back to the caller, with or without any data. so it will return to where? the second if in this case? Link to comment
ixjf Posted September 13, 2013 Share Posted September 13, 2013 No, I said the caller of the function. Link to comment
.:HyPeX:. Posted September 14, 2013 Author Share Posted September 14, 2013 No, I said the caller of the function. but there's no caller at all almost, it is onplayerchat, what should i do? Link to comment
ixjf Posted September 14, 2013 Share Posted September 14, 2013 There always is. How do you expect the function bound to the event to be called? Link to comment
.:HyPeX:. Posted September 14, 2013 Author Share Posted September 14, 2013 There always is. How do you expect the function bound to the event to be called? i'm not sure how to translate that part with the bound, but i was thinking of 2 separate functions, and like return to the function before if that doesnt works and it then should run correctly... (lets say it would trigger the 1st one as the 2nd one wasnt runing). But then it comes to the part, how i call it? as you said return doesnt do it. hows a caller, as you said? Link to comment
ixjf Posted September 14, 2013 Share Posted September 14, 2013 An event can be bound to the function, the function handles the event. The event name is pretty much like an identifier to the handler when you're triggering it. An example of how return works: local function main() local received = MessageHandler:GetSingleton():GetMessage() while ( true ) do if ( received ) then if ( received == MESSAGE_QUIT ) then -- this will halt the function and return to the caller -- in this example, this is the entry point, and the caller should have been the host program (the one who runs Lua, e.g. MTA) return end MessageHandler:GetSingleton():HandleMessage(received) received = MessageHandler:GetSingleton():GetMessage() end end end ... function MessageHandler:GetMessage() -- some code here to get the message -- now you return the message, this returns to the caller with the message -- if you check the function 'main' above, you'll see it expects the function to return that message return message end All of this doesn't really make much sense (the main function could have been done in a more logical way but it would not make use of the return keyword which was the point of this example), but it should have been enough to understand this. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now