Scripts / Script discussion - random variable

Author
Message
Cobbs
4
Years of Service
User Offline
Joined: 20th Sep 2020
Location:
Posted: 14th Feb 2021 14:05
Hi, I'm wondering how, in lua, you generate a random number to use as a variable. The goal would be, for example, to generate a number from 1-10 and play a sound effect based on what number was randomly selected.
PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Feb 2021 15:30 Edited at: 14th Feb 2021 15:30
variable = math.random(min,max)
so
local slot = math.random(0, 4)
PlaySound(e, slot)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 14th Feb 2021 15:41
To play 10 sounds you will be better off using global sounds as entities only have 5 sound slots.

LoadGlobalSound( <filename>, 1 )
LoadGlobalSound( <filename>, 2 )
...
LoadGlobalSound( <filename>, 10 )

PlayGlobalSound( math.random( 1, 10 ) )
Been there, done that, got all the T-Shirts!
PM
Cobbs
4
Years of Service
User Offline
Joined: 20th Sep 2020
Location:
Posted: 15th Feb 2021 22:56 Edited at: 15th Feb 2021 23:10
I changed it to Loop instead of Play, so it doesn't end. Pretty key.

How can I get activating the script again to stop the current song from playing? Currently, with that script, if the next song is different it will play on top. If the randomly selected song is the same, then it will stop and restart it. I don't know what command tells the global sounds to end.

Edit: Figured it out. Enter this for each global sound added. Put it before you trigger the next song.
StopGlobalSound(1)
StopGlobalSound(2)
etc


So, this is working so far:

in the init section:
LoadGlobalSound( "audiobank\\music\\1.wav", 1 )
LoadGlobalSound( "audiobank\\music\\2.wav", 2 )

in the main section:
if PlayerDist < 90 then
StopGlobalSound(1)
StopGlobalSound(2)
LoopGlobalSound( math.random ( 1, 2 ) )
Destroy(e)
end



So if you're using 100 sound effects or songs or whatnot, you'd have to have 100 lines saying to stop each of the global sounds that can possibly play, since you don't know what's playing and this code doesn't cover that.
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 15th Feb 2021 23:16
how about test:
for s = 1,100 do
StopGlobalSound(s)
end

Anyone ever try it? Make sure number fits what is actually loaded.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 16th Feb 2021 00:40
You can check if a global sound is playing.
e.g.:

if GetGlobalSoundPlaying( 1 ) == 1 then
Been there, done that, got all the T-Shirts!
PM
Cobbs
4
Years of Service
User Offline
Joined: 20th Sep 2020
Location:
Posted: 3rd Mar 2021 00:50
Hmm I haven't tried the 's' variable option, or any variable and tying it to a number amount. How do you actually put that in the lua script?
Currently I'm doing the solution I posted. It would be nice to combine all the possible numbers in one line of code. IE StopGlobalSound(1-100) if that would stop global sounds 1 through to 100, that'd be clean. As is, I'd be putting 100 lines of code instead.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 3rd Mar 2021 10:24 Edited at: 3rd Mar 2021 10:27
Been there, done that, got all the T-Shirts!
PM
Cobbs
4
Years of Service
User Offline
Joined: 20th Sep 2020
Location:
Posted: 4th Mar 2021 14:04 Edited at: 4th Mar 2021 15:38
Having trouble with inconsistencies that I'm working on figuring out.

I put 3 songs in the audiobank/music folder and have them loaded at the start of the game with
LoadGlobalSound( "audiobank\1.wav", 1 )
for each song.
In test mode, the activated script in-game plays one of the three random songs successfully.
However, when I export the game, only 2 of the 3 songs are in the game's exported directory.

If I move the songs to a different folder and direct them there, then no songs load into the exported game. But things still work in test mode. Why is the game not compiling properly? What more do I have to do to signal the game's exporter to include these files consistently?

EDIT: The problem may be that telling the game to play one of these randomly doesn't trigger the exporter to include the files? I just included a starting script that directly plays all 3 songs and stops them right away:
PlayGlobalSound(1)
PlayGlobalSound(2)
PlayGlobalSound(3)
StopGlobalSound(1)
StopGlobalSound(2)
StopGlobalSound(3)
Destroy(e)
It seems like this has communicated to the exporter to include these files in the exported game. I think the random function is the issue here, can't use it alone maybe. Obviously I'm new to this lol.
PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 4th Mar 2021 17:34
you can assign them to a sound slot on any of your entities and the standalone will copy them over
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Cobbs
4
Years of Service
User Offline
Joined: 20th Sep 2020
Location:
Posted: 4th Mar 2021 18:07
Yeah that's a solution too, but this way seems better considering I plan to use much more than 3
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 4th Mar 2021 18:59
This works fine for me.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
GoDevils
10
Years of Service
User Offline
Joined: 24th Sep 2014
Location: Arizona USA
Posted: 15th Mar 2021 01:17
@ Cobb

"How can I get activating the script again to stop the current song from playing? Currently, with that script, if the next song is different it will play on top. If the randomly selected song is the same, then it will stop and restart it. I don't know what command tells the global sounds to end"

I just went through this. See current post titled: Sound Command Questions

"THERE IS NO SPOON"

AMD 6300 6 core 3.5 ghz, Windows 10, 8GB ram, GTX 1060 2GB ram
meatball
3
Years of Service
User Offline
Joined: 20th Mar 2021
Location:
Posted: 21st Mar 2021 16:41
I was working on something on GameGuru and i was wondering how it would be possible to make any item (a handgun, etc..) work as a key for a door (ex.: upon picking up a gun a specific door opens), and then afterwards be still able to use the gun normally?
PM

Login to post a reply

Server time is: 2024-12-22 01:59:48
Your offset time is: 2024-12-22 01:59:48