Often you have a game where the screen is broken apart into cells; and everything snaps to that.
In these cases, its often good to store the information about the occupant of a cell directly into the cell.
Here's a quick way to control screen cell arrays.
cell[] - array, size is determined by your screen settings. 640x480 divided into 32x32 cells=300 total cells, with 0 being the upper left cell and 299 being the lower right cell.
ccell - global integer
so you can find the mouse clicks and refer them to a cell; in this type of setup, you would not use mousebuttondowns on any actor except one, filled region, that catches all mouse clicks.(simply put it on the bottom in editor mode, and put "ChangeZDepth("Event Actor", 1); in its creatactor to bring it to the top for gameplay.
filed region>mousebuttondown>
int i=round(xmouse/32);
int j=round(ymouse/32);
ccell=i+(j*20);
now we've found the cell clicked on , and can do anything from there.
Lets say you dropped a unit on a cell, you can set the cell values from that unit, or simply link the cell to the unit;
Each unit has an id(you can use cloneindex for instance).
so you can say cell[i]=unit.cloneindex; (though cloneindex isn't great because it can't reset. Next post, i'll show how to find unused array cells).
then refer to the cell
health[cid]-=damage[ccell]; etc. etc. and work off the cell values.//cid stands for current id....(selected actor).
or you can refer it, cell[ccell]=unit[id];
now at any time, lets say you dropped spike traps on a cell, we can ask for the unit id contained in that cell, then take damage[cell[id]]; where id would be the name of the spikes.
this would return the value of the damage array corresponding to the unit id that was found in the cell.
you can also use the ccell calculation for an object to know its cell value.
actor>event
int i=round(x/32);
int j=round(x/32);
mycell=i+(j*20);
and use that to calculate nearby cells, if its facing an enemy, it can ask for the enemy's cell number, and calculate distances, angles, etc. off that.
you can also precalculate the xy values of cells, at game startup, to avoid having to recalculate them.
int i;
int j;
int k;
for(i=0; i<300; i++){
cellx[i]=(j*32)+16; //assign x value for cell
//the +16 offsets to the center of the cell(very useful).
j++; //increment j, this is the x reference.
if(j>19){ //if j has reached the edge of the screen
j=0; //j returns to 0
k++; //increment k, this is the y reference.
}
celly[i]=(k*32)+16; //assign y value for cell
} //end loop
Now at any time, i can refer to cellx[ccell] or celly[ccell] for any calculations i need.
I stored the variable 'cid' on click, in the referncing method, cid would be set to the cell[ccell]] value, and not changed till we click on another cell, such as to highlight an actor, then pick another actor to attack.