My problem is that I wish to use a single mouse click (or key press) to "turn on" a set of conditions then I will "turn off" the conditions when a different requirement is met. Alas! Every time I release the mouse button the bool reverts to its initialized state. I do not want it to do that and I do not want to set it to "toggle" on and off with each mouse click either. Here are some examples of what has failed:

// first, simple failure

bool start = false;
if (dbMouseClick())
    start = true;

// another unsuccessful attempt

bool start = false;
int click = 0;
if (dbMouseClick ())
    ++click
if (click > 0)
    start = true;

// more pain here

bool start = false;
if (dbMouseClick())
    start = true;
if (start == true)
{
    if (dbMouseClick() == false)
    start = true;
}

// and my desperate, embarrassing last ditch effort

dbSprite (100, 0, 0, 100);
bool start;
if (dbSpriteExist (100))
{
    start = false;
    if (dbMouseClick ())
        dbDeleteSprite (100);
}
if (dbSpriteExist (100) == false)
    start = true;

Even that last and rather cumbersome bit of code failed because the sprite kept redrawing!!! It is obvious that my mind is on the wrong track. Would anyone be willing to help derail it. I have been at this for WAY too long. This is one of the last hang-ups I have left before completing my simple game project and moving onto bigger and more daunting brain twisters.
Thanks for any help.

Dani AI

Generated

The core problem is scope and input semantics: the flag you want to persist is being re-created each frame and the mouse call you used reports the current button state (held), not a one-time event. Declare the persistent state outside the per-frame block (or as a static/global), and convert the mouse level input into a single “pressed this frame” event by comparing current vs previous mouse state. This is standard C++ storage behavior (local/automatic variables are created and torn down with their block). (cppreference.patrickfasano.com)

A minimal, clean pattern (keeps the state until some other condition clears it) is edge detection: read the mouse each frame, detect a rising edge (was not pressed last frame, is pressed this frame), and set your persistent flag then. Use any other condition you need to clear that flag later (another key, collision, timeout, whatever your game requires). The idea of comparing current and previous input states to detect a single press is a common solution. ()

Example (place the first two variables once, before your main loop):

bool started = false;
int prevMouse = 0;

while ( LoopGDK() ) {
    int currMouse = dbMouseClick();   // read mouse level
    if (currMouse != 0 && prevMouse == 0) {
        started = true;              // mouse pressed THIS frame
    }

    // clear condition (replace with your own requirement)
    if (started && someStopCondition()) {
        started = false;
    }

    prevMouse = currMouse;

    // use 'started' for persistent behavior here

    dbSync();
}

If you used the sprite-existence trick to remember state, that works but is heavier than necessary—create visual sprites once and show/hide them or use a boolean state to control drawing instead of creating/deleting every frame. The DarkGDK examples follow the create-once / update-in-loop pattern for sprites. (jairxpereira.github.io)

Troubleshooting tips: log prev/curr values for a few frames to confirm the rising edge, make sure the persistent variables are declared outside the loop, and choose a clear stop condition that is only true when you want the state to reset.

So, OK, I have kind of solved the problem. All I did was a varient on an already tried bit of code.

bool start = false;
if (dbMouseClick())
    dbSprite (100, 0, 0, 100);
if (dbSpriteExist (100))
    start = true;

That works well enough but I think that it is a sloppy solution and would be open to a far more efficient way to solve this problem. Any takers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.