Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C error from V code, static array #23192

Open
HaraldWik opened this issue Dec 17, 2024 · 1 comment
Open

C error from V code, static array #23192

HaraldWik opened this issue Dec 17, 2024 · 1 comment

Comments

@HaraldWik
Copy link

HaraldWik commented Dec 17, 2024

V doctor:

V full version: V 0.4.8 fc6e481.1fae506
OS: linux, Pop!_OS 24.04 LTS
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 4800HS with Radeon Graphics

getwd: /home/admin/Projects/game-engine/src/input
vexe: /home/admin/.v/v
vexe mtime: 2024-12-16 18:15:33

vroot: OK, value: /home/admin/.v
VMODULES: OK, value: /home/admin/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.43.0
Git vroot status: weekly.2024.50-49-g1fae5069
.git/config present: true

CC version: cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9-dirty

What did you do?
./v -g -o vdbg cmd/v && ./vdbg keyboard.v

module input

import sdl

pub fn get_pressed_keys() []Key {
	mut pressed_keys := []Key{}
	numkeys := 0
	keyboard_state := sdl.get_keyboard_state(&numkeys)

	for scancode in 0 .. numkeys {
		unsafe {
			if keyboard_state[scancode] != 0 {
				if key := Key.from(scancode) {
					pressed_keys << key
				}
			}
		}
	}

	pressed_keys << get_pressed_mouse_buttons()

	if pressed_keys.len < 1 {
		return [.none]
	}

	return pressed_keys
}

pub fn is_pressed(keys ...Key) bool {
	pressed_keys := get_pressed_keys()
	for key in keys {
		if key !in pressed_keys {
			return false
		}
	}
	return true
}

@[unsafe]
pub fn is_just_pressed(key Key) bool {
	mut static pressed_keys := []Key{}
	if !pressed_keys.contains(key) && is_pressed(key) {
		pressed_keys << key
		return true
	} else if pressed_keys.contains(key) && is_pressed(key) {
		pressed_keys.filter(it != key)
		return false
	}

	return false
}

/*
func IsJustPressed(keycode string) bool {
	current := IsPressed(keycode)
	pressed := current && !previousPressed[keycode]
	previousPressed[keycode] = current

	return pressed
}

^ go version
*/

What did you expect to see?

not get a c error from the is_just_pressed function

What did you see instead?

keyboard.v:5:27: error: unknown type `input.Key`
    3 | import sdl
    4 | 
    5 | pub fn get_pressed_keys() []Key {
      |                           ~~~~~
    6 |     mut pressed_keys := []Key{}
    7 |     numkeys := 0
keyboard.v:29:27: error: unknown type `input.Key`
   27 | }
   28 | 
   29 | pub fn is_pressed(keys ...Key) bool {
      |                           ~~~
   30 |     pressed_keys := get_pressed_keys()
   31 |     for key in keys {
keyboard.v:40:28: error: unknown type `input.Key`
   38 | 
   39 | @[unsafe]
   40 | pub fn is_just_pressed(key Key) bool {
      |                            ~~~
   41 |     mut static pressed_keys := []Key{}
   42 |     if !pressed_keys.contains(key) && is_pressed(key) {
keyboard.v:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`)
    1 | module input
      | ^
    2 | 
    3 | import sdl
If the code of your project is in multiple files, try with `v .` instead of `v keyboard.v`

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21629

@jorgeluismireles
Copy link

Yours is not a C code error but a V error. If you are using the official sdl module I think you need change Key by
sdl.KeyCode.

When you use Key alone the compiler thinks Key is located in your module (which is called input) so the V error report says I can't found input.Key. In case you have a struct called Key you need to include the module where is located.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants