Something happening once in an if/while statement?

Blitz3D Forums/Blitz3D Beginners Area/Something happening once in an if/while statement?

Polarix(Posted March) [#1]
So like, if i have a sound loaded, and i want to play it in an if statement but that if statement is true, what would i do? Thanks


grable(Posted March) [#2]
Check the channel with ChannelPlaying before doing PlaySound.


Polarix(Posted March) [#3]
But what if I don't a death sound to play until it's time?


grable(Posted March) [#4]
It sounds like your logic is flawed. You are the one who decides when something dies and needs to play a sound or whatever.
I suggest looking into State Machines, which would help with this.


Stevie G(Posted March) [#5]
I suggest looking into State Machines, which would help with this.


It certainly would not help if you are struggling with the playing a sound. What a rediculous piece of advice!

Let's assume you've loaded your sound ..

Global SOUNDdead = Loadsound( "../MyDeathSound.wav")
Global CHANNELdead


It's always a good idea to check it's loaded by checking if SOUNDdead > 0. e.g.

If SOUNDdead = 0
   Runtimeerror "Failed to load sound"
   End
Endif


Assume you have a character who has 100% Health defined like so:

Global Health = 100


You would reduce the heath when hurt. It's up to you to decide what circumstances result in loss of health. Once this is reduced to zero you would then play the dead sound:

If MyCharacterIsHurt then
   Health = Health - 5
   If Health = 0 then
       CHANNELdead = Playsound( SOUNDdead)
   Endif
Endif


Blitz will automatically assign the next availble sound channel to ChannelDead.

Something along these lines.

Stevie


grable(Posted March) [#6]

It certainly would not help if you are struggling with the playing a sound. What a rediculous piece of advice!
From my understanding, its not the sound playing he is struggling with, but states in general.
A state machine isnt some advanced programing topic, its something we do implicitly everywhere, even your example above with MyCharacterIsHurt is a state, though a boolean one.

I suspect he is doing everything at once, having NO state at all. Reading about statemachines and how they work will give insight into how to handle state in general. IMO.


Stevie G(Posted March) [#7]
Fair enough. I assumed you were talking about coding some full blown FSM which would be pretty advanced for someone struggling with the simplest logic.

Like you, I took a guess - you are probably correct.


Matty(Posted March) [#8]
pseudocode

if character has just died
play sound 'character died'
endif

...

to check if character has just died as opposed to is dead (as you don't want sound playing over and over while the hitpoints are less than or equal to zero):

if character hitpoints <= 0 and character hitpoints on previous frame > 0
character has just died
endif


Kryzon(Posted March) [#9]
A "death" is an event. You want to do something when that happens --like playing a sound-- and then move on, not playing that sound anymore until another death happens.
In case that sound keeps playing then you know that something is wrong with your logic.

Touching an enemy, crossing the screen, the health hitting zero, pressing a key. These are all events.


Polarix(Posted March) [#10]
Yeah but like when a statement is true it just keeps playing the sound a bunch of times at once


Stevie G(Posted March) [#11]
There's alot of ways to skin a cat, this is one ...

Global SOUNDdead = loadsound( ... )
Global CHANNELdead
Const PS_ALIVE = 1
Const PS_DEAD = 2
Const PS_RESPAWN = 3
Global PLAYER_STATE = PS_ALIVE
Global PLAYER_HEALTH = 100

Select PLAYER_STATE
   case PS_ALIVE
       {Movement / shooting / damage etc..}
       If PLAYER_HEALTH <= 0
            PLAYER_STATE = PS_DEAD
       Endif
   case PS_DEAD
        CHANNELdead = Playsound( SOUNDdead)
        PLAYER_STATE = RESPAWN
   case PS_RESPAWN
        {Reset health, position etc.. }
        PLAYER_STATE = ALIVE
        PLAYER_HEALTH = 100
End Select