GML I can't figure out why this crashes my Game.

O

OriginalGrim

Guest
I'm trying to make dialog appear in a textbox different to what is in the textbox object but after the textbox is created and distroyed the first time the next time i walk into the npc it crashes the game and I can't figure out why.

if distance_to_object(obj_player) <= 5 {
if keyboard_check(ord("E"))
if (textbox = noone)
textbox = instance_create_layer(obj_NPC_Bob.x, obj_NPC_Bob.y, "TextLayer", obj_textbox)
obj_textbox.text = "Hi I'm Bob";
}
if (textbox != noone)
if keyboard_check_pressed(ord("I")) {
instance_destroy(obj_textbox);
textbox = noone;
}

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

Unable to find any instance for object index '11' name 'obj_textbox'
at gml_Object_obj_parent_NPC_Step_0 (line 39) - if (textbox != noone)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_parent_NPC_Step_0 (line 39)

If there's other parts of the code needed to solve this just ask I've been trying to figure this out and it's incredibly frustrating.
 
I'm not entirely certain of what is causing the problem. Analyzing the error message, however, the game is trying to access variables in the textbox object when it doesn't exist- this would be your set for obj_textbox.text = "Hi I'm Bob", which throws an error because, naturally, you can't set variables in an object that doesn't exist. Looking over this code here, I've noticed one simple thing that might help you out...

Make sure to nest your checks.
In your code here, you have segments where you're checking multiple things in a row. When you do this, you want to make sure you have a { after each check (and then a matching } at the end of the code, of course). This ensures that the next check cannot trip until the current one returns true. The way you have them now, the game could very well be checking both checks, finding that the first is false, but then ignoring that to check the second one, as you haven't nested them properly.

I hope this helps you out! If nothing else, at least you know what the error is telling you now!
~Pir
 
O

OriginalGrim

Guest
I'm not entirely certain of what is causing the problem. Analyzing the error message, however, the game is trying to access variables in the textbox object when it doesn't exist- this would be your set for obj_textbox.text = "Hi I'm Bob", which throws an error because, naturally, you can't set variables in an object that doesn't exist. Looking over this code here, I've noticed one simple thing that might help you out...

Make sure to nest your checks.
In your code here, you have segments where you're checking multiple things in a row. When you do this, you want to make sure you have a { after each check (and then a matching } at the end of the code, of course). This ensures that the next check cannot trip until the current one returns true. The way you have them now, the game could very well be checking both checks, finding that the first is false, but then ignoring that to check the second one, as you haven't nested them properly.

I hope this helps you out! If nothing else, at least you know what the error is telling you now!
~Pir
Thank you this fixed the problem I'll have to make sure I do this more when I'm coding.

if distance_to_object(obj_player) <= 5 {
if keyboard_check(ord("E")) {
if (textbox = noone) {
textbox = instance_create_layer(obj_NPC_Bob.x, obj_NPC_Bob.y, "TextLayer", obj_textbox)
obj_textbox.text = "Hi I'm Bob";
}
}
}
if (textbox != noone) {
if keyboard_check_pressed(ord("I")) {
instance_destroy(obj_textbox); {
textbox = noone;
}
}
}
 
Top