• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows Make an object create an object and follow it

S

squidsRockets

Guest
I am trying to implement a vision mechanic in my game (not using distance_to_object) by using an object with the sprite of a vision cone.
This is the current code for creating the cones at the start of the game.
Code:
//Check if we have vision
HaveVision = instance_place(x,y,oVision)

if instance_exists(HaveVision)
{
    CanSee = true;
}

if CanSee == false
{
   instance_create(x,y,oVision);
}
My intent was for the game check if a vision cone had been created and if not, create one. This works fine, each enemy creates a cone but it drops it right away and the cone doesn't follow them.
So I added this code
Code:
with(oVision)
{
    oVision.x = x;
    oVision.y = y;
}
I also tried this in the oVision code itself but either way, all of them follow one enemy object.
How do i get this so each enemy gets a vision cone to stick to them.

I would also appreciate any advice for a different vision/detection system.
 
G

gabomastr

Guest
if instance_exists(instance_place(x,y,oVision)){} <---- i think that is wrong

try with this

if instance_exists(oVision){
CanSee = true;
}else{
instance_create(x,y,oVision);
}
 

Tthecreator

Your Creator!
That's because oVision refers to all instances that are of type oVision. The with statement thus executes that code for every oVision code. Not for one single object.

Since you have multiple enemy objects, the with statement is called numerous times, meaning that all cones will be placed at the position of the enemy that last executed it's code.

To fix this you will need to get the reference to a single instance. You can do this when you create it using something like this:

ReferenceToSingleInstance = instance_create(obj,x,y)

Then you can use the following code in the same enemy object:

With(ReferenceToSingleInstance){//your code}

And then it should work!

Do you understand this?
 
S

squidsRockets

Guest
That's because oVision refers to all instances that are of type oVision. The with statement thus executes that code for every oVision code. Not for one single object.

Since you have multiple enemy objects, the with statement is called numerous times, meaning that all cones will be placed at the position of the enemy that last executed it's code.

To fix this you will need to get the reference to a single instance. You can do this when you create it using something like this:

ReferenceToSingleInstance = instance_create(obj,x,y)

Then you can use the following code in the same enemy object:

With(ReferenceToSingleInstance){//your code}

And then it should work!

Do you understand this?
Would this mean i would have to type that out for every enemy on screen?
 

Tthecreator

Your Creator!
No it doesn't.
If I'm not mistaken you have an object, and that object is an enemy. If you just put that code on the enemy object, it will be executed for all instances. (Note that an object refers to the thing you program with and an instance refers to a single instance of your object placed within your game)
Also the following code won't work:

With(oVision){
oVision.x = x
oVision.y = y
}

By writing a with statement you are saying "any code inside of here now is going to be executed as if it were this object between the (). All variable references will used from the perspective of this object"
This means that if I get the x value inside of a while(oVision){} I'm getting the x value for the oVision objects.

Another method you use, even inside the with statement is the object.variablename call. This is some very useful thing to have, but you aren't supposed to put them in a with statement that way. If I say oVision.x = x outside of a with statement then that will set the x of oVision to the x of the current object.

Now get a reference to a single instance of the object at a time and use this code:
ReferenceToSingleInstance.x = x

Put it inside your enemy and you are good to go.
 
Top