adamb Posted May 15, 2013 Share Posted May 15, 2013 So basically, i have these function CreateX() function CreateZ() function CreateY() So, i want to make the script use math.random to select a random one of those three above to start with addEventHandler onResourceStart tho i have no idea how to do it.. Can anyone help me out? Link to comment
Moderators IIYAMA Posted May 15, 2013 Moderators Share Posted May 15, 2013 NOT tested..... local myFunctions = {} -- store the functions myFunctions[1]= function (player)--CreateX if player then outputChatBox(getPlayerName(player) .. ", executed: CreateX") else outputChatBox("CreateX") end end myFunctions[2]= function (player)--CreateY if player then outputChatBox(getPlayerName(player) .. ", executed: CreateY") else outputChatBox("CreateY") end end myFunctions[3]= function (player)--CreateZ if player then outputChatBox(getPlayerName(player) .. ", executed: CreateZ") else outputChatBox("CreateZ") end end addEventHandler("onResourceStart",resourceRoot, function () local number = math.random(1,#myFunctions) -- a random number from 1 t/m the amount of functions in the table. myFunctions[number]() end) addEventHandler("onPlayerDamage",root, -- random event....... function () myFunctions[math.random(1,#myFunctions) ](source) -- compressed. end) Link to comment
Moderators IIYAMA Posted May 15, 2013 Moderators Share Posted May 15, 2013 "The only question that I have been asking my self, are local functions faster .?.?.?." then just a trigger, Link to comment
denny199 Posted May 15, 2013 Share Posted May 15, 2013 http://www.lua.org/pil/4.2.html It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones. Link to comment
Moderators IIYAMA Posted May 15, 2013 Moderators Share Posted May 15, 2013 These are functions, did you ever made your functions local? (like the sample from a both) Link to comment
ixjf Posted May 15, 2013 Share Posted May 15, 2013 What you call a function is just a variable that holds a value of type function (this doesn't apply only to functions). Always make sure you declare variables as local, unless you need them elsewhere out of that scope. Link to comment
adamb Posted May 15, 2013 Author Share Posted May 15, 2013 So, out of that, i did this, i get a syntax error near 'myFunctions' myFunctions[1]= startcrazy local startResource ( crazy ) myFunctions[2]= startmugging local startResource ( mugging ) myFunctions[3]= startgang local startResource ( gang ) Link to comment
3NAD Posted May 15, 2013 Share Posted May 15, 2013 Not Tasted. Resources = { [1] = "crazy"; [2] = "mugging"; [3] = "gang"; }; addCommandHandler ( "random", function ( player ) local random = Resources [ math.random ( #Resources ) ] outputChatBox ( ( random or "Error" ), player, 255, 255, 0, true ) end ) Link to comment
Moderators IIYAMA Posted May 15, 2013 Moderators Share Posted May 15, 2013 lol scripts or functions O_o Link to comment
adamb Posted May 15, 2013 Author Share Posted May 15, 2013 Any will do, i just want it to work like this, either trigger a random script or function out of three... Link to comment
Moderators IIYAMA Posted May 15, 2013 Moderators Share Posted May 15, 2013 now you got both Link to comment
adamb Posted May 15, 2013 Author Share Posted May 15, 2013 Done this, i get Bad arguement on line 10 @ startResource tho... Resources = { [1] = "crazy"; [2] = "mugging"; [3] = "gang"; }; function callouts() local res = getResourceFromName("crazy", "mugging", "gang") local random = Resources [ math.random ( #Resources ) ] startResource ( random ) outputChatBox ( "Dispatch here, all units standby for callout" ) end addEventHandler ( "onResourceStart", resourceRoot, callouts ) setTimer (addEventHandler, 300000, 0) function this ( theResource ) local they = getResourceFromName ( "calloutsbeta" ) if getResourceFromName then restartResource(they) end end setTimer (this, 420000,0 ) Link to comment
iPrestege Posted May 15, 2013 Share Posted May 15, 2013 Resources = { [1] = "crazy"; [2] = "mugging"; [3] = "gang"; }; addEventHandler ("onResourceStart",resourceRoot, function ( ) setTimer ( function ( ) local random = Resources [ math.random ( #Resources ) ] local res = getResourceFromName( random ) if not res then return end startResource ( res ) outputChatBox ( "Dispatch here, all units standby for callout" ) end,300000,0 ) end ) Does that what are you trying to do ? Link to comment
adamb Posted May 15, 2013 Author Share Posted May 15, 2013 Thanks to everyone that contributed, it now works! PresTege, thank you, you have solved alot of my problems so far! It worked! 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