Drakath Posted April 12, 2015 Share Posted April 12, 2015 I have a server function that is used by both server and client. When I want to call it via same server script, is it more efficient to simply: myFunction(arg) or triggerEvent("myEventName", resourceRoot, arg) ? Link to comment
Drakath Posted April 12, 2015 Author Share Posted April 12, 2015 ofcourse "myFunction(arg)" So why triggerEvent is even in MTA? Link to comment
WhoAmI Posted April 12, 2015 Share Posted April 12, 2015 triggerEvent is for creating custom events, that's completely different thing than calling a function. Link to comment
Drakath Posted April 12, 2015 Author Share Posted April 12, 2015 And those custom events call a function. It's basically the same thing. Link to comment
WhoAmI Posted April 12, 2015 Share Posted April 12, 2015 It isn't. triggerEvent calling an event, which can be attached to function. Link to comment
Drakath Posted April 12, 2015 Author Share Posted April 12, 2015 It isn't. triggerEvent calling an event, which can be attached to function. You mean you can do stuff like this? triggerEvent("onPlayerJoin", root, arg) Link to comment
Ryancit2 Posted April 12, 2015 Share Posted April 12, 2015 And those custom events call a function. It's basically the same thing. No, here is explanation... You should call functions in same file like function(arg) while you have a choice triggerEvent, say you made your own event like "onClientDepositMoney" but this event will not be triggered unless you make it happen manually, in this case, you'll use 'triggerEVent' within SAME file, now when you add "onClientDepositMoney" EXTERNALLY in some other script, the event will be triggered on original script and will call the function from OTHER script too, here is example: -- atms_c.lua function confirmDeposit(acc, money) -- Acc is account of player, got from serverside ofcourse while money is numeric value. if (not money) or (not acc) then return end local prev = getAccountData(acc, "atms.money") or 0 setAccountData(acc, "atms.money", tonumber(prev) + money) triggerEvent("onDepositMoney", root, getAccountName(acc), money) end addEventHandler("onClientGUIClick", deposit_btn, confirmDeposit, false) addEvent("onDepositMoney", true) -- Make event trigger-able externally... Now some other resource, some other file: -- logs_c.lua function writeLog(acc, money) if (not acc) or (not money) then return end writeLog(acc, "Player has deposited $"..money.."in his bank.") end addEventHandler("onDepositMoney", root, writeLog) Link to comment
Drakath Posted April 12, 2015 Author Share Posted April 12, 2015 And those custom events call a function. It's basically the same thing. No, here is explanation... You should call functions in same file like function(arg) while you have a choice triggerEvent, say you made your own event like "onClientDepositMoney" but this event will not be triggered unless you make it happen manually, in this case, you'll use 'triggerEVent' within SAME file, now when you add "onClientDepositMoney" EXTERNALLY in some other script, the event will be triggered on original script and will call the function from OTHER script too, here is example: -- atms_c.lua function confirmDeposit(acc, money) -- Acc is account of player, got from serverside ofcourse while money is numeric value. if (not money) or (not acc) then return end local prev = getAccountData(acc, "atms.money") or 0 setAccountData(acc, "atms.money", tonumber(prev) + money) triggerEvent("onDepositMoney", root, getAccountName(acc), money) end addEventHandler("onClientGUIClick", deposit_btn, confirmDeposit, false) addEvent("onDepositMoney", true) -- Make event trigger-able externally... Now some other resource, some other file: -- logs_c.lua function writeLog(acc, money) if (not acc) or (not money) then return end writeLog(acc, "Player has deposited $"..money.."in his bank.") end addEventHandler("onDepositMoney", root, writeLog) Wow, that's a really nice feature. Thank you for a nice explanation Link to comment
Ryancit2 Posted April 12, 2015 Share Posted April 12, 2015 You are welcomed, happy to know it helped you 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