-2

I'm getting this problem where I get this error message;

error: 'ballPosition' undeclared (first use in this function)

This is my code

#include"raylib.h"

int main()
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth,screenHeight, "ArcadeGame");
    
    while(!WindowShouldClose())
    {
        ballPosition = GetMousePosition();
        BeginDrawing();
            DrawCircleV(ballPosition, 35, DARKBLUE);
             ClearBackground(RAYWHITE);
             
        EndDrawing();
    }
    
    CloseWindow();
    
    return 0;
}

I tried to look at some examples but it still wont work.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

2 Answers2

1

In C, every variable needs a defined type. When you declare or define a new variable, you have to give a type (there are some exceptions where int is used without a type given but that isn't relevant for now). You didn't specify a type for ballPosition, so the compiler doesn't know the type and and complains.

When you look at the source code of raylib.h, you see that GetMousePosition() returns a Vector2-type value, Vector2 is a struct and typedef defined in raylib.h. Therefore it makes sense to Vector2 as type for ballPosition. You can do that by writing the type Vector2 before ballPosition is used the first time in a scope;

1

The C programming language is a so called statically typed language with explicitly typed variable declarations. This means that every variable has a fixed type which must be specified in a variable declaration. In this case we can see at line 1129 in https://github.com/raysan5/raylib/blob/master/src/raylib.h that the function GetMousePosition is declared as

RLAPI Vector2 GetMousePosition(void);  // Get mouse position XY

so it returns a value of type Vector2. Therefor you need to declare the variable ballPosition with this type:

#include"raylib.h"

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 450;
    Vector2 ballPosition;

    InitWindow(screenWidth, screenHeight, "ArcadeGame");
    while (! WindowShouldClose()) {
        ballPosition = GetMousePosition();
        BeginDrawing();
        DrawCircleV(ballPosition, 35, DARKBLUE);
        ClearBackground(RAYWHITE);
        EndDrawing();
    }
    CloseWindow();
    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60