panic: runtime error: index out of range [0] with length 0 #19

Closed
opened 2025-08-20 10:21:45 +00:00 by Furkan · 10 comments

This code gives the error : panic: runtime error: index out of range [0] with length 0

When I change the type from Color to int and use hex values it works but when I use structs it doesn't work

main() {
	title := "Title\0"
	raylib.InitWindow(1280, 720, title.ptr)
	raylib.SetTargetFPS(165)

	loop {
		if raylib.WindowShouldClose() {
			return
		}

		raylib.BeginDrawing()

		col := Color{12, 12, 30, 255}
		raylib.ClearBackground(col)

		raylib.EndDrawing()
	}
}

Vector2 {
	x float
	y float
}

Color {
	r byte
	g byte
	b byte
	a byte
}

extern {
	raylib {
	    InitWindow(width int, height int, title *byte)
	    WindowShouldClose() -> bool
        SetTargetFPS(fps int)
	    BeginDrawing()
	    EndDrawing()
	    ClearBackground(color Color)
	    DrawRectangleV(position Vector2, size Vector2, color Color)
	}
}
This code gives the error : panic: runtime error: index out of range [0] with length 0 When I change the type from Color to int and use hex values it works but when I use structs it doesn't work ``` main() { title := "Title\0" raylib.InitWindow(1280, 720, title.ptr) raylib.SetTargetFPS(165) loop { if raylib.WindowShouldClose() { return } raylib.BeginDrawing() col := Color{12, 12, 30, 255} raylib.ClearBackground(col) raylib.EndDrawing() } } Vector2 { x float y float } Color { r byte g byte b byte a byte } extern { raylib { InitWindow(width int, height int, title *byte) WindowShouldClose() -> bool SetTargetFPS(fps int) BeginDrawing() EndDrawing() ClearBackground(color Color) DrawRectangleV(position Vector2, size Vector2, color Color) } } ```
Owner

I'll boot up Windows and look into the details.

From the source alone I am guessing that the reasons are:

1.) "float" type isn't implemented yet but used in Vector2. (note to myself: add a proper error message)
2.) Passing structs by value to external DLL calls isn't implemented yet.

I'll boot up Windows and look into the details. From the source alone I am guessing that the reasons are: 1.) "float" type isn't implemented yet but used in Vector2. (note to myself: add a proper error message) 2.) Passing *structs* by value to external DLL calls isn't implemented yet.
Owner

At the very least, I can confirm the following code to be working on Windows. It creates an empty window:

main() {
	title := "Title\0"
	raylib.InitWindow(1280, 720, title.ptr)
	raylib.SetTargetFPS(165)

	loop {
		if raylib.WindowShouldClose() {
			return
		}

		raylib.BeginDrawing()
		raylib.EndDrawing()
	}
}

extern {
	raylib {
		InitWindow(width int, height int, title *byte)
		WindowShouldClose() -> bool
		SetTargetFPS(fps int)
		BeginDrawing()
		EndDrawing()
	}
}
At the very least, I can confirm the following code to be working on Windows. It creates an empty window: ``` main() { title := "Title\0" raylib.InitWindow(1280, 720, title.ptr) raylib.SetTargetFPS(165) loop { if raylib.WindowShouldClose() { return } raylib.BeginDrawing() raylib.EndDrawing() } } extern { raylib { InitWindow(width int, height int, title *byte) WindowShouldClose() -> bool SetTargetFPS(fps int) BeginDrawing() EndDrawing() } } ```
Author

yes also if you use integers as colors you can clear window to any color

So probably

2.) Passing structs by value to external DLL calls isn't implemented yet.

yes also if you use integers as colors you can clear window to any color So probably > 2.) Passing structs by value to external DLL calls isn't implemented yet.
Owner

The panic: runtime error has been fixed in cli/q@aa5a28d2bf which now shows an error message.

Currently, struct initialization as a value type must be exhaustive (all fields must be initialized) and the field names must be specified (can be out-of-order, though). I realize that sometimes developers want a short way to initialize structs, but such a method does not exist yet.

The `panic: runtime error` has been fixed in https://git.urbach.dev/cli/q/commit/aa5a28d2bf56c0fa41e9664a2c8263e9b8f916ee which now shows an error message. Currently, struct initialization as a value type must be exhaustive (all fields must be initialized) and the field names must be specified (can be out-of-order, though). I realize that sometimes developers want a short way to initialize structs, but such a method does not exist yet.
ed self-assigned this 2025-08-20 13:30:44 +00:00
ed added spent time 2025-08-20 13:31:11 +00:00
30 minutes
Owner

Struct value passing fixed in cli/q@7324b97ccd.
Note that this currently only works for structs up to a size of 8 bytes (1x int64).
Color has a size of 4 bytes so that works.

16 byte structs are handled differently, but I'll deal with them in a future commit.

Struct value passing fixed in https://git.urbach.dev/cli/q/commit/7324b97ccd11d343b93284f9951ff3a774b45053. Note that this currently only works for structs up to a size of 8 bytes (1x int64). Color has a size of 4 bytes so that works. 16 byte structs are handled differently, but I'll deal with them in a future commit.
ed added spent time 2025-08-20 15:40:00 +00:00
2 hours
Author

I tried the example and it gave me a redish black
image


I tried to make white Color{r: 255, g: 255, b: 255, a: 255}
image


And I tried
Color{r: 0xFFFFFF, g: 255, b: 255, a: 255}

image

It seems like red channel controls everything

I'm on windows 11 intel i5 cpu

I tried the example and it gave me a redish black ![image](/attachments/66752896-3e55-42c9-b438-b78f1ca32c77) --- I tried to make white Color{r: 255, g: 255, b: 255, a: 255} ![image](/attachments/9074db74-caaf-4cb9-a638-ed2ffd30af39) --- And I tried Color{r: 0xFFFFFF, g: 255, b: 255, a: 255} ![image](/attachments/019d7249-07e4-4b1a-9da2-5b57220be2a5) It seems like red channel controls everything I'm on windows 11 intel i5 cpu
Owner

I think you might have forgotten to rebuild the compiler with go build after pulling the latest git commits.

I think you might have forgotten to rebuild the compiler with `go build` after pulling the latest git commits.
Author

mb I forgot to copy new executable to path dir

mb I forgot to copy new executable to path dir
Owner

16-byte structs should theoretically work after cli/q@4caf7e0cc9.

I think we can close the issue (?) and track support for float in a separate one.

16-byte structs should theoretically work after https://git.urbach.dev/cli/q/commit/4caf7e0cc965c3aa060d25ee6f162e8bd5f908f1. I think we can close the issue (?) and track support for `float` in a separate one.
Author

I agree

I agree
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 2 hours 30 minutes
ed
2 hours 30 minutes
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#19
No description provided.