this a waste of time ?

BlitzMax Forums/BlitzMax Programming/this a waste of time ?

Paul "Taiphoz"(Posted 2011) [#1]
just sat down this morning to continue work on my current pet project, and started adding sound to my game.

Before I realized it, and without really putting much thought into it I did this.

I'm sitting here now trying to justify doing this like this istead of just global sndboom=loadsound"" and then playsound when needed.

I think before I went to bed last night I must have been thinking about this, and thinking that I might want to mess with the sound in some way, but for the life of me I have no clue what it was ROFL.


Global SoundList : TList = CreateList()

'debugstop
Global SoundEffect:TSoundEffect = New TSoundEffect
	SoundEffect.effect = LoadSound("sound/button.wav" , False)
	SoundEffect.name = "button"	
	
	SoundEffect:TSoundEffect = New TSoundEffect
	SoundEffect.name="launch"
	SoundEffect.effect = LoadSound("sound/launch.wav" , False)
	
	SoundEffect:TSoundEffect = New TSoundEffect	
	SoundEffect.name="explosion"
	SoundEffect.effect = LoadSound("sound/explosion.wav" , False)

	SoundEffect:TSoundEffect = New TSoundEffect
	SoundEffect.name="landing"
	SoundEffect.effect = LoadSound("sound/landing.wav" , False)

	SoundEffect:TSoundEffect = New TSoundEffect
	SoundEffect.name="waypoint"
	SoundEffect.effect = LoadSound("sound/waypoint.wav" , False)

	SoundEffect:TSoundEffect = New TSoundEffect
	SoundEffect.name="warning"
	SoundEffect.effect = LoadSound("sound/warning.wav" , False)




'sound
Type TSoundEffect
	Field effect:TSound
	Field name:String
	
	Method New()	
		ListAddLast(SoundList,self)		
	End Method
	
	Method Play()
		PlaySound (Self.effect)
	End Method
	
	Method kill()
		
	End method
End Type



Function PlayEffect(effect:String)
	For Local l:TsoundEffect = EachIn SoundList
		If l.name = effect
			l.play
			exit
		End if
	next
End function



GfK(Posted 2011) [#2]
Your PlayEffect function is inefficient.

Use a TMap instead, with the effect name as the Key, and the sound effect itself as the Value.


Paul "Taiphoz"(Posted 2011) [#3]
Tmap ?


Yasha(Posted 2011) [#4]
TMaps are containers that use a binary tree structure instead of a linear list. This provides O(log n) lookup time instead of O(n).

They're only actually more efficient once you get above a certain minimum size threshold (8-16 entries). For an ultrashort list like that one there's not going to be any measurable amount of time spent iterating over it anyway.


matibee(Posted 2011) [#5]
I use an entire system like this for all resources in my game (sounds, sprites, images, gui forms, even game constants etc etc) and specify them in an external script..

http://www.matibee.co.uk/temp/commands.html#RESOURCES

The game handles loading the script and all the resources it specifies, I just access them by name when I need them.

If you're really worried about iterating the list, there's no reason why you can't set up a local variable to access the resource repeatedly without having to iterate the list. Just add a Find method...

TSoundEffect

  Method Find:TSound ( name$ )
  
  End Method


Last edited 2011


Paul "Taiphoz"(Posted 2011) [#6]
your still going to need to iterate through the list to find name$ anyway.

I never actually though of controlling all media with a setup like this, but now that you have mentioned it its something I will give some thought on my next project.

Tmaps cant seem to find any documentation on them.


matibee(Posted 2011) [#7]
your still going to need to iterate through the list to find name$ anyway.


But only once. If you have mission critical resource initialised by one section of code, you can grab and store a local handle to it using the method above and avoid going through the list again.

One more thing.. if you fail to find a resource, trigger an assert..

Method Find:TSound( name$ )
 ' iterate list
   if ....
      return found
   endif 

  'NOT FOUND!
  assert(0, "Sound not found " + name$ )
End Method


No more guessing why your "SPLAT" sound isn't playing and avoid days of debugging all because you tried to play "SPLOT" instead. If that sounds dumb you ain't bin gamedev'ing long enough ;)


H&K(Posted 2011) [#8]
lol got Yavin and yasha mixed up

Last edited 2011