Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by jcl. 04/20/24 08:52
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (7th_zorro, Aku_Aku, henrybane, flink, 1 invisible), 712 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Non repeating random #472650
05/10/18 18:20
05/10/18 18:20
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Hello friends,

I have a series of different sound effects, for example, metal impact sounds. When a sword hits a shield, I want it play a random impact sound, but never 2 of the same sounds in a row. Whats the pseudo code for this?

Re: Non repeating random [Re: jumpman] #472653
05/10/18 18:53
05/10/18 18:53
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
play next sound into an array when the random sample returns the same of the previous play.

Code:
SOUND *sndArray[SOUND_COUNT];
...
int _i = random(SOUND_COUNT);
if(_i == my->skSound)
	my->skSound = (_i + 1) % SOUND_COUNT;
else
	my->skSound = _i;
ent_playsound(me, sndArray[my->skSound], 100);



Salud!

Re: Non repeating random [Re: txesmi] #472661
05/12/18 05:02
05/12/18 05:02
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Hi txesmi!!

Thank you for taking the time! skSound is a local variable correct?

How do you fill that sndArray with sounds? Is each sound in that array a string?

Re: Non repeating random [Re: jumpman] #472663
05/12/18 07:26
05/12/18 07:26
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
You are welcome laugh

Originally Posted By: jumpman
skSound is a local variable correct?

Yes, a variable to save the index of the played sound at the end, local or not.

Originally Posted By: jumpman
Is each sound in that array a string?

Nops. They are pointers to SOUND structs.

Originally Posted By: jumpman
How do you fill that sndArray with sounds?

I use to build some helper functions by the array: create, destroy and functionality.

Code:
#define SND_METAL_COUNT     5
SOUND *sndMetal[SND_METAL_COUNT];

void sndMetalCreate () {
	sndMetal[0] = snd_create("metald01.wav");
	sndMetal[1] = snd_create("metald02.wav");
	sndMetal[2] = snd_create("metald03.wav");
	sndMetal[3] = snd_create("metald04.wav");
	sndMetal[4] = snd_create("metald05.wav");
}

void sndMetalRemove () {
	int _i = 0;
	for (; _i<SND_METAL_COUNT; _i+=1)
		snd_remove(sndMetal[_i]);
}

SOUND *sndMetalRandom () {
	static int _indexOld = 0;
	int _i = random(SND_METAL_COUNT - 1);
	if (_i >= _indexOld)
		_i += 1;
	_indexOld = _i;
	return sndMetalArray[_i];
}

...

sndMetalCreate ();
...
ent_playsound(me, sndMetalRandom(), 100);
...
sndMetalRemove ();


Notice that I used a little bit cheaper algorithm. It also mantains a uniform probability the other did not.

Salud!

Last edited by txesmi; 05/12/18 07:44. Reason: error!! fixed
Re: Non repeating random [Re: txesmi] #472717
05/15/18 19:04
05/15/18 19:04
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Thank you txesmi!

Is constantly creating and removing sounds OK for the engine?

Re: Non repeating random [Re: jumpman] #472720
05/15/18 20:28
05/15/18 20:28
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
yes but keep in mind that this might cause stuttering while the file is being loaded.

usually you create a sound once and keep it (a more sophisticated approach would be only loading sounds in areas of the game where they are needed)

I'm also sure that txesmi's example was meant that way.

when setting up your application you call sndMetalCreate() to create all the sounds needed and you then can play them as much as you want.
once you don't need the sounds anymore (like closing your application) you call sndMetalRemove() to remove those sounds again.


POTATO-MAN saves the day! - Random
Re: Non repeating random [Re: Kartoffel] #472721
05/15/18 20:39
05/15/18 20:39
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Originally Posted By: Kartoffel
I'm also sure that txesmi's example was meant that way.

Yes, of course.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1