Saturday, October 22, 2005

Single Unit Selection.

Well, it seems that the my.enable_detect in that code in my last post was indeed pointless. I commented it out, and it seemed to continue the same. I'll leave it there for now though just in case.

My selection code problem turned out easier than I thought. I had a small mistake in the code.


you = ent_next(null);
while(you != null)
{
you.selected = off;
you = ent_next(null);
wait(1);
}


The problem is this: You is assigned to the first entity. Then while you exists, its .selected flag is set to off. Then you is set to the first entity. The while loop never terminates because you is never null. Pointers are the bane of programming indeed!

So at the forum, someone notice this problem and fixed it for me. That second ent_next command is suppose to be you = ent_next(you); So now single unit selection, ctrl+click multiple unit selection and single unit deselection is finished. Same type unit selection via double click, lassoing units and global deselect is still in the works.

The code is as follows:

function selectMe()
{
if((event_type == event_click) && (my.selected == off) && (key_ctrl != 1))
{
you = ent_next(null);
while(you != null)
{
you.selected = off;
you = ent_next(you);
wait(1);
}
wait(1);
my.selected = on;
return;
}
if((event_type == event_click) && (my.selected == off) && (key_ctrl))
{
my.selected = on;
return;
}
if((event_type == event_click) && (my.selected == on))
{
my.selected = off;
return;
}
}

action selectSprite
{
my.passable = on;
my.overlay = on;
my.tilt = 90;
my.scale_x /= 3;
my.scale_y /= 3;
while((you != null) && (you.selected == on))
{
my.x = you.x;
my.y = you.y;
my.z = you.z;
wait(1);
}
you.selecter = 0;
ent_remove(me);
}

action guard
{
tester = my;
my.enable_click = on;
my.event = selectMe;
while(1)
{
if((my.selected == on) && (unselect == off))
{
if(my.selecter != 1)
{
ent_create(selectBMP,my.x,selectSprite);
my.selecter = 1;
}

Logically, we don't start at the top. In action guard the second and third lines are the beginning of this code. The guard is sensitive to being clicked and his event when clicked is the selectMe() function. Then, when he is clicked, selectMe() is called.

The first line in selectMe() says that if the entity was clicked on, if its .selected flag is off and if the ctrl key is not being pressed then it goes to the next set of commands. The next line searches for the first entity. It enters that while loop, sets .selected to off and then gets the next entity until it runs out of entities. Then the .selected of the entity that entity is turned on. Then the return leaves the function so it can't evaluate the other ifs and hypothetically deselect the unit.

The next if-statement says that if the entity was clicked on, if its .selected flag is off and the ctrl key is on, the entity's .selected is set to on and the return closes the function. This was the easiest of the selection methods.

The last if-statement says that if the entity was clicked on and its .selected flag is on, turn the flag off and leave the function.

Back in the action, we go into the loop. Then there is the if-statement that checks to see if the .selected flag is on. If so, it creates the select sprite, if one hasn't already been created. The sprite follows the entity as it moves and removes itself if the entity's .selected flag is off or if the entity is removed (killed.)

I'm going to leave the wonderful world of selection and movement for a while and get a few basic buildings working. Start working on entity creation around a building. The theory is simple: I have a building, I select it and a panel opens up. I press a button on that panel that creates a unit. That unit is created in a vector by the building. If there is a unit in that vector, create a unit in the next vector and so on and so forth. If all vectors are taken up, wait until there is space. This means arrays of vectors and trace commands.

This is the beginning of my RTS prototype.

while(thisCodeDoesntWork)
{
smashComputer(); //take out some pent out anger
throwDartsAtBoss(); //take out more anger
sleep(6*60*60); //sleep six hours
}

No comments: