GML [SOLVED] Using variable from 'other' Instance not working.

K

Kabutsk

Guest
Hi, this is likely a noob question but i'm trying to get a variable from an instance, but for some reason it doesn't use the variable.

if (position_meeting(x - 60 , y + 10, obj_tile) && stop = false) {

with position_meeting(x - 60, y + 10, obj_tile) { // Adding bomb count to tile

self.rowbomb = self.rowbomb + other.bomb

}
}
i'm trying to use "self" for the instance calling the command, but i think it's using obj_tile as "self".
can anyone help with this? I can't use instance ID's since it's in a randomly generated field.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Hi, this is likely a noob question but i'm trying to get a variable from an instance, but for some reason it doesn't use the variable.

if (position_meeting(x - 60 , y + 10, obj_tile) && stop = false) {

with position_meeting(x - 60, y + 10, obj_tile) { // Adding bomb count to tile

self.rowbomb = self.rowbomb + other.bomb

}
}
i'm trying to use "self" for the instance calling the command, but i think it's using obj_tile as "self".
can anyone help with this? I can't use instance ID's since it's in a randomly generated field.
you do not use the "self" just remove it... regarding the "other" it should work as long as the variable exists within the object you are calling this script in!
it must be a instance variable (in blue) not a local variable (yellow).
if it is a local variable you will not need to use other as well.
 
K

Kabutsk

Guest
you do not use the "self" just remove it... regarding the "other" it should work as long as the variable exists within the object you are calling this script in!
it must be a instance variable (in blue) not a local variable (yellow).
if it is a local variable you will not need to use other as well.
alright, i removed the self. the other things should be right though
 
K

Kabutsk

Guest
position_meeting returns a boolean, therefore you can't use it with the with command.
alright, i removed the self. the other things should be right though
i did use instance_position as well but it completely crashes, saying this;
FATAL ERROR in
action number 1
of Step Event0
for object obj_sidetile:

Variable obj_tile.rowbomb(100005, -2147483648) not set before reading it.
at gml_Object_obj_sidetile_Step_0 (line 8) - rowbomb = rowbomb + other.bomb
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_sidetile_Step_0 (line 8)

just to be clear, rowbomb is the variable i'm trying to use for the instance calling the command, not obj_tile
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
i did use instance_position as well but it completely crashes, saying this;
FATAL ERROR in
action number 1
of Step Event0
for object obj_sidetile:

Variable obj_tile.rowbomb(100005, -2147483648) not set before reading it.
at gml_Object_obj_sidetile_Step_0 (line 8) - rowbomb = rowbomb + other.bomb
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_sidetile_Step_0 (line 8)

just to be clear, rowbomb is the variable i'm trying to use for the instance calling the command, not obj_tile
so you need to use the "other.rowbomb" on them.... then again if you want to use "with" you should be using instance_position instead as @Tommah stated out!
you use other to reference the instance you are calling the script on (when you are inside a with statement)
 
K

Kabutsk

Guest
so you need to use the "other.rowbomb" on them.... then again if you want to use "with" you should be using instance_position instead as @Tommah stated out!
you use other to reference the instance you are calling the script on (when you are inside a with statement)
thanks, i didn't know that. and yes i'm already using instance_position. there still seems to be a problem though. what i'm trying to do is displaying the rowbomb variable, with this code;

if (rowbomb = 0 && draw = true){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb0, 0 , x , y)

}

else if (rowbomb = 1){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb1, 0 , x , y)

}
for some reason it draws the sprite bomb0 and the sprite_index, even if i dont set draw to true, and even if rowbomb should be 1. i'm using this perticular code in the draw event btw.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
thanks, i didn't know that. and yes i'm already using instance_position. there still seems to be a problem though. what i'm trying to do is displaying the rowbomb variable, with this code;

if (rowbomb = 0 && draw = true){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb0, 0 , x , y)

}

else if (rowbomb = 1){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb1, 0 , x , y)

}
for some reason it draws the sprite bomb0 and the sprite_index, even if i dont set draw to true, and even if rowbomb should be 1. i'm using this perticular code in the draw event btw.
what should be drawn is the sprite_index and bomb1 in the second "if" (else if) you are not testing for the "draw = true" so it ignores it if you want not to draw if "draw = false" than change "if else" to "rowbomb == 1 && draw == true".

PS: when checking for equality between to members you should use "==" instead of "=" it works either way but it is good practice to use "==" so people can easily understand your code... or even you in the future. You said you were new to this.. so better learn the good practice from the start!
 
K

Kabutsk

Guest
so it ignores it if you want not to draw if "draw = false" than change "if else" to "rowbomb == 1 && draw == true".
sorry, i don't get this part
if you meant changing to this;

if (rowbomb == 0 && draw == true){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb0, 0 , x , y)

}

if (rowbomb == 1 && draw == true){

draw_sprite(sprite_index, 0 , x , y)
draw_sprite(bomb1, 0 , x , y)

}

then it still won't work, it displays the sprite bomb0, instead of bomb1. i think there is something with the step event, since if i change the rowbomb to 1 in the create event, it displays bomb1, so i think somehow it doesnt add the "bomb" count from the obj_tile from which it's checking in the step event. i know that the obj_tile does have the bomb count created for a fact.
thanks for the tip though, i'll use "==" from now on
 
K

Kabutsk

Guest
alright, i've got it. I should've used the begin step event in the object i'm checking. thanks for the help
 
Top