GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2019-08-07 05:41:11

Kosta
Member
From: Moscow
Registered: 2019-08-07
Posts: 2
Website

Copy mp_grid

There is a function for copying a regular grid, but it is not suitable for mp_grid.

Expand///@function mp_grid_copy(id,source);
///@description Copies one mp_grid to another mp_grid
///@param {real}	id object_index
///@param {real}	source object_index
{
var my_mp_grid_id=argument[0];
var other_mp_grid_id=argument[1];
mp_grid_clear_all(my_mp_grid_id);
for (var ix = 0; ix <= room_width; ix++;)
    {
	for (var iy = 0; iy <= room_height; iy++;)
		{
		if mp_grid_get_cell(other_mp_grid_id, ix , iy)=-1 mp_grid_add_cell(my_mp_grid_id, ix , iy);
		}
	}
}

Last edited by Kosta (2019-08-07 14:26:02)

Offline

#2 2019-08-07 19:30:30

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Copy mp_grid

Oh, interesting. This is a good idea for a script. It is a shame GML doesn't provide the tools to make it more elegant.

That said, I think this can be optimized quite a bit by measuring the destination mp_grid before copying to it. After it is cleared, it can be scanned along each axis, checking for -1, to determine its dimensions. With that known, we don't waste a lot of unnecessary effort scanning every pixel of the entire room (which might not even by the right room or size).

Beyond that (and some style changes), I would remove any reference to `object_index` from the comments because it doesn't apply in any way here.

Thanks for the script and welcome to the forums.


Abusing forum power since 1986.

Offline

#3 2019-08-08 08:10:08

Kosta
Member
From: Moscow
Registered: 2019-08-07
Posts: 2
Website

Re: Copy mp_grid

Thanks for the welcome!
The size of the grid "from the room size" did not come out intentionally for me - this was relevant only for my situation (now I understand that it was worth passing the dimensions of the grid to the function from the outside, and not expect everyone to have the same room size).
Using it, I came to the conclusion that it is not optimized enough - its execution for large mp_grids (the size of a room) takes too long.
Therefore, today I would recommend using it only for small grids.
I took a different path: I entered the coordinates information into the ds_list, and then created a filled cell in these coordinates (in mp_grid). All this turned out to be not universal, so I do not send another function.
Thanks for your site!

Offline

Board footer

Powered by FluxBB