• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Help with selecting random target from specific object

G

Gerald Tyler

Guest
Hey guys, quick question, hopefully an easy answer.

So what I'm wanting to do is set up multiple stationary Conduits in a room, and have the first one fire a Bolt at a random one, then the one which has it will fire at another random Conduit, and so on and so forth.

So I know how to create the bolt, set alarms, the basic stuff. But how do I basically do

if (ready = true)
{
create and fire instance of obj_Bolt towards Random obj_Conduit that has ready = false<--Part I need help with
ready = false;
}

I know there's a long way I can do it for pre-fabricated rooms, but I'm hoping to get some code that would work even with procedurally generated levels, where I wouldn't know ahead of time how many conduits there are.
 
Last edited by a moderator:
G

Gerald Tyler

Guest
random_conduit_inst = instance_find( obj_Conduit, irandom(instance_number(obj_Conduit) -1) );
Thank you Maximus. I was about to edit my original post actually, would this prevent it from selecting itself again? Basically I would need it to target one of the ones where (ready = false). Sorry for being a pain.
 
M

Maximus

Guest
Code:
random_conduit_inst = instance_find( obj_Conduit, irandom(instance_number(obj_Conduit) -1) );
while (random_conduit_inst == id) || random_conduit_inst.ready != false || (collision_line(x,y,random_conduit_inst.x, random_conduit_inst.x, obj_wall, false, true) != noone)
{
    random_conduit_inst = instance_find( obj_Conduit, irandom(instance_number(obj_Conduit) -1) );
}
edit: added the ready condition, sorry
 
G

Gerald Tyler

Guest
Awesome, thanks a ton! Going to need a few to wrap my head around that but it should be what I need =)
 
M

Maximus

Guest
Another way to do the while loop which might make more sense to look at:
Code:
while  not (  (random_conduit_inst.id != id) and (random_conduit_inst.ready == false) and (collision_line(x,y,random_conduit_inst.x, random_conduit_inst.x, obj_wall, false, true) == noone)  )
{
   random_conduit_inst = instance_find( obj_Conduit, irandom(instance_number(obj_Conduit) -1) );
}
This checks if all the conditions the ideal candidate needs are met and if they are not, it gets another random instance and tries again.
 
Top