duktape icon indicating copy to clipboard operation
duktape copied to clipboard

Can't use for loops.

Open bditt opened this issue 1 year ago • 4 comments

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++;
}

bditt avatar Oct 14 '22 08:10 bditt

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]);
}

shdon avatar Oct 19 '22 22:10 shdon

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?

bditt avatar Oct 20 '22 00:10 bditt

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.

shdon avatar Oct 20 '22 01:10 shdon

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.

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");
					}
				}
			}
		}
	}
}

bditt avatar Oct 20 '22 01:10 bditt