Closed zwazel closed 11 months ago
I looked into this, and it seems the error comes from this line:
https://github.com/makspll/bevy_mod_scripting/blob/eb691a26d316aba6fbd5f58322be7850db4833b0/bevy_script_api/src/common/std.rs#L136
as when the length is 0, it tries to subtract one. it being a usize, results in a attempt to subtract with overflow
.
So just to test, i changed it to
fn into_iter(self) -> Self::IntoIter {
ScriptVecIterator {
current: 0,
end: self.len().map(|v| v - 1).unwrap_or(0),
base: self,
}
}
Which kind of solves it, as it doesnt crash anymore.
~But interestingly the variable, in my case lobbies
, can't be found anymore:~
(not true, thats just me writing my variables wrong)
Now that i wrote the variables correctly, it still crashes, obviously because i'm still subtracting from usize... my bad, brb.
Alright, changing it to
fn into_iter(self) -> Self::IntoIter {
ScriptVecIterator {
current: 0,
// TODO?: end used to be an Option, and this check moved into the next method but
// I am not sure if this will ever realistically fail, so if you do get this exception happening
// hit me with an issue
// if len > 0, subtract 1, otherwise set to 0
end: self
.len()
.and_then(|len| Ok(if len > 0 { len - 1 } else { 0 }))
.expect("Failed to get length of ScriptVec"),
base: self,
}
}
Makes it not crash. But I now get following error printed out:
Runtime error in script `lobby_list_container.rhai` Runtime error: Invalid reflection path: `[0]`. No such element
in call to function 'lobby_list_updated'
Is the no such element error happening on empty vecs? That might be because of the issue I mentioned with the iterator where it's still trying to index into it on empty vecs
I got following struct:
I register all the needed things to correctly access the vec and loop through it via scripts:
With this I can then loop through the lobbies in Rhai like this:
And this works... as long as the vec is not empty! When it's empty i get following error and it crashes:
As I'm only using Rhai, I don't know if this error exists in Lua too.
The current workaround is to just check if the vec is empty before trying to loop through it: