• 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 I'm working on my program and i got an issue i can't describe it. So i'm looking for some help...

A

AGameMaker

Guest
I'm trying to get a fully functioning turret to tell when my obj_Xwing is in range to rotate towards it and begin firing upon it. I got my code here and I've been piece it together from other sources i have found.

bulletSpeed = 4
//Find the nearest enemy:
nearest_enemy = instance_nearest(x,y,obj_Xwing);

//Make sure we actually have something to aim at!
if (nearest_enemy > 0) { //This is a GM pointer trick - pointers to objects are set to -1 if they're not pointing at anything
//Check to see how close that enemy is:
if (point_distance(x, y, nearest_enemy.x, nearest_enemy.y) < 200) {
//Spawn a bullet:
obj_TurretBullets = instance_create_depth(x,y, 5, obj_TurretBullets);

//Aim it at the specific enemy from earlier:
with (obj_TurretBullets) {
move_towards_point(other.nearest_enemy.x, other.nearest_enemy.y, bulletSpeed);
image_angle = direction;
}
}
}

//Aim it at the specific enemy from earlier:
obj_Xwing = nearest_enemy;
with (obj_TurretBullets) {
move_towards_point(x, y, obj_TurretBullets);
image_angle = direction;
}
it says it Cannot set a constant ("obj_TurretsBullets") to a value
also Cannot set a constant ("obj_Xwing") to a value
 

rIKmAN

Member
I'm trying to get a fully functioning turret to tell when my obj_Xwing is in range to rotate towards it and begin firing upon it. I got my code here and I've been piece it together from other sources i have found.

bulletSpeed = 4
//Find the nearest enemy:
nearest_enemy = instance_nearest(x,y,obj_Xwing);

//Make sure we actually have something to aim at!
if (nearest_enemy > 0) { //This is a GM pointer trick - pointers to objects are set to -1 if they're not pointing at anything
//Check to see how close that enemy is:
if (point_distance(x, y, nearest_enemy.x, nearest_enemy.y) < 200) {
//Spawn a bullet:
obj_TurretBullets = instance_create_depth(x,y, 5, obj_TurretBullets);

//Aim it at the specific enemy from earlier:
with (obj_TurretBullets) {
move_towards_point(other.nearest_enemy.x, other.nearest_enemy.y, bulletSpeed);
image_angle = direction;
}
}
}

//Aim it at the specific enemy from earlier:
obj_Xwing = nearest_enemy;
with (obj_TurretBullets) {
move_towards_point(x, y, obj_TurretBullets);
image_angle = direction;
}
it says it Cannot set a constant ("obj_TurretsBullets") to a value
also Cannot set a constant ("obj_Xwing") to a value
Don't name the variable the same as the actual object you are creating an instance of.

For example: obj_TurretBullets is the name of the object you are trying to create an instance of and the name you are giving to the variable to hold the return value from the instance_create() function. Change the variable name to something else so they don't clash.
 
A

AGameMaker

Guest
Don't name the variable the same as the actual object you are creating an instance of.

For example: obj_TurretBullets is the name of the object you are trying to create an instance of and the name you are giving to the variable to hold the return value from the instance_create() function. Change the variable name to something else so they don't clash.
i changed all that then this came up

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_TurretEmpire:

Variable obj_TurretEmpire.Xwing(100009, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 12) - nearest_enemy = instance_nearest(x,y,Xwing);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_TurretEmpire_Step_0 (line 12)
 

rIKmAN

Member
i changed all that then this came up

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_TurretEmpire:

Variable obj_TurretEmpire.Xwing(100009, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 12) - nearest_enemy = instance_nearest(x,y,Xwing);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_TurretEmpire_Step_0 (line 12)
Get into the habit of reading the error messages - they tell you what the problem is.
Code:
Variable obj_TurretEmpire.Xwing(100009, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 12) - nearest_enemy = instance_nearest(x,y,Xwing);
This means that the variable "Xwing" does not exist, but you are trying to use it in the instance_nearest() function.
The instance_nearest() function requires an object to be passed into it, so it looks like Xwing should be obj_Xwing based off your previous code.
 
A

AGameMaker

Guest
i changed all that then this came up

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_TurretEmpire:

Variable obj_TurretEmpire.Xwing(100009, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 12) - nearest_enemy = instance_nearest(x,y,Xwing);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_TurretEmpire_Step_0 (line 12)
i figured out my mistake it's all good thank you ☺
 
A

AGameMaker

Guest
Get into the habit of reading the error messages - they tell you what the problem is.
Code:
Variable obj_TurretEmpire.Xwing(100009, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 12) - nearest_enemy = instance_nearest(x,y,Xwing);
This means that the variable "Xwing" does not exist, but you are trying to use it in the instance_nearest() function.
The instance_nearest() function requires an object to be passed into it, so it looks like Xwing should be obj_Xwing based off your previous code.
you're right that was my problem but as i fixed it it did go back to my first problem on the Constant Xwing from earlier
 

rIKmAN

Member
you're right that was my problem but as i fixed it it did go back to my first problem on the Constant Xwing from earlier
Then take the advice I gave earlier, and make sure that you are not using variable names that are the same as object names.

This is for all variables / objects, not just the one we fixed earlier.
 
A

AGameMaker

Guest
Then take the advice I gave earlier, and make sure that you are not using variable names that are the same as object names.

This is for all variables / objects, not just the one we fixed earlier.
Right which i did i'm just changing them cause maybe i messed it up myself on the transition of the original to my own
 

rIKmAN

Member
Right which i did i'm just changing them cause maybe i messed it up myself on the transition of the original to my own
Yeah possibly.

Go through it a few times and check and if you are sure everything is correct then post the error message you are getting and post your code.
Post your code in code tags, or use the buttons at the top of the text box (Insert > Code) to make things easier to read.
 
A

AGameMaker

Guest
Code:
bulletSpeed = 4
//Find the nearest enemy:
nearest_enemy = instance_nearest(x,y, obj_Xwing);

//Make sure we actually have something to aim at!
if (nearest_enemy > 0) { //This is a GM pointer trick - pointers to objects are set to -1 if they're not pointing at anything
  //Check to see how close that enemy is:
  if (point_distance(x, y, nearest_enemy.x, nearest_enemy.y) < 200) {
    //Spawn a bullet:
    TurretBullets = instance_create_depth(x,y, 5, obj_TurretBullets);
 
    //Aim it at the specific enemy from earlier:
    with (obj_TurretBullets) {
      move_towards_point(other.nearest_enemy.x, other.nearest_enemy.y, bulletSpeed);
      image_angle = direction;
    }
  }
}

//Aim it at the specific enemy from earlier:
   Xwing = nearest_enemy;
  with (TurretBullets) {
    move_towards_point(x, y, obj_TurretBullets);
    image_angle = direction;
  }
i know that Xwing is wrong because it's telling me it's only referenced once but other than that it should be all correct
 

rIKmAN

Member
Code:
bulletSpeed = 4
//Find the nearest enemy:
nearest_enemy = instance_nearest(x,y, obj_Xwing);

//Make sure we actually have something to aim at!
if (nearest_enemy > 0) { //This is a GM pointer trick - pointers to objects are set to -1 if they're not pointing at anything
  //Check to see how close that enemy is:
  if (point_distance(x, y, nearest_enemy.x, nearest_enemy.y) < 200) {
    //Spawn a bullet:
    TurretBullets = instance_create_depth(x,y, 5, obj_TurretBullets);
 
    //Aim it at the specific enemy from earlier:
    with (obj_TurretBullets) {
      move_towards_point(other.nearest_enemy.x, other.nearest_enemy.y, bulletSpeed);
      image_angle = direction;
    }
  }
}

//Aim it at the specific enemy from earlier:
   Xwing = nearest_enemy;
  with (TurretBullets) {
    move_towards_point(x, y, obj_TurretBullets);
    image_angle = direction;
  }
i know that Xwing is wrong because it's telling me it's only referenced once but other than that it should be all correct
What's the error message?
 
A

AGameMaker

Guest
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_TurretEmpire:

Variable obj_TurretEmpire.TurretBullets(100010, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 31) - with (TurretBullets) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_TurretEmpire_Step_0 (line 31)
 

rIKmAN

Member
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_TurretEmpire:

Variable obj_TurretEmpire.TurretBullets(100010, -2147483648) not set before reading it.
at gml_Object_obj_TurretEmpire_Step_0 (line 31) - with (TurretBullets) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_TurretEmpire_Step_0 (line 31)
OK, so it's the same error as earlier and as before the error message is telling you the problem and exactly where in your code it is occurring.
Code:
Variable obj_TurretEmpire.TurretBullets(100010, -2147483648) not set before reading it.
 at gml_Object_obj_TurretEmpire_Step_0 (line 31) -   with (TurretBullets) {
The variable "TurretBullets" in the object "obj_TurretEmpire" is not set at the time you are trying to use it in the "with(TurretBullets)" statement on line 31 of the Step Event of your object "obj_TurretEmpire".

I'm not going to give you the exact answer as this forum is about helping people to learn for themselves, but I will ask you a question regarding the code you posted.

If nearest_enemy is NOT bigger than 0, and/or point_distance is NOT less than 200, then what is the value of TurretBullets?
More importantly does it even exist at all?

If it does exist even if those if checks aren't met (*cough* Create Event *cough*), would it be worth checking that it contains an instance before trying to use it later on in the with() statement?
A function like instance_exists() might come in handy here so you don't try and use something which doesn't exist.

Using show_debug_message() to output values to the console is an easy way of checking what values your variables contain. Using the debugger would be better but is a little more complicated.
 
A

AGameMaker

Guest
well so i had a issue i resolved everything that's wrong, but now my turret just has to rotate which i'm pretty sure i wrote that in and i'm confused to as why it isn't rotating towards my enemies. The turrets shoot little snakes out it looks like but i'm assuming cause the turret isn't moving towards the enemy.
 
Top