duktape
duktape copied to clipboard
Can't use for loops.
Hello there, when I try to use a simple for loop, I get "SyntaxError: parse error (line 2)" I can use while loops, but any for loops seems to cause parse errors.
Here is the really simple code that doesn't work: (Uses a custom print function bound via C)
const arr = [3, 5, 7];
for (let i of arr) {
print(i); // logs 3, 5, 7
}
Here is the really simple code that does work:
const arr = [3, 5, 7];
const counter = arr.length;
const index = 0;
while (index < counter)
{
print(arr[index])
index++;
}
I don't think that either let
or for(... of ...)
are supported in Duktape at this time. If it is a clean array, you can just use:
for (var i in arr) {
print (arr [i]);
}
const arr = [3, 5, 7]; for (let i of arr) { print(i); // logs 3, 5, 7 }
This worked!
Do you know how to return a clean array from a C function? Would it just be a std::vector<uintptr_t> or what?
You'd use duk_push_array
to create the array, then you would push whatever item you want to put in, then you can put it into the array at the proper index using duk_put_prop_index
.
You'd use
duk_push_array
to create the array, then you would push whatever item you want to put in, then you can put it into the array at the proper index usingduk_put_prop_index
.
Ah, great to know!
I was able to figure out a way to do it without doing that by creating a class, and having properties to get size and get index. Terrible way to do it kinda, but meh.
game.render_callback = function()
{
var cent = new client_entity()
var cent_size = cent.get_size();
for (var index = 0; index < cent_size; index++)
{
var current_object = cent.get(index);
if (current_object == 0)
{
print("current_object == 0");
continue;
}
var base_object = read_ptr(current_object + 0x10);
if (base_object == 0)
{
print("current_object == 0");
continue;
}
var object = read_ptr(base_object + 0x30);
if (object == 0)
{
print("current_object == 0");
continue;
}
var prefab = read_str(object + 0x60);
if (prefab.includes("player.prefab"))
{
var player_pos = get_object_position(object);
if (!player_pos.is_zero())
{
var screen = new w2s(player_pos.x, player_pos.y, player_pos.z);
if (screen.in_frame)
{
var screen_pos = new vector2(screen.x, screen.y);
if (!screen_pos.is_zero())
{
gui_text(screen.x, screen.y, 100, 100, "Player");
}
}
}
}
}
}