Jump to content


Photo

"While" loop to ensure object spawns correctly


  • Please log in to reply
8 replies to this topic

#1 jhawks

jhawks

    GMC Member

  • GMC Member
  • 257 posts
  • Version:GM:HTML5

Posted 05 April 2012 - 08:33 AM

Hi all,

I have a spawn system which checks for a Object A to never spawn above or below Object B (it's a top down game).

When spawning an object, I first find an empty space and then spawn the object in that space. During this, I have a while loop for Object A that checks where the chosen space is. If that space is above or below Object B, then it should re-choose the space. This is the while loop (which is inside a script called "spawnObject()"):

if (obj == objectA)
{
    while (space.x == objectB.x and space.y == objectB.y - 32 or space.x == objectB.x and space.y == objectB.y + 32)  
    {
        space = findSpace(); //Use script to find another space
    }
}

However, sometimes, ObjectA still spawns above or below ObjectB.

Hope that is enough information. Is there anything wrong with the above code?
  • 0

#2 darkclower

darkclower

    GMC Member

  • New Member
  • 80 posts
  • Version:GM8

Posted 05 April 2012 - 09:01 AM

We wiil need the findspace() script to help you because the problem probably lies in there. :)

Edited by darkclower, 05 April 2012 - 09:02 AM.

  • 0

#3 jhawks

jhawks

    GMC Member

  • GMC Member
  • 257 posts
  • Version:GM:HTML5

Posted 05 April 2012 - 10:32 AM

Okie dokie

space = argument0;

n = instance_number(space);                 //Checks how many instances of the space object in question
instance_find(space, floor(random(n)));   //Find one at random

  • 0

#4 Blake

Blake

    GMC Member

  • GMC Member
  • 443 posts
  • Version:GM:Studio

Posted 05 April 2012 - 10:56 AM

Lack of brackets can sometimes cause problems, so try this and see if it makes a difference:

while ((space.x == objectB.x and space.y == objectB.y - 32) or (space.x == objectB.x and space.y == objectB.y + 32))
Also, I'm finding it hard to follow your code because you are not using prefixes, eg. "obj_" for objects. This is a good habit to get into because it prevents resource name clashes, as well as making it easier for us to follow your code :thumbsup:

Edited by Blake, 05 April 2012 - 10:57 AM.

  • 0

#5 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 5208 posts
  • Version:GM8

Posted 05 April 2012 - 12:22 PM

try

if space.y >= objectB.y-32 && space.y <= objectB.y+32
{

}
  • 0

#6 jhawks

jhawks

    GMC Member

  • GMC Member
  • 257 posts
  • Version:GM:HTML5

Posted 06 April 2012 - 12:41 AM

I currently tried the brackets idea to see if that works. I'll do multiple tries and see if the two objects do not spawn together. Since it's randomly generated, this can take a while.

Oh and yeah sorry. I do use prefixes like "obj_". But I didn't do it in this example because it was an example. But yeah, I should next time so it's easier for you guys to follow my code ><
  • 0

#7 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 06 April 2012 - 05:59 AM

space = findSpace(); //Use script to find another space

space = argument0;

n = instance_number(space);                 //Checks how many instances of the space object in question
instance_find(space, floor(random(n)));   //Find one at random

Hi,

How come your script is using/requires an argument, yet you don't call it with one?

In GM8.1, this'll actually throw you an error (although you have the option to switch that off). It's not really good practice.

In any case, I have a hard time visualizing what you're trying to do. Perhaps a screenshot would help.
  • 0

#8 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 06 April 2012 - 04:09 PM

I was hoping, at first, that your spaces were laid out in a regular grid. Then you could simply use some mathematical magic to avoid the loop altogether. Since you're finding space instances, I'm assuming it's not quite so regular a shape?

Still, you can avoid the while() loop and get the desired result by playing with deactivation. You'd deactivate any space instance above/below your objectB (I'm also assuming there's only one objectB in the room, since you're using that notation?), then find a space and re-activate the others again.

findSpace(SPACE_OBJECT):
var p, numDeactivated, deactive, n, space, retn;
space=argument0;
retn=noone;

numDeactivated = 0;
with (space_obj) {
  if (x == objectB.x) {
    if (abs(y - objectB.y) == 32) {  // This is the same as saying "if (y==objectB.y + 32 || y==objectB.y - 32)"
      deactive[numDeactivated] = id;
      instance_deactivate_object(id);
    }
  }
}

n = instance_number(space);                 
retn=instance_find(space, floor(random(n)));

for (p=0; p<numDeactivated; p+=1) {
  instance_activate_object(deactive[p]);
}

return retn;

-IMP
  • 0

#9 jhawks

jhawks

    GMC Member

  • GMC Member
  • 257 posts
  • Version:GM:HTML5

Posted 07 April 2012 - 03:05 AM

@Desert Dog

Oh sorry. It's suppose to have "space" inside the brackets. The code I posted is inside another script which has "space = argument1;" in it.

@IceMetalPunk

Yeah, the grid is irregular.

I never tried using deactivation. Since I'm currently busy with another task, I'll do that first before trying to apply your solution. Thanks a lot for it though. Hopefully it'll be better than my solution when I try implementing it ^_^
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users