• Print

Author Topic: Serverside generated random  (Read 5313 times)

0 Members and 1 Guest are viewing this topic.

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Serverside generated random
« on: April 05, 2015, 06:00:37 am »
Hello gys
I have 2 files
cl_endroundboard.lua
sh_endroundboard.lua

cl_endroundboard.lua :
Code: Lua
  1. hook.Add( "CreateRandMusic", "Does something else", DoSomethingElse )
  2.         create_random_music()
  3.         if end_music == 0 then
  4.         ply:EmitSound( "end_of_round/afraid.mp3", 40, 100, 1, CHAN_AUTO )
  5.         elseif end_music == 1 then
  6.         ply:EmitSound( "end_of_round/benn_to_hell.mp3", 40, 100, 1, CHAN_AUTO )
  7.         elseif end_music == 2 then
  8.         ply:EmitSound( "end_of_round/bumpin.mp3", 30, 100, 1, CHAN_AUTO )
  9.         elseif end_music == 3 then
  10.         ply:EmitSound( "end_of_round/gorky_park.mp3", 40, 100, 1, CHAN_AUTO )
  11.         elseif end_music == 4 then
  12.         ply:EmitSound( "end_of_round/private_lesson.mp3", 40, 100, 1, CHAN_AUTO )
  13.         elseif end_music == 5 then
  14.         ply:EmitSound( "end_of_round/skrillex.mp3", 40, 100, 1, CHAN_AUTO )
  15.         elseif end_music == 6 then
  16.         ply:EmitSound( "end_of_round/wild_animals.mp3", 40, 100, 1, CHAN_AUTO )
  17.         elseif end_music == 7 then
  18.         ply:EmitSound( "end_of_round/yes i can lie.mp3", 40, 100, 1, CHAN_AUTO )
  19.         else
  20.         MsgC( Color( 255, 0, 0 ), "Error loading sounds" )
  21.         end
sh_endroundboard.lua :
Code: Lua
  1. function create_random_music()
  2. end_music = math.random (0,7)
  3. hook.Call("CreateRandMusic")
  4. end
  5.  
  6. function DoSomethingElse()
  7.         --Does something else, once the hook DoneDoingSomething is called.
  8.         print("Done!")
  9. end
  10.  
Problem: clientside random generation(so all players listen different music at the end of round),I want to change to serverside, how Can I do this?
« Last Edit: April 05, 2015, 06:18:19 am by ZeD »

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Serverside generated random
« Reply #1 on: April 05, 2015, 10:56:30 am »
Is this for any specific gamemode? Or are you just picking any old time to play this?
Either way, why use hooks? Network messages seem like they could slim this down quite a bit.

Every time your hook is called, you generate a new random number. You call create_random_music() in your hook, which both generates a random number and calls the hook again. That's an infinite loop, which could be avoided if you didn't call the hook from inside the hook like that. Also, you'll have to generate a number on the server and send it to clients for it all to be static---again a reason why you should use net messages over hooks.
bw81@ulysses-forums ~ % whoami
Homepage

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Serverside generated random
« Reply #2 on: April 05, 2015, 11:09:25 am »
Is this for any specific gamemode? Or are you just picking any old time to play this?
Either way, why use hooks? Network messages seem like they could slim this down quite a bit.

Every time your hook is called, you generate a new random number. You call create_random_music() in your hook, which both generates a random number and calls the hook again. That's an infinite loop, which could be avoided if you didn't call the hook from inside the hook like that. Also, you'll have to generate a number on the server and send it to clients for it all to be static---again a reason why you should use net messages over hooks.
yeah, thank you, hook.Call("CreateRandMusic") must be hook.Call("DoSomethingElse").
Can you please provide to me example of serverside random generation?
That code specific for prophunters gamemode. cl_endroundboard displayerd at the end of round.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Serverside generated random
« Reply #3 on: April 05, 2015, 02:18:28 pm »
Here's a basic example of generating a random number and sending it to clients. This won't do what you want exactly, of course, but it'll work as a base for you to work with. This uses network messages, and keeps the generated random number constant across clients.

SERVER:
Code: Lua
  1. util.AddNetworkString("random_number")
  2.  
  3. -- serverside function to send a random number to the client - call however you want
  4. function generate_and_send()
  5.    -- edit for your desired range
  6.    num = math.random(1, 7)
  7.  
  8.    for _, ply in pairs(player.GetAll()) do
  9.       net.Start("random_number")
  10.       -- you shouldn't need more than 32 bits
  11.       net.WriteUInt(num, 32)
  12.       net.Send(ply)
  13.    end
  14. end

CLIENT:
Code: Lua
  1. local num
  2.  
  3. net.Receive("random_number", function()
  4.    num = net.ReadUInt(32)
  5.  
  6.    print(num) -- replace with what you want to do with the num variable
  7. end)
bw81@ulysses-forums ~ % whoami
Homepage

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Serverside generated random
« Reply #4 on: April 06, 2015, 06:46:51 am »
thank you for good example, but I don't know why it doesn't work for me :(  :(  :(
What I do:
in file sv_rounds.lua
Code: Lua
  1. include("sh_endroundboard.lua")
  2. ...
  3. function GM:EndRound(reason)
  4.     generate_and_send()
  5. ....
  6. end
  7.  
file sh_endroundboard.lua
Code: Lua
  1. if SERVER  then
  2.  util.AddNetworkString("random_number")
  3.      
  4.     -- serverside function to send a random number to the client - call however you want
  5.     function generate_and_send()
  6.       -- edit for your desired range
  7.       end_music = math.random(0, 8)
  8.      
  9.       for _, ply in pairs(player.GetAll()) do
  10.          net.Start("random_number")
  11.          -- you shouldn't need more than 32 bits
  12.          net.WriteUInt(end_music, 32)
  13.          net.Send(ply)
  14.       end
  15.     end
  16. end
  17.  
in cl_endrounboard
Code: Lua
  1. include("sh_endroundboard.lua")
  2. ....
  3. SetVisible(true)
  4. local end_music
  5.        
  6.     net.Receive("random_number", function()
  7.     end_music = net.ReadUInt(32)
  8.  
  9.     end)
  10.          if end_music == 0 then
  11. ....
  12.  
In console no errors, but end_music == nil
« Last Edit: April 06, 2015, 06:55:34 am by ZeD »

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Serverside generated random
« Reply #5 on: April 06, 2015, 08:56:40 am »
thank you for good example, but I don't know why it doesn't work for me :(  :(  :(
What I do:
in file sv_rounds.lua
Code: Lua
  1. include("sh_endroundboard.lua")
  2. ...
  3. function GM:EndRound(reason)
  4.     generate_and_send()
  5. ....
  6. end
  7.  
file sh_endroundboard.lua
Code: Lua
  1. if SERVER  then
  2.  util.AddNetworkString("random_number")
  3.      
  4.     -- serverside function to send a random number to the client - call however you want
  5.     function generate_and_send()
  6.       -- edit for your desired range
  7.       end_music = math.random(0, 8)
  8.      
  9.       for _, ply in pairs(player.GetAll()) do
  10.          net.Start("random_number")
  11.          -- you shouldn't need more than 32 bits
  12.          net.WriteUInt(end_music, 32)
  13.          net.Send(ply)
  14.       end
  15.     end
  16. end
  17.  
in cl_endrounboard
Code: Lua
  1. include("sh_endroundboard.lua")
  2. ....
  3. SetVisible(true)
  4. local end_music
  5.        
  6.     net.Receive("random_number", function()
  7.     end_music = net.ReadUInt(32)
  8.  
  9.     end)
  10.          if end_music == 0 then
  11. ....
  12.  
In console no errors, but end_music == nil
Because you'll have to work with the end_music variable in the callback function. It won't be assigned until the net message is received.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Serverside generated random
« Reply #6 on: April 06, 2015, 09:23:51 am »
i.e., note how the print is inside the net.Receive callback.

btw - just a nit, if you're sending to all clients, might as well turn it into a net.Broadcast and save some cycles...

Code: Lua
  1. if SERVER  then
  2.  util.AddNetworkString("random_number")
  3.  
  4.     function generate_and_send()
  5.         end_music = math.random(0, 8)
  6.         net.Start("random_number")
  7.              net.WriteUInt(end_music, 32)
  8.         net.Broadcast()  
  9.     end
  10.    
  11. else
  12.  
  13.     net.Receive("random_number", function()
  14.         local end_music = net.ReadUInt(32)
  15.         print (end_music)
  16.     end)
  17.  
  18. end
  19.  
  20.  
  21.  
« Last Edit: April 06, 2015, 09:26:29 am by Buzzkill-THABB »

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Serverside generated random
« Reply #7 on: April 06, 2015, 10:02:25 am »
i.e., note how the print is inside the net.Receive callback.

btw - just a nit, if you're sending to all clients, might as well turn it into a net.Broadcast and save some cycles...

Code: Lua
  1. if SERVER  then
  2.  util.AddNetworkString("random_number")
  3.  
  4.     function generate_and_send()
  5.         end_music = math.random(0, 8)
  6.         net.Start("random_number")
  7.              net.WriteUInt(end_music, 32)
  8.         net.Broadcast()  
  9.     end
  10.    
  11. else
  12.  
  13.     net.Receive("random_number", function()
  14.         local end_music = net.ReadUInt(32)
  15.         print (end_music)
  16.     end)
  17.  
  18. end
  19.  
  20.  
  21.  
*** Bytewave forgets about net.Broadcast()
bw81@ulysses-forums ~ % whoami
Homepage

Offline ZeD

  • Newbie
  • *
  • Posts: 20
  • Karma: 0
Re: Serverside generated random
« Reply #8 on: April 06, 2015, 10:21:45 am »
thank you guys, that noticed my mistakes))karma+
« Last Edit: April 06, 2015, 10:47:09 am by ZeD »

  • Print