Provide optional values for Vector.new libraries Vector4.new() -> 0,0,0,0

This commit is contained in:
unittensor 2024-01-22 15:05:08 -05:00
parent 76ca43a645
commit e40e960ef2
4 changed files with 114 additions and 36 deletions

View File

@ -12,26 +12,30 @@ local function InspectVectorTable(Vector: {[string]: number})
end end
print("----StrafeLua----") print("----StrafeLua----")
print(_VERSION) warn(_VERSION)
print("Vector2=",InspectVectorTable(v2)) print(`Vector2=\t{InspectVectorTable(v2)}`)
print("Vector3=",InspectVectorTable(v3)) print(`Vector3=\t{InspectVectorTable(v3)}`)
print("Vector4=",InspectVectorTable(v4)) print(`Vector4=\t{InspectVectorTable(v4)}`)
print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO)) print(`Vector2.ZERO=\t{InspectVectorTable(Vector2.ZERO)}`)
print("Vector2.ONE=",InspectVectorTable(Vector2.ONE)) print(`Vector2.ONE=\t{InspectVectorTable(Vector2.ONE)}`)
print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO)) print(`Vector2.NEG_ZERO=\t{InspectVectorTable(Vector2.NEG_ZERO)}`)
print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE)) print(`Vector2.NEG_ONE=\t{InspectVectorTable(Vector2.NEG_ONE)}`)
print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X)) print(`Vector2.NEG_X=\t{InspectVectorTable(Vector2.NEG_X)}`)
print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y)) print(`Vector2.NEG_Y=\t{InspectVectorTable(Vector2.NEG_Y)}`)
print("Vector3.ZERO=",InspectVectorTable(Vector3.ZERO)) print(`Vector3.ZERO=\t{InspectVectorTable(Vector3.ZERO)}`)
print("Vector3.ONE=",InspectVectorTable(Vector3.ONE)) print(`Vector3.ONE=\t{InspectVectorTable(Vector3.ONE)}`)
print("Vector3.NEG_ZERO=",InspectVectorTable(Vector3.NEG_ZERO)) print(`Vector3.NEG_ZERO=\t{InspectVectorTable(Vector3.NEG_ZERO)}`)
print("Vector3.NEG_ONE=",InspectVectorTable(Vector3.NEG_ONE)) print(`Vector3.NEG_ONE=\t{InspectVectorTable(Vector3.NEG_ONE)}`)
print("Vector3.NEG_X=",InspectVectorTable(Vector3.NEG_X)) print(`Vector3.NEG_X=\t{InspectVectorTable(Vector3.NEG_X)}`)
print("Vector3.NEG_Y=",InspectVectorTable(Vector3.NEG_Y)) print(`Vector3.NEG_Y=\t{InspectVectorTable(Vector3.NEG_Y)}`)
print("Vector4.ZERO=",InspectVectorTable(Vector4.ZERO)) print(`Vector4.ZERO=\t{InspectVectorTable(Vector4.ZERO)}`)
print("Vector4.ONE=",InspectVectorTable(Vector4.ONE)) print(`Vector4.ONE=\t{InspectVectorTable(Vector4.ONE)}`)
print("Vector4.NEG_ZERO=",InspectVectorTable(Vector4.NEG_ZERO)) print(`Vector4.NEG_ZERO=\t{InspectVectorTable(Vector4.NEG_ZERO)}`)
print("Vector4.NEG_ONE=",InspectVectorTable(Vector4.NEG_ONE)) print(`Vector4.NEG_ONE=\t{InspectVectorTable(Vector4.NEG_ONE)}`)
print("Vector4.NEG_X=",InspectVectorTable(Vector4.NEG_X)) print(`Vector4.NEG_X=\t{InspectVectorTable(Vector4.NEG_X)}`)
print("Vector4.NEG_Y=",InspectVectorTable(Vector4.NEG_Y)) print(`Vector4.NEG_Y=\t{InspectVectorTable(Vector4.NEG_Y)}`)
print("-----------------")
print(`No args Vector2.new=\t{InspectVectorTable(Vector2.new())}`)
print(`No args Vector3.new=\t{InspectVectorTable(Vector3.new())}`)
print(`No args Vector4.new=\t{InspectVectorTable(Vector4.new())}`)
print("-----------------") print("-----------------")

View File

@ -8,7 +8,7 @@ type struct_Vector4 = struct_Vector3 & {w: f64}
export type warn = (message: string) -> () export type warn = (message: string) -> ()
export type Vector2 = { export type Vector2 = {
new: (x: f64, y: f64) -> struct_Vector2, new: (x: f64?, y: f64?) -> struct_Vector2,
ONE: struct_Vector2, ONE: struct_Vector2,
ZERO: struct_Vector2, ZERO: struct_Vector2,
NEG_ZERO: struct_Vector2, NEG_ZERO: struct_Vector2,
@ -17,7 +17,7 @@ export type Vector2 = {
NEG_Y: struct_Vector2, NEG_Y: struct_Vector2,
} }
export type Vector3 = { export type Vector3 = {
new: (x: f64, y: f64, z: f64) -> struct_Vector3, new: (x: f64?, y: f64?, z: f64?) -> struct_Vector3,
ONE: struct_Vector3, ONE: struct_Vector3,
ZERO: struct_Vector3, ZERO: struct_Vector3,
NEG_ZERO: struct_Vector3, NEG_ZERO: struct_Vector3,
@ -26,7 +26,7 @@ export type Vector3 = {
NEG_Y: struct_Vector3, NEG_Y: struct_Vector3,
} }
export type Vector4 = { export type Vector4 = {
new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4, new: (x: f64?, y: f64?, z: f64?, w: f64?) -> struct_Vector4,
ONE: struct_Vector4, ONE: struct_Vector4,
ZERO: struct_Vector4, ZERO: struct_Vector4,
NEG_ZERO: struct_Vector4, NEG_ZERO: struct_Vector4,

View File

@ -34,7 +34,7 @@ impl Luavm for StrafeluaGlobals {
fn warn(&self) -> Function { fn warn(&self) -> Function {
return self.vm.create_function(|_, message: mlua::String| { return self.vm.create_function(|_, message: mlua::String| {
match Some(message) { match Some(message) {
Some(lua_string) => cprintln!("<yellow>{}</yellow>", lua_string.to_string_lossy()), Some(lua_string) => cprintln!("<yellow>{}</yellow>", lua_string.to_str().unwrap()),
None => println!("Nothing provided to warn"), None => println!("Nothing provided to warn"),
}; };
Ok(()) Ok(())
@ -42,7 +42,6 @@ impl Luavm for StrafeluaGlobals {
} }
fn vector2(&self) -> Table { fn vector2(&self) -> Table {
//In a better world, this can all be a macro
let zero = Vec2::ZERO; let zero = Vec2::ZERO;
let one = Vec2::ONE; let one = Vec2::ONE;
let neg_zero = Vec2::ZERO; let neg_zero = Vec2::ZERO;
@ -83,8 +82,16 @@ impl Luavm for StrafeluaGlobals {
field_vector2.set("NEG_X", vector2_neg_x).unwrap(); field_vector2.set("NEG_X", vector2_neg_x).unwrap();
field_vector2.set("NEG_Y", vector2_neg_y).unwrap(); field_vector2.set("NEG_Y", vector2_neg_y).unwrap();
field_vector2.set("new", self.vm.create_function(|this: &Luau, (x,y): (f32,f32)| { field_vector2.set("new", self.vm.create_function(|this: &Luau, (x,y): (Option<mlua::Number>, Option<mlua::Number>)| {
let glam_vec = Vec2::new(x,y); let vec2_x = match x {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec2_y = match y {
Some(n) => n as f32,
None => 0.0 as f32,
};
let glam_vec = Vec2::new(vec2_x,vec2_y);
let vec2 = this.create_table().unwrap(); let vec2 = this.create_table().unwrap();
vec2.set("x", glam_vec.x).unwrap(); vec2.set("x", glam_vec.x).unwrap();
vec2.set("y", glam_vec.y).unwrap(); vec2.set("y", glam_vec.y).unwrap();
@ -142,15 +149,26 @@ impl Luavm for StrafeluaGlobals {
field_vector3.set("NEG_X", vector3_neg_x).unwrap(); field_vector3.set("NEG_X", vector3_neg_x).unwrap();
field_vector3.set("NEG_Y", vector3_neg_y).unwrap(); field_vector3.set("NEG_Y", vector3_neg_y).unwrap();
let new_vector3 = self.vm.create_function(|this: &Luau, (x,y,z): (f32,f32,f32)| { field_vector3.set("new", self.vm.create_function(|this: &Luau, (x,y,z): (Option<mlua::Number>, Option<mlua::Number>, Option<mlua::Number>)| {
let glam_vec = Vec3::new(x,y,z); let vec3_x = match x {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec3_y = match y {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec3_z = match z {
Some(n) => n as f32,
None => 0.0 as f32,
};
let glam_vec = Vec3::new(vec3_x,vec3_y,vec3_z);
let vec3 = this.create_table().unwrap(); let vec3 = this.create_table().unwrap();
vec3.set("x", glam_vec.x).unwrap(); vec3.set("x", glam_vec.x).unwrap();
vec3.set("y", glam_vec.y).unwrap(); vec3.set("y", glam_vec.y).unwrap();
vec3.set("z", glam_vec.z).unwrap(); vec3.set("z", glam_vec.z).unwrap();
Ok(vec3) Ok(vec3)
}).unwrap(); }).unwrap()).unwrap();
field_vector3.set("new", new_vector3).unwrap();
return field_vector3; return field_vector3;
} }
@ -208,8 +226,24 @@ impl Luavm for StrafeluaGlobals {
field_vector4.set("NEG_X", vector4_neg_x).unwrap(); field_vector4.set("NEG_X", vector4_neg_x).unwrap();
field_vector4.set("NEG_Y", vector4_neg_y).unwrap(); field_vector4.set("NEG_Y", vector4_neg_y).unwrap();
let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (f32,f32,f32,f32)| { let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (Option<mlua::Number>, Option<mlua::Number>, Option<mlua::Number>, Option<mlua::Number>)| {
let glam_vec = Vec4::new(x,y,z,w); let vec4_x = match x {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec4_y = match y {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec4_z = match z {
Some(n) => n as f32,
None => 0.0 as f32,
};
let vec4_w = match w {
Some(n) => n as f32,
None => 0.0 as f32,
};
let glam_vec = Vec4::new(vec4_x,vec4_y,vec4_z,vec4_w);
let vec4 = this.create_table().unwrap(); let vec4 = this.create_table().unwrap();
vec4.set("x", glam_vec.x).unwrap(); vec4.set("x", glam_vec.x).unwrap();
vec4.set("y", glam_vec.y).unwrap(); vec4.set("y", glam_vec.y).unwrap();

View File

@ -120,7 +120,47 @@ pub fn default_models()->model::IndexedModelInstances{
fn main(){ fn main(){
let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua"); let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua");
luau::error_wrapper(strafelua_vm.load(r#" luau::error_wrapper(strafelua_vm.load(r#"
warn("hi") local v2 = Vector2.new(1, 3)
local v3 = Vector3.new(-10, 30, 50)
local v4 = Vector4.new(50, 30, 10, -1000)
local function InspectVectorTable(Vector: {[string]: number})
local aye: {string} = {"{"}
for k,v in Vector do
table.insert(aye, `{tostring(k)}={tostring(v)}`)
end
table.insert(aye, "}")
return table.concat(aye, " ")
end
print("----StrafeLua----")
warn(_VERSION)
print(`Vector2=\t{InspectVectorTable(v2)}`)
print(`Vector3=\t{InspectVectorTable(v3)}`)
print(`Vector4=\t{InspectVectorTable(v4)}`)
print(`Vector2.ZERO=\t{InspectVectorTable(Vector2.ZERO)}`)
print(`Vector2.ONE=\t{InspectVectorTable(Vector2.ONE)}`)
print(`Vector2.NEG_ZERO=\t{InspectVectorTable(Vector2.NEG_ZERO)}`)
print(`Vector2.NEG_ONE=\t{InspectVectorTable(Vector2.NEG_ONE)}`)
print(`Vector2.NEG_X=\t{InspectVectorTable(Vector2.NEG_X)}`)
print(`Vector2.NEG_Y=\t{InspectVectorTable(Vector2.NEG_Y)}`)
print(`Vector3.ZERO=\t{InspectVectorTable(Vector3.ZERO)}`)
print(`Vector3.ONE=\t{InspectVectorTable(Vector3.ONE)}`)
print(`Vector3.NEG_ZERO=\t{InspectVectorTable(Vector3.NEG_ZERO)}`)
print(`Vector3.NEG_ONE=\t{InspectVectorTable(Vector3.NEG_ONE)}`)
print(`Vector3.NEG_X=\t{InspectVectorTable(Vector3.NEG_X)}`)
print(`Vector3.NEG_Y=\t{InspectVectorTable(Vector3.NEG_Y)}`)
print(`Vector4.ZERO=\t{InspectVectorTable(Vector4.ZERO)}`)
print(`Vector4.ONE=\t{InspectVectorTable(Vector4.ONE)}`)
print(`Vector4.NEG_ZERO=\t{InspectVectorTable(Vector4.NEG_ZERO)}`)
print(`Vector4.NEG_ONE=\t{InspectVectorTable(Vector4.NEG_ONE)}`)
print(`Vector4.NEG_X=\t{InspectVectorTable(Vector4.NEG_X)}`)
print(`Vector4.NEG_Y=\t{InspectVectorTable(Vector4.NEG_Y)}`)
print("-----------------")
print(`No args Vector2.new=\t{InspectVectorTable(Vector2.new())}`)
print(`No args Vector3.new=\t{InspectVectorTable(Vector3.new())}`)
print(`No args Vector4.new=\t{InspectVectorTable(Vector4.new())}`)
print("-----------------")
"#).exec()); "#).exec());
//Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false } //Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false }
//we got our first lua syntax error, todo: make an error handler in luau.rs //we got our first lua syntax error, todo: make an error handler in luau.rs