#picking random clips

1 messages · Page 1 of 1 (latest)

inland bane
#

foo

pulsar cobalt
#

wait no why random

inland bane
#

Show me your current code. I'd like to see what you are doing.

#

!code

jolly swanBOT
inland bane
#

It doesn't have to be working code

#

I just want to see your intent.

pulsar cobalt
#

There is nothing so far.

inland bane
#

I'm looking for wherever this came from.

mellow storm
pulsar cobalt
#

That was made as an example and deleted afterwards

inland bane
#

Okay.

pulsar cobalt
#

some of my first projects worrked this way

mellow storm
#

(i'll go backread rq and let fen handle it)

inland bane
#

If you only have one clip of a certain type, you can simply store it in a field.

#
public AudioClip shootClip;
#

that's it; you're done

pulsar cobalt
#

writing a new line of code and a variable name for every ne wsound feels wrong

#

Something that wwould be perfect is a AudioStorage.clip, with .clip being an AudioCLip from a list, but accessed via . instead of []

inland bane
#

A "shoot" clip is conceptually different from a "jump" clip. You use them in different places, and they have very clear meanings.

I think it's perfectly fine to do that.

inland bane
#

You've just moved the fields somewhere else.

#

Now, this could be reasonable if you need a fixed set of sounds.

pulsar cobalt
#

It's easier to + a new AudioClip in a list rather than changing code

inland bane
#

You aren't just going to put it in a list and never use it.

#

There is nothing wrong with doing things explicitly.

#

I used to write a lot more "implicit" code. I'd use GetComponent instead of adding a field and assigning a reference, for example.

#

Now, with more experience, I try to avoid that.

pulsar cobalt
#

I feel something bad about both of these things

mellow storm
#

exo rq just to narrow the scope of what you're asking; do you actually need sound variants for each action

#

or do you just need to keep track of a single clip for each action

pulsar cobalt
#

I mean that'd be a separate system, a special object that holds a bunch of clips with a method that returns a random one. That way accessing a .shoot0 is different than .shoot, one playing a specific sound and .shoot likely playing a random one out of a list of it's variants.

#

I think I should say how I imagine this working in the end, and then work our way back to a solution

mellow storm
#

you should probably not have 2 separate systems for "audio" vs "audio with variants"

inland bane
#

There is nothing wrong with directly referencing the assets you need.

#

You seem fixated on this.

#

If anything, I think it's a great idea.

mellow storm
#

if any have variants, you could just use "audio with variants" and set the ones without variants as 1-member lists

inland bane
#

I think you're trying to solve a problem that simply does not exist.

pulsar cobalt
#

I don't like the idea of a C# script changing size based on the amount of audio files. That's why I'd prefer a list. But then, to reference a clip I need it's index, which I only imagine making legible through a codename to index system

inland bane
#

The script will, indeed, change if you add new kinds of audio

#

But that's a given.

#

You wouldn't add a "jump sound" field to your class and then nothing with it

#

Surely you would add some code that plays the sound.

#

But the script doesn't have to change at all if you want to have many versions of the same kind of sound

#
public AudioSource source;
public List<AudioClip> jumpSounds;

public void Jump() {
  int index = Random.Range(0, jumpSounds.Length);
  source.PlayOneShot(jumpSounds[index]);
}
#

It doesn't matter how many unique jump sounds you have.

#

You can have one, two, ten, or a hundred.

#

Absolutely nothing in your code changes.

inland bane
pulsar cobalt
#

I'll try writing something that kinda resembles my imagined goal, and then you could tell me what's wrong with it, is that ok?

inland bane
#

That means you have a design problem! This should be setting off alarm bells!

#

You should only have to change your code when you start doing new kinds of things

inland bane
pulsar cobalt
#

It would be amazing if you turned this to a scriptable object or something, because all that happened in the main chat fried my receptors

inland bane
#

That's quite similar to what I suggested with AudioSet -- except that I would not look things up by name at all.

inland bane
pulsar cobalt
#

How can this be turned into a scriptable object, and how do I make it work?

#

Somewhat, I'm not perfectly healthy today

pulsar cobalt
inland bane
#

You can create a script named AudioSet and put this inside (you'll need to add some using directives; have your IDE do this for you).

#

also, dammit, it's Count!

#

I always forget that

pulsar cobalt
#

No, nevermind, I must've messed something up earlier

inland bane
#

(make sure you include my edit; I wrote Length instead of Count)

#

forgor

pulsar cobalt
#

I did

inland bane
#

This will allow you to create an AudioSet from the Create Asset menu. You can give it some audio clips in the inspector.

#

You can then reference this AudioSet wherever you need it.

pulsar cobalt
#

Is Resources > Audio > fire.asset a bad way to put it in?

mellow storm
# pulsar cobalt I still don't understand what's so wrong with using names

there's a difference between names and identifiers
names are just.. names
identifiers are names that other tools (unity/your ide/c#) know about, so they can help check and verify that you didn't misspell them or anything

imagine instead of using GetComponent<Camera>() you had to do GetComponent("Camera")
that's a subtle difference in text, but in the latter case, it's inside quotes; none of the tools i mentioned will check inside. they're treated literally.
if you typo, and instead say Camrea or something, the former case will light up with errors telling you exactly what happened. you tried to use an identifier, and c# has no idea what you're talking about
but the latter case, well that's just up to the whims of GetComponent. best case scenario, it gives a warning or something at runtime, and then stuff happens without it, leading to indirect errors
worst case it just fails silently, and gives you hard-to-trace bugs

#

(also this is strings in general, so also stuff with tags, Find, Resources.Load, etc.)

inland bane
#

Just reference it!

#
public AudioSet jumpSounds;
pulsar cobalt
#

Now this is my original problem, what do I put this in

inland bane
#

Where do you actually need to use these sounds?

pulsar cobalt
#

an AudioManager, with a singleton or something like that

inland bane
#

Suppose you have ten kinds of character in your game.

#

Each one has their own set of jumping sounds.

#

I wouldn't want to have to have ten audio sets named jump0, jump1, jump2, ..., jump9

#

all stuffed into a single audio manager component

#

why not just have each character refer to an audio set?

#
public class Character : MonoBehaviour {
  public AudioSet jumpSounds;
}
#

(I need to scram in about 15 minutes, by the way)

pulsar cobalt
#

You're saying it's fine for classes to have a [Header("Clips")]?

inland bane
#

Sure, if you have several audio sets you need to reference.

#

Might as well give them a nice header.

inland bane
#

The character can then get a clip and play it...or pass it to an audio manager to be played, maybe!

pulsar cobalt
#

I think I know most of what I needed, one question tho

#

AudioManager should be a singletonized object?

inland bane
#

That sounds reasonable, yes.

#

One thing tho

#

This does roughly what your code did

#

this is intended for 3D, which might not be what you're doing

pulsar cobalt
#

Also

#

how should I go about audio sources?

#

Create one dynamically whenever a sound is played just for that sound, deleting it once it finishes playing?

#

@inland bane

#

I think that's my last question for a while

mellow storm
#

you could, but you probably shouldn't

inland bane
#

That’s how PlayClipAtPoint works

#

It’ll be okay for modest numbers of sounds. If possible, just use an audio source stuck to whoever is making the sounds

#

You can use PlayOneShot there.

#

One source can handle many oneshots

pulsar cobalt
#

What about a PlaySFX function that takes a dynamicSource boolean default to false as an argument, which, if true, will create a source dynamically. Otherwise the sound is played from the fixed audio source

#

Would there be something bad about this approach?

mellow storm
#

the boolean flag, i guess, but it's not really a major issue, it just could be a problem in the future

pulsar cobalt
#

That's what I'm looking to avoid

#

What would you change here

mellow storm
#

do you need both kinds of sources?

pulsar cobalt
#

One problem I found with fixed audio sources is playing a sound while another is playing cancels out the previous one

#

I never want that to happen

#

dynamically adding a sfx audio source sounds the most reasonable. A fixed one would be good for ambiance and music

inland bane
inland bane
#

(also, gotta run now! i'll be back in several hours)