Got Warning for nebula.width / .height

EDIT: For everyone in Moving Hazard lesson who has this error - the solution is in the comment section of the next lesson (thanks guys).

 Rectangle nebulaRec{0.0, 0.0, nebula.width/8.f, nebula.height/8.f};

or

 Rectangle nebulaRec{0.0, 0.0, (float)nebula.width/8, (float)nebula.height/8};

Hi,
when I define attributes of nebulaRec, precisely nebula.width / .height in short form:

 Rectangle nebulaRec{0.0, 0.0, nebula.width/8, nebula.height/8}

I get this warning:

narrowing conversion of ‘(nebula.Texture::height / 8)’ from ‘int’ to ‘float’ [-Wnarrowing]

Everything works fine as it should but I am curious why it has happened.

Also, I’ve written different code to solve the challenge. It’s correct for now, we’ll see in the future if the cascade system will be necessary.

            if((runningTime >= updateTime)
            && (!isInAir)) {

                runningTime = 0.0;
            //animation of scarfy - 6 images changing frame by frame
                scarfyRec.x = frame * scarfyRec.width;
                frame++;
                    if(frame > 5) {
                        frame=0;
                    }
            }

Thanks for sharing and updating this lectures Q&A

To chime in quickly regarding this particular warning.

This particular narrowing conversion would only cause an issue if your project was using really large numbers (something like 2^30), which in most cases isn’t reached for games. That said, learning about what it is and how to resolve it is also good :slight_smile:

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms