|
| 1 | +## Running Luau |
| 2 | + |
| 3 | +Luau can be interesting for its typing system. |
| 4 | +However, for it to be useful, it needs to be integrated both in the bevy lua runtime and also a language server much like luau-lsp. |
| 5 | + |
| 6 | +This example will provide snippets to integrate `luau-lsp` and `bevy_mod_scripting` within VSCode |
| 7 | + |
| 8 | +### Luau package import |
| 9 | + |
| 10 | +The first step for packages to load each other is to enable the `unsafe_lua_modules` in `cargo.toml`, example: |
| 11 | +```toml |
| 12 | +[dependencies] |
| 13 | +bevy_mod_scripting = { version = "0.13.0", features = ["luau","unsafe_lua_modules"] } |
| 14 | +``` |
| 15 | + |
| 16 | +The second step to make luau`require("./source/to/package")` work within bevy, the `LUA_PATH` needs to be exported as an environment variable. This also probably needs to be initialized at runtime as the location of the bevy executable may change. |
| 17 | +Here is an example snippet that updates the path at runtime. |
| 18 | + |
| 19 | +```rust |
| 20 | +fn main() { |
| 21 | + // Set the LUA_PATH env variable |
| 22 | + let mut assets_path = std::env::current_dir().expect("Failed to get current dir"); |
| 23 | + assets_path.push("assets"); |
| 24 | + |
| 25 | + let assets_str = assets_path |
| 26 | + .to_str() |
| 27 | + .expect("Failed to convert path to str") |
| 28 | + .replace("\\", "/"); |
| 29 | + |
| 30 | + let luau_package_path = format!("{}{}", assets_str, "/?.luau"); |
| 31 | + |
| 32 | + unsafe{ |
| 33 | + env::set_var("LUA_PATH", luau_package_path); |
| 34 | + } |
| 35 | + |
| 36 | + let mut app = App::new(); |
| 37 | + app.add_plugins(BMSPlugin); |
| 38 | + |
| 39 | + // More code adding luau scripts, callbacks and else. |
| 40 | + |
| 41 | + app.run(); |
| 42 | +} |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +Assuming a folder structure that is the following, |
| 47 | +- assets |
| 48 | + - scenarios |
| 49 | + - scenario.luau |
| 50 | + - scripts |
| 51 | + - Template.luau |
| 52 | + |
| 53 | +`scenario.luau` should be able to import Template.luau using the following lines: |
| 54 | + |
| 55 | +```lua |
| 56 | +local Template = require("./../scripts/Template") |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | +### Luau-lsp |
| 61 | + |
| 62 | +To have luau autocomplete and type check within VSCode install the Luau Language Server plugin. |
| 63 | + |
| 64 | +To avoid excessive underlines within the luau-lsp, a `.luaurc` file needs to be added in the root of the project. |
| 65 | + |
| 66 | +`.luaurc` |
| 67 | +```json |
| 68 | +{ |
| 69 | + "languageMode": "strict", |
| 70 | + "lint": { "*": true, "LocalUnused": false, "FunctionUnused": false }, |
| 71 | + "lintErrors": true, |
| 72 | + "globals": ["world" , "construct", "types", "package"] |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Additionally, because luau-lsp is focused towards Roblox plugins, additional VSCode config should be inserted to avoid incorrect autocomplete. |
| 77 | + |
| 78 | +`.vscode/settings.json` |
| 79 | +```json |
| 80 | +{ |
| 81 | + "luau-lsp.platform.type": "standard", |
| 82 | + "luau-lsp.types.robloxSecurityLevel": "None", |
| 83 | + "luau-lsp.sourcemap.enabled": false, |
| 84 | + "luau-lsp.sourcemap.autogenerate": false |
| 85 | +} |
| 86 | +``` |
0 commit comments