• 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!

Spawning Wrong Characters in Wrong Rooms

Velocity

Member
Hey,

So I'm making a Fighting Game like Street Fighter...

And IN it, I have an "arcade ladder", where you fight different characters as the game progresses.

But I'm having a problem, where it's spawning the wrong character for the room it's currently in.

So I made an arcade ladder controller, and in its create event, I have:

// Start of level (create)
if (room != rm_intro)
{
global.level = -1;
for(var i = 0; i < array_length_1d(global.rooms); i++)
{
if(room == global.rooms)
{
global.level = i;
break;
}
}
instance_create(512,288, global.enemies[global.level]); //if global.level == -1, you are in a room where nothing is coded to spawn.
}



And then, in its game start event, I have this:

// Start of game
global.enemies[0] = obj_itachiE;
global.rooms[0] = rm_battle ;
global.enemies[1] = obj_nejiE;
global.rooms[1] = rm_hong_kong ;
global.enemies[2] = obj_narutoE ;
global.rooms[2] = rm_light_house ;
global.enemies[3] = obj_kakashiE;
global.rooms[3] = rm_saji_mansion ;
global.level = 0;


obj_narutoE stands for obj Naruto ENEMY...

So I have an enemy parent object that selects the enemy object to act as the enemy in a fight.




So my PROBLEM is, that in rm_saji_mansion, INSTEAD of having obj_kakashiE as my enemy... I have obj_narutoE... And I can't seem to figure out how to CHANGE it...

I ALSO wanted to switch obj_itachiE around with obj_narutoE, and have obj_itachiE in rm_light_house, and obj_narutoE in rm_battle... But I can't seem to do THAT, either... Because last time I tried, I got error messages...



But also, as experimentation, I've also made things like:

An object named obj_globalenemyequalsnaruto

Where, in the create event, I have:

global.enemy = obj_narutoE

Or another object, named:

Create_Naruto

And in it's room end event, I have:

instance_create(512,288,obj_narutoE);

And these objects are scattered about in different rooms...





Can anyone help me?

Cheers.
 

Simon Gust

Member
Not sure why it doesn't work, but you can make code way easier than this.
Since each room has it's own enemy, just make the room id the array index and the entry the enemy object.
Code:
global.enemy[rm_battle] = obj_itachiE;
global.enemy[rm_hong_kong] = obj_nejiE;
global.enemy[rm_light_house] = obj_narutoE;
global.enemy[rm_saji_mansion] = obj_kakashiE;
And when you traverse a room
Code:
if (room != rm_intro)
{
    instance_create(512, 288, global.enemy[room]);
}
 

Velocity

Member
Not sure why it doesn't work, but you can make code way easier than this.
Since each room has it's own enemy, just make the room id the array index and the entry the enemy object.
Code:
global.enemy[rm_battle] = obj_itachiE;
global.enemy[rm_hong_kong] = obj_nejiE;
global.enemy[rm_light_house] = obj_narutoE;
global.enemy[rm_saji_mansion] = obj_kakashiE;
And when you traverse a room
Code:
if (room != rm_intro)
{
    instance_create(512, 288, global.enemy[room]);
}
I tried it, and I got this error:

FATAL ERROR in
action number 1
of Create Event
for object obj_arcade_ladder_controller:


trying to index a variable which is not an array
at gml_Object_obj_arcade_ladder_controller_Create_0 (line 30) - instance_create(512, 288, global.enemy[room]);
 

Velocity

Member
I tried it, and I got this error:

FATAL ERROR in
action number 1
of Create Event
for object obj_arcade_ladder_controller:


trying to index a variable which is not an array
at gml_Object_obj_arcade_ladder_controller_Create_0 (line 30) - instance_create(512, 288, global.enemy[room]);

I put THIS into the game start event of my obj_arcade_ladder_controller:


global.enemy[rm_battle] = obj_itachiE;
global.enemy[rm_hong_kong] = obj_nejiE;
global.enemy[rm_light_house] = obj_narutoE;
global.enemy[rm_saji_mansion] = obj_kakashiE;


And I put THIS into the create event of that same object:

if (room != rm_intro)
{
instance_create(512, 288, global.enemy[room]);
}
 

Simon Gust

Member
I put THIS into the game start event of my obj_arcade_ladder_controller:


global.enemy[rm_battle] = obj_itachiE;
global.enemy[rm_hong_kong] = obj_nejiE;
global.enemy[rm_light_house] = obj_narutoE;
global.enemy[rm_saji_mansion] = obj_kakashiE;


And I put THIS into the create event of that same object:

if (room != rm_intro)
{
instance_create(512, 288, global.enemy[room]);
}
Is obj_arcade_ladder_controller flagged as persistent?
What also could cause an issue is that maybe the create event runs before the game start event.
 

Velocity

Member
Is obj_arcade_ladder_controller flagged as persistent?
What also could cause an issue is that maybe the create event runs before the game start event.
No, obj_arcade_ladder_controller isn't persistant...

So, is there some way around this?

Cheers.
 

Simon Gust

Member
No, obj_arcade_ladder_controller isn't persistant...

So, is there some way around this?

Cheers.
It should definetly be persistent as it is a controlling object. Put it in rm_intro and in no other room.
With it being persistent, the game start event has no importance and you can put this code in the create event.
Code:
global.enemy[rm_battle] = obj_itachiE;
global.enemy[rm_hong_kong] = obj_nejiE;
global.enemy[rm_light_house] = obj_narutoE;
global.enemy[rm_saji_mansion] = obj_kakashiE;
And this code
Code:
if (room != rm_intro)
{
    instance_create(512, 288, global.enemy[room]);
}
in the room start event.
upload_2018-6-9_12-43-32.png
And see if that works.
If you're ever unsure in what order the events are called. The manual explains it pretty well
https://docs.yoyogames.com/source/dadiospice/000_using gamemaker/events/index.html
 

Velocity

Member
Thanks man. It worked!

I had some error problems when I tried making the object persistent. I think it's because it was trying to load all of my sprite config scripts without having characters loaded or something. But it works when I just put the controller into all of the battle rooms.

Also, it turns out that I had some code in my player parent object saying if room = rm_light_house {global.enemy = narutoE}. Which is why I couldn't vs kakashiE there.
 

Simon Gust

Member
Thanks man. It worked!

I had some error problems when I tried making the object persistent. I think it's because it was trying to load all of my sprite config scripts without having characters loaded or something. But it works when I just put the controller into all of the battle rooms.

Also, it turns out that I had some code in my player parent object saying if room = rm_light_house {global.enemy = narutoE}. Which is why I couldn't vs kakashiE there.
Just note that if you put a persistent object in every room, at the end of your game you'll have a million of that object running at the same time making an inviting home to bugs and errors.
 

Velocity

Member
Just note that if you put a persistent object in every room, at the end of your game you'll have a million of that object running at the same time making an inviting home to bugs and errors.
Oh, yeah... I didn't end up making it persistent... I just put it into all the relevant battle rooms...
 
Top