-
Notifications
You must be signed in to change notification settings - Fork 17
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
Weird Rhai error #2
Comments
This is somewhat sneaky: there is already a function named circle, and it's getting called instead of your function. In addition, it's already set up to return a lamda function, so you'd invoke it like draw(circle(0, 0, 1)) // center x, center y, r -> (Fn(x, y) -> f32) It works if you rename your function to not collide with the existing function: fn length(x, y) {
sqrt(x*x + y*y)
}
fn my_circle(x, y, r) {
length(x, y) - r
}
draw(|x, y| my_circle(x, y, 1)); However, this violates the search order in the Rhai docs (and is confusing!), so let's leave this issue open. |
Shall I report this upstream to the |
When I try this with
|
Ah, I see, it doesn't take a closure any more. Sorry about the noise, kindly ignore! |
Docs say there is a |
Yes, #67 switched to using the new The new signature is draw_rgb(circle(0, 1, 2), 1.0, 0.0, 0.0) |
Cheers, it was a typo. Is there a list of built-ins somewhere? It seems e.g. Rhai is missing e.g. |
I can not get a simple box in the viewer. Whatever function I try, except for the circle, I only get the positive part of the axes, i.e. fn abs(x) {
if x < 0 {
-x
} else {
x
}
}
fn box(x, y, radius_x, radius_y)
{
let p_x = abs(x) - radius_x;
let p_y = abs(y) - radius_y;
max(p_x, p_y)
}
draw(box(x, y, 1, 1)) I also tried |
Here's the issue: fn abs(x) {
if x < 0 {
-x
} else {
x
}
} During script evaluation, #70 (merging soon) adds overloaded operators to ban these comparisons, so that it will fail at script evaluation instead of returning a misleading result. This PR also adds missing opcodes, so you can write your script as fn box(radius_x, radius_y)
{
let ax = axes();
let p_x = abs(ax.x) - radius_x;
let p_y = abs(ax.y) - radius_y;
max(p_x, p_y)
}
draw(box(1, 1)) (I'm still figuring out best practices, but using |
Speaking of which: writing the functions in Rhai, doing each operation for each coordinate, is very verbose because of the lack of tuple types. Most functions from e.g. here almost double in code length. What is the performance hit if one used arrays of two (or three) elements to represent vectors? Of course then one would need to write the entire shebang of basics like I.e. best would probably be some language extension that supports vectors (and possible matrices) inside Rhai? Not sure this possible. EDIT: I just saw this, so it seems this is possible and likely |
Cheers! 😃 So I have something like: fn rounded_box(
height_x, height_y,
radius_top_right,
radius_bottom_right,
radius_top_left,
radius_bottom_left)
{
let ax = axes();
let x = ax.x;
let y = ax.y;
let r_x = if 0.0 < x { radius_top_right } else { radius_top_left };
let r_y = if 0.0 < x { radius_bottom_right } else { radius_bottom_left };
let r_x = if 0.0 < y { r_x } else { r_y };
let q_x = abs(x)- height_x + r_x;
let q_y = abs(y)- height_y + r_x;
min(max(q_x, q_y), 0) + length(max(q_x, 0.0), max(q_y, 0)) - r_x
} And that gives me an error for the comparisons in the
I tried rewriting the resp. expressions with let r_x = if compare(0, x) == -1 { radius_top_right } else { radius_top_left };
let r_y = if compare(0, x) == -1 { radius_bottom_right } else { radius_bottom_left };
let r_x = if compare(0, y) == -1 { r_x } else { r_y }; results in the same error. What is the workaround here? EDIT: thinking back of my days as a VFX shader writer (where conditionals were a no-no): |
And another one. A simple rounding op, i.e. fn round(shape, radius) {
shape - radius
} Only grows my shape by What am I missing/what is the workaround? |
So I just started playing around with the
viewer
.This Rhai script
works as expected.
However
Yields:
The text was updated successfully, but these errors were encountered: