• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
FGFS

how to use or ||

13 posts in this topic

Hi

this seem to be the trouble:

                                    if (rainP <= 0.3 || temperatureC < 0 || cloudAlt <= altibaro){
                                            alSourceStop(Sources[0]);
                                    }else{
                                        alSourcePlay(Sources[0]);
                                    }

I want to stop the sound if either rain is less 0.3 or temp below zero or cloud altitude is below.

But the sound keeps playing even if temp is below zero. Am I doing something wrong and

how to write the above?

Thanks

 

0

Share this post


Link to post
Share on other sites
Do you run this code repeatedly while the sound is playing?
1

Share this post


Link to post
Share on other sites

Check your values like Buckeye and unbird suggests, also Wooh is hinting at something here:

 

Why don't you make a flag, a boolean telling if the sound is playing or not, and track state changes rather than be calling playsound or stopsound on every iteration?

0

Share this post


Link to post
Share on other sites

That should actually work (with the operator precedence there should be no confusion, so no brackets needed).
 
There must be something else off (maybe the sound switch off?). Go through with your debugger and inspect those values.
 
As a general hint: If you don't know/trust what's going on, declare a couple of temporary variables (or even functions), and inspect those, e.g:
 

bool b1 = rainP <= 0.3;
bool b2 = temperatureC < 0;
bool b3 = cloudAlt <= altibaro;
bool b = b1 || b2 || b3;
Edit: Thinking about it, using parentheses explicitly (even if not needed) isn't that bad. Not only for readability.

Edit2: @Buckeye: Question now is rather who has ninja'd whom ? wink.png

 

 

This is a fantastic idea.  In fact, const bools are often used in UE4 for readability purposes.  Variables and constants may be self-documenting by themselves, but you can take it a step further as in the below example.  For example:

// Before:
if (someDescriptiveVariable < someDescriptiveNumber) {
    // Do stuff
}

// After:
const bool bSomeDescriptiveBool = (someDescriptiveVariable < someDescriptiveNumber);
if (bSomeDescriptiveBool) {
    // Do stuff
}

It may seem unnecessary to some, but let me tell you that it makes for very clean and self-documenting code that other people (not just yourself) can read.  I also agree that expressions are generally cleaner when they're surrounded by parentheses.

 

As for the actual topic, the best thing to do here (as previously mentioned) is to display the values in one way or another.

Edited by Phil123
0

Share this post


Link to post
Share on other sites

It should be noted that performing your comparisons using multiple Boolean variables means that you cannot take advantage of short-circuit evaluation.

Edited by Chris_F
2

Share this post


Link to post
Share on other sites

Thanks. || was ok but a displaced clamp.  Below seems fine:

 

                                    if(state != AL_PLAYING)
                                    {
                                        SourcesPos[0][0] = 0.0;
                                        SourcesPos[0][1] = 0.0;
                                        SourcesPos[0][2] = 0.0;

                                        alSourcefv(Sources[0], AL_POSITION, SourcesPos[0]);
                                    }
                                    if (rainP <= 0.3 || temperatureC < 0 || cloudAlt <= altibaro){
                                            alSourceStop(Sources[0]);
                                    }else if(state != AL_PLAYING){
                                        alSourcePlay(Sources[0]);
                                    }

0

Share this post


Link to post
Share on other sites

 

 

It should be noted that performing your comparisons using multiple Boolean variables means that you cannot take advantage of short-circuit evaluation.

 

VS2012, compiled with default release mode settings:

..

Resulting disassembly:

...

Compilers are very, very smart.

 

They are, hopefully, also very smart not to do that when the evaluations of the temporary variables have side effects because that would change the meaning of the program. Your example works because the individual conditions does not have any observable side effects. If the expressions have side effects, or otherwise relies on short circuiting to prevent the evaluation of an expression in the condition (for example checking for null-pointer before accessing a member of an object), expanding the expressions to temporary variables before the if-statement won't work.

 

 

OP's code does not include side effects.

 

On the other hand, in the case that the statements do (or may) have side effects, the compiler will obviously not screw it over.

 

In that case, though, it's better to just write the test sequence out clearly rather than relying on obscure details of the spec. If you want to do something obvious like null checking, you can just do that into the temp:

bool a = (ptr && ptr->something);
bool b = stuff;
if(a || b) {etc}
0

Share this post


Link to post
Share on other sites


Compilers are very, very smart.

 

But they don't perform magic. I'm specifically referring to cases such as this:

 

#include <iostream>

bool SomeExpensiveComputation()
{
    std::cout << "D'oh!" << std::endl;
    return true;
}

int main()
{
    int value = 42;
   
    if ( value > 0 || SomeExpensiveComputation() )
    {
        std::cout << "Example 1" << std::endl;
    }
   
    bool b1 = value > 0;
    bool b2 = SomeExpensiveComputation();
   
    if ( b1 || b2 )
    {
        std::cout << "Example 2" << std::endl;
    }
}

 

http://coliru.stacked-crooked.com/a/339dc09bdbc542dc

0

Share this post


Link to post
Share on other sites

That's just begging for a long debug session some time in the distant future. Stuff like that is hard to spot if you aren't looking for it.

bool execute = false;
if(value > 0) {execute = true;}
else if(SomeExpensiveComputation()) {execute = true;}
if(execute) {std::cout << "Example 1" << std::endl;}

^ Compiles the same, but is far more clear about what's going on.

 

Alternatively, it may be a good idea to at least leave a comment indicating that you're making use of a short circuit.

Edited by Khatharr
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0