• Add Delay To math.random
    17 replies, posted
[I]-snip-[/I]
Why would you delay math.random?
You shouldn't restrict math.random but rather the function your doing... For example, a roll the die... allow !roll per player every 0.5 seconds. For that you'd log the time it happened and call math.random IF the logged-time-variable doesn't exist OR CurTime( ) - logged-time-variable > 0.5... [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/time/understanding_time%20and_basic_time_calculations.lua.html[/url] Micro-caching nwvars is a good method of doing it: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/caching/micro_caching_nw_vars.lua.html[/url] Although instead of always returning the value, only return the value if it changes. Add an else to output anti-spam lingo.
Here's a good snippet from what Ace said. [quote]You shouldn't restrict math.random but rather the function your doing...[/quote] Just delay your calling of it.
Here, basically I have a table full of strings, and I have a variable picking between them all. local name = names[math.random(1,52)] I just want it to choose between 1 to 52 every half a second.
I think you are looking for [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Create]timer.Create[/url] [Lua] local randomNumber = math.random( 0, 10 ) timer.Create( "RandomNumber", 0.5, 0, function() randomNumber = math.random( 0, 10 ) end ) [/lua]
[code] timer.Create( "RandomNameSelector", 0.5, 0, function() local name = names[ math.random( 1, 52 ) ] ...code here... end )[/code] ??? Ninja'd.
If I were to post code here, would you guys be able to insert the timer to do it, please? I'm pretty bad with Lua right now.
Or... Here's a thought... You try coding it, and we help you correct it.
Look, timer.Create couldnt be more simple. Lets have a look at how to use it [lua] timer.Create( "timer", -- This creates a timer with the identifier "timer". 0.5, -- Delay the timer by 0.5 seconds 0, -- Repeat the timer forever function() -- The function that is ran after the delay has happened, this can also be a function name without the () end ) -- Closes the function, and since this is the end of the arguments for timer.Create, there is also a ). [/lua] If that isn't clear enough I don't know what is.
Actually it could be more simple, hence timer.Simple
1 to 52? If those are ALL of the entries in the "names" table then simply use: local _name, _number = table.Random( names ); -- where _name is the random string, ie value of names[ _number ] and _number is the key...
I'm sorry, but I'm just completely clueless to this.
[lua]timer.Simple(0.5, function() math.random( number 1, number 2 ) end)[/lua] It can't get simpler.
No, I'm talking about making this work. You guys make it seem simple but oh my god, it's not.
It really is. Looking at the wiki makes things extremely simple since they usually have snippets. If they don't then google that function and see what pops up, just play with it. That's really the only way to effectively know something (reading as well, but I learn more by doing)
Can I just get this locked, please?
[QUOTE=_VeXan;46878976]Can I just get this locked, please?[/QUOTE] no
Sorry, you need to Log In to post a reply to this thread.