Legacy GM [Help] Accessing variables from different objects

M

mrwappa

Guest
NOTE: I am new to the Game Maker language and don't have any noteable experience with sharing variables from different objects and instances!

I have the following problem:

  • I have been unsuccesfully trying to access a variable that checks collision from a different object

  • Error I'm receiving:
Variable o_circlegun.Bullet(100008, -2147483648) not set before reading it.

at gml_Object_o_circlegun_StepNormalEvent_1 (line 18) - if instance_exists(Bullet.TouchPlayer)

I am trying to:

  • I'm trying to access a variable that checks collision from a different object than the variable is declared

  • Code(Step Event) of object that checks collision(o_bulleth):

    PlayerTouch = instance_place(x,y,o_player)

    PlayerTouch.Bullet = self.id

    Code(Step Event) of object that uses the variable PlayerTouch from o_bulleth(o_circlegun):

    if instance_exists(Bullet.TouchPlayer)

    { image_angle -= 2.5 } else { image_angle += 2.5

    }
    • The goal of this is to make o_circlegun rotate either clockwise or counterclockwise depending on if o_bulleth has had a collision(touched) with o_player
I have tried:

  • I've searched for different methods, for example: How to grab X and Y of an instance(So I could use instance_place in the o_circlegun object), or adressing variables from other instances by making the global.

  • I tried to solve the problem by using this guide.
I am using:

 

Phil Strahl

Member
NOTE: I am new to the Game Maker language and don't have any noteable experience with sharing variables from different objects and instances!

I have the following problem:

  • I have been unsuccesfully trying to access a variable that checks collision from a different object

  • Error I'm receiving:
Variable o_circlegun.Bullet(100008, -2147483648) not set before reading it.

at gml_Object_o_circlegun_StepNormalEvent_1 (line 18) - if instance_exists(Bullet.TouchPlayer)

I am trying to:

  • I'm trying to access a variable that checks collision from a different object than the variable is declared

  • Code(Step Event) of object that checks collision(o_bulleth):

    PlayerTouch = instance_place(x,y,o_player)

    PlayerTouch.Bullet = self.id

    Code(Step Event) of object that uses the variable PlayerTouch from o_bulleth(o_circlegun):

    if instance_exists(Bullet.TouchPlayer)

    { image_angle -= 2.5 } else { image_angle += 2.5

    }
    • The goal of this is to make o_circlegun rotate either clockwise or counterclockwise depending on if o_bulleth has had a collision(touched) with o_player
I have tried:

  • I've searched for different methods, for example: How to grab X and Y of an instance(So I could use instance_place in the o_circlegun object), or adressing variables from other instances by making the global.

  • I tried to solve the problem by using this guide.
I am using:


I think this line is your problem
Code:
if instance_exists(Bullet.TouchPlayer)
as before you assigned the self-id to the variable Bullet in the PlayerTouch instance.
Try
Code:
if instance_exists(PlayerTouch.Bullet)
 
M

mrwappa

Guest
I think this line is your problem
Code:
if instance_exists(Bullet.TouchPlayer)
as before you assigned the self-id to the variable Bullet in the PlayerTouch instance.
Try
Code:
if instance_exists(PlayerTouch.Bullet)
Sadly, this didn't work as a fix.

GameMaker keeps telling me that the variable o_circledun.PlayerTouch is not set before reading it. Which leads me to believe there is something fundamentally wrong with how I'm sharing the variable.
This method wasn't my original thought of how I should have solved the problem:
I initially set out to use the X and Y values of o_bulleth inside of o_circlegun(Step Event) with in an instance_place variable that checks collision with the X and Y of o_bulleth and the instance of o_player.
That way I could check for a collision( instance_exists(PlayerTouch) ) with an instance_place variable that has been declared inside of o_circlegun. As I said before: "The goal of this is to make o_circlegun rotate either clockwise or counterclockwise depending on if o_bulleth has had a collision(touched) with o_player".

Evidently, it didn't work.
 

Tsa05

Member
What would happen if instance_place found no collision?
In this case, PlayerTouch is "noone"--a GameMaker value that correlates to no object.
So, noone.Bullet = self.id would not set the bullet variable for any object.
So Bullet does not get set.

In the Create event, be sure to define Bullet = noone.
In your instance_place checking code, be sure to check whether an actual instance of an object was returned: if(instance_exists(PlayerTouch))
 
M

mrwappa

Guest
What would happen if instance_place found no collision?
In this case, PlayerTouch is "noone"--a GameMaker value that correlates to no object.
So, noone.Bullet = self.id would not set the bullet variable for any object.
So Bullet does not get set.

In the Create event, be sure to define Bullet = noone.
In your instance_place checking code, be sure to check whether an actual instance of an object was returned: if(instance_exists(PlayerTouch))
I'll try to implement this later when I have time to sit down and understand it, thanks for the tip!
 
Top