(SOLVED) Error when passing an object as a script argument

N

nagi

Guest
Greetings, first time posting here. I'm trying to make a standard turn-based battle system, but I keep having trouble when starting the battle from a script.

I'm trying to pass enemy objects as arguments, so the script assigns them to the enemy array in the battle_logic object. Here's my code:

Calling the script
Code:
if (keyboard_check_pressed(vk_f5) && room == test_room)
 script_execute(script_battle_start, obj_battle_enemy1);
Contents of the script
Code:
//Used to start a battle
//argument0, 1, 2 are the enemies to spawn (min. 1)

room_goto(room_battle);

instance_create(0, 0, obj_battle_logic);

//Create enemies up to the number of arguments passed
for (i = argument_count - 1; i >= 0; i--)
    obj_battle_logic.enemy[i] = instance_create(0, 0, argument[i]);
obj_battle_logic creation code
Code:
//Initializing enemy array
for (i = 0; i < 3; i++)
    enemy[i] = noone;
Problem is, everything works properly... except enemy1 is never created.

It worked, however, when using instance_create while initializing the enemy array directly in the battle_logic object.

When trying to attack the non-existant enemy, the following error occurs (note that I hardcoded enemy[0] for testing purposes only).
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 2
of  Step Event0
for object obj_battle_atk_pointer:

Variable <unknown_object>.<unknown variable>(100008, -2147483648) not set before reading it.
 at gml_Script_script_battle_damage_normal (line 5) - obj_battle_logic.enemy[0].life -= (argument0 - obj_battle_logic.enemy[0].def);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_script_battle_damage_normal (line 5)
called from - gml_Object_obj_battle_atk_pointer_StepNormalEvent_2 (line 10) -         script_execute(script_battle_damage_normal, dmg);

Any help is greatly appreciated.
 
From the room_goto() docs:

Note that calling this function does not instantly change rooms, and the room will not change until the end of the current game frame (meaning that any code after this function will still be run, as will some events).
 
Hmmm...ok. Well the error message is saying that the instance you are trying to access doesn't exist, and therefore also the variable doesn't exist.

So the instance was either destroyed during the room change, or if it made survived the room change because it was persistent, something else removed it from the room.

What's the contents of : script_battle_damage_normal ?

Could you have perhaps destroyed the enemy, and not removed the reference to the enemy from the enemy[0] array?

You could add an "if (instance_exists(enemy[0]) ) " check before you try to use that code to make sure the instance is still there.

To get to the bottom of it, I would put in a few debug messages to print out the status of the enemy[] array, and also use the debugger to step through the code and to view what instances are active in the room, find out when and where they are being destroyed.
 
N

nagi

Guest
Hmmm...ok. Well the error message is saying that the instance you are trying to access doesn't exist, and therefore also the variable doesn't exist.

So the instance was either destroyed during the room change, or if it made survived the room change because it was persistent, something else removed it from the room.

What's the contents of : script_battle_damage_normal ?

Could you have perhaps destroyed the enemy, and not removed the reference to the enemy from the enemy[0] array?

You could add an "if (instance_exists(enemy[0]) ) " check before you try to use that code to make sure the instance is still there.

To get to the bottom of it, I would put in a few debug messages to print out the status of the enemy[] array, and also use the debugger to step through the code and to view what instances are active in the room, find out when and where they are being destroyed.
script_battle_damage_normal contains a single line that reduces the life of enemy[0], it doesn't even destroy it.
Code:
obj_battle_logic.enemy[0].life -= (argument0 - obj_battle_logic.enemy[0].def);
Anyways, I re-coded everything from scratch and it magically worked now, sort of...
All instances were created correctly, but were invisible. Pretty sure it had something to do with room_goto as you said, so I added a room-start event to set the correct position of all involved objects. It works now, but code became spaghetti. :oops: I'll have to refactor it later.

Thanks for your help! =D
 
Top