• Math.Random help?
    20 replies, posted
I just wanna know a way to make this work. I dont know how unless I replace the whole self:emitsound line and put it in each little choice. [code] self:EmitSound("vo/" .. math.random(1,7) .. ".wav", 500, 100) if ( choice == 1 ) then "heavy_singing01" if ( choice == 2 ) then "heavy_singing02" if ( choice == 3 ) then "heavy_singing03" if ( choice == 4 ) then "heavy_singing04" if ( choice == 5 ) then "heavy_singing05" if ( choice == 1 ) then "heavy_cheers07" if ( choice == 1 ) then "heavy_cheers08" end )[/code]
[lua] local random = math.random(1,5) self:EmitSound("vo/heavy_singing0"..random..".wav",500,100) [/lua] [editline]04:45PM[/editline] Something like that?
But then I can't use Cheer07/8 [editline]04:33PM[/editline] [code] function ENT:Initialize() local selfent = self.Entity local Timers = { 20, 30, 40, 50, 60, 70, 80 } local Time = (table.Random(Timers)) local Sound = (table.Random(Sounds)) timer.Create("RandomStuff", Time, 0, function() random(1,7) if ( choice == 1 ) then self:EmitSound("vo/heavy_singing01.wav", 500, 100) elseif ( choice == 2 ) then self:EmitSound("vo/heavy_singing02.wav", 500, 100) elseif ( choice == 3 ) then self:EmitSound("vo/heavy_singing03.wav", 500, 100) elseif ( choice == 4 ) then self:EmitSound("vo/heavy_singing04.wav", 500, 100) elseif ( choice == 5 ) then self:EmitSound("vo/heavy_singing05.wav", 500, 100) elseif ( choice == 1 ) then self:EmitSound("vo/heavy_cheers07.wav", 500, 100) elseif ( choice == 1 ) then self:EmitSound("vo/heavy_cheers08.wav", 500, 100) end end ) [/code] Its the whole function and it didn't work. Any ideas?
[lua]local Sounds = { Sound( "vo/heavy_singing01.wav" ), Sound( "vo/heavy_singing02.wav" ), Sound( "vo/heavy_singing03.wav" ), Sound( "vo/heavy_singing04.wav" ), Sound( "vo/heavy_singing05.wav" ), Sound( "vo/heavy_cheers07.wav" ), Sound( "vo/heavy_cheers08.wav" ) }; self:EmitSound( table.Random( Sounds ), 500, 100 );[/lua] Put them in a table.
lua\includes\extensions\table.lua:165: bad argument #1 to 'pairs' (table expected, got nil) What?
Post your code if you edited it
You dont really have to use [QUOTE=Nevec;23609503][B]Sound( [/B]"vo/heavy_singing01.wav"[B] )[/B][/QUOTE] conserve some memory.. anyway. Nevec seems to have done everything right, tell us how you used it, since the problem is coming from you side.
Alright [code] function ENT:Initialize() local selfent = self.Entity local Timers = { 20, 30, 40, 50, 60, 70, 80 } local Time = (table.Random(Timers)) timer.Create("RandomStuff", Time, 0, function() self:EmitSound( table.Random( Sounds ), 500, 100 ); local Sounds = { Sound( "vo/heavy_singing01.wav" ), Sound( "vo/heavy_singing02.wav" ), Sound( "vo/heavy_singing03.wav" ), Sound( "vo/heavy_singing04.wav" ), Sound( "vo/heavy_singing05.wav" ), Sound( "vo/heavy_cheers07.wav" ), Sound( "vo/heavy_cheers08.wav" ) }; end ) self.Entity:SetModel("models/props_interiors/VendingMachinezombies.mdl") self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end [/code]
[QUOTE=Averice;23609136][lua] local random = math.random(1,5) self:EmitSound("vo/heavy_singing0"..random..".wav",500,100) [/lua] [editline]04:45PM[/editline] Something like that?[/QUOTE] Shouldn't that be: [lua] local random = math.random(1,5) self:EmitSound("vo/heavy_singing0"..tostring(random)..".wav",500,100) [/lua]
[QUOTE=Zombie Strider;23610762]Alright [CODE]self:EmitSound( table.Random( Sounds ), 500, 100 ) local Sounds = { Sound( "vo/heavy_singing01.wav" ), Sound( "vo/heavy_singing02.wav" ), Sound( "vo/heavy_singing03.wav" ), Sound( "vo/heavy_singing04.wav" ), Sound( "vo/heavy_singing05.wav" ), Sound( "vo/heavy_cheers07.wav" ), Sound( "vo/heavy_cheers08.wav" ) } [/CODE][/QUOTE] Glua.. like any other, reads code from left to right from top to bottom, so it tries to emit the sound but it cant cause local sounds hasint been created yet.. put the whole local sounds above emit sound. Just paste this instead.. [LUA]local Sounds = {"vo/heavy_singing01.wav","vo/heavy_singing02.wav","vo/heavy_singing03.wav","vo/heavy_singing04.wav","vo/heavy_singing05.wav","vo/heavy_singing07.wav","vo/heavy_singing08.wav"} self:EmitSound(table.Random(Sounds), 500, 100)[/LUA]
[QUOTE=Zombie Strider;23610762]Alright [code] function ENT:Initialize() local selfent = self.Entity local Timers = { 20, 30, 40, 50, 60, 70, 80 } local Time = (table.Random(Timers)) timer.Create("RandomStuff", Time, 0, function() self:EmitSound( table.Random( Sounds ), 500, 100 ); local Sounds = { Sound( "vo/heavy_singing01.wav" ), Sound( "vo/heavy_singing02.wav" ), Sound( "vo/heavy_singing03.wav" ), Sound( "vo/heavy_singing04.wav" ), Sound( "vo/heavy_singing05.wav" ), Sound( "vo/heavy_cheers07.wav" ), Sound( "vo/heavy_cheers08.wav" ) }; end ) self.Entity:SetModel("models/props_interiors/VendingMachinezombies.mdl") self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end [/code][/QUOTE] Your trying to use the Sounds table before you define it, try: [lua] function ENT:Initialize() local selfent = self.Entity local Timers = { 20, 30, 40, 50, 60, 70, 80 } local Time = (table.Random(Timers)) timer.Create("RandomStuff", Time, 0, function() local Sounds = { Sound( "vo/heavy_singing01.wav" ), Sound( "vo/heavy_singing02.wav" ), Sound( "vo/heavy_singing03.wav" ), Sound( "vo/heavy_singing04.wav" ), Sound( "vo/heavy_singing05.wav" ), Sound( "vo/heavy_cheers07.wav" ), Sound( "vo/heavy_cheers08.wav" ) }; self:EmitSound( table.Random( Sounds ), 500, 100 ); end ) self.Entity:SetModel("models/props_interiors/VendingMachinezombies.mdl") self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end [/lua]
Wow. That worked fine indeed. Thanks guys!
[QUOTE=Zombie Strider;23611054]Wow. That worked fine indeed. Thanks guys![/QUOTE] np
[QUOTE=freemmaann;23610585]You dont really have to use conserve some memory.. anyway.[/QUOTE] I like to precache all assets before using them, so the game doesn't randomly freeze. It doesn't matter if the sound is short, though. Also, put the Sounds table outside the Initialize function. You're recreating it every time the timer triggers.
[QUOTE=Ronon Dex;23610800]Shouldn't that be: [lua] local random = math.random(1,5) self:EmitSound("vo/heavy_singing0"..tostring(random)..".wav",500,100) [/lua][/QUOTE] no?
[QUOTE=Zombie Strider;23611054]Wow. That worked fine indeed. Thanks guys![/QUOTE] The thing you did, was calling "Sounds" before you defined what it was.
Yeah makes sense now.
[CODE] choice = math.Random(1,7) if choice == 1 then self:EmitSound("vo/heavy_singing01.wav",500,100) elseif choice == 2 then self:EmitSound("vo/heavy_singing02.wav",500,100) elseif choice == 3 then self:EmitSound("vo/heavy_singing03.wav",500,100) elseif choice == 4 then self:EmitSound("vo/heavy_singing04.wav",500,100) elseif choice == 5 then self:EmitSound("vo/heavy_singing05.wav",500,100) elseif choice == 4 then self:EmitSound("vo/heavy_cheers07.wav",500,100) elseif choice == 5 then self:EmitSound("vo/heavy_cheers08.wav",500,100) end [/CODE]
You could also make a table and then make it randomly select one, like this: [code]local Sounds = { "vo/heavy_singing01.wav", -- stuff inside the table "vo/heavy_singing02.wav", "vo/heavy_singing03.wav", "vo/heavy_singing04.wav", "vo/heavy_singing05.wav", "vo/heavy_cheers07.wav", "vo/heavy_cheers08.wav" } local Sound = (table.Random(Sounds)) -- Pick a random thing from the "Sounds" table self:EmitSound(Sound) -- Play the randomly picked thing from "Sounds" table[/code]
Or if you want to optimize memory usage : [code]local Number = math.Random(1,7) local prefix if Number > 5 then prefix = "cheers" Number = Number + 1 else prefix = "singing" end self:EmitSound("vo/heavy_"..prefix.."0"..Number..".wav") -- Play the randomly composed sound.[/code] I just felt like writting that.
Guys guys!! [img] http://img33.imageshack.us/img33/3497/problemsolvedy.jpg[/img]
Sorry, you need to Log In to post a reply to this thread.