GML Change character control when colliding with an invisible object

Raskovia

Member
Hey! I´ve been having a headache meanwhile i was trying to resolve the code to do this: the videogame am making is told in the view of the three principal characters, despite the lineal story. I cant make the player object change their sprites (because they have a lot) and also, the mecanics each character has are diferent. My first though to resolve the problem was to make them collide with another object that makes the control change, but i cant make it work. Im not looking for a selection menu, just that when an object collides with an invisible object, the control switches to the right character (object).
I dont even know if it is possible...

I leave here some of the codes that i tried but didnt work... // surprise


Code:
globalvar controlList

global.controlList[0]=obj_aster //> ASTER
global.controlList[1]=obj_aster //> ANZU
global.controlList[2]=obj_aster //> NORA
global.controlList[3]=obj_nora //> NORA
global.controlList[4]=obj_nora //> ASTER
global.controlList[5]=obj_nora //> ANZU
global.controlList[6]=obj_anzu //> ANZU
global.controlList[7]=obj_anzu //> NORA
global.controlList[8]=obj_anzu//> ASTER

global.controlCurrent = global.controlList

instance_change(controlList[global.controlCurrent],0)

if collision_point(x,y,obj_salida, false, true) = true
{ instance_change(obj_aster,false) }
else collision_point(x,y,obj_entrada, false, true)
{ instance_change(obj_aster,true) }

Code:
var controlList[0] =obj_aster //> ASTER
   if controlList[0] = true
     {collision_point(x,y,obj_entrada,false,true)}
     {instance_change(obj_entrada,true) }
      
then controlList0
{
    instance_change(obj_aster,false)
}
Code:
control = collision point (instance_change(obj_aster,true)) or  instance_change(obj_aster,false)
Thank you for taking the time to read this!! :)
 

LilRony

Member
I think I understand your question: you want to make it so that when you touch a fellow character, you switch to controlling them? If so, I think I can help:

Code:
var newCharacter = noone;
var numCharacters = 3;
for(var i = 0; i < numCharacters; i++) {
 
    if (global.controlList[i] != object_index) {
        var characterTouching = instance_place(x + hspeed, y + vspeed, global.controlList[i]);
        if (characterTouching != noone) {

            global.currentCharacter = characterTouching;
            hspeed = 0; vspeed = 0;
            break;

        }

    }

}
Put that code in the code for cotrolling every character. In addition, wrap the character controlling code inside the following if-statement:
Code:
if (global.currentCharacter == object_index) {

    // all of your controlling code

}
You'll have to introduce the global.currentCharacter variable somewhere. and global.controlList[] array can just contain your three main characters.

Also note how I used x + hspeed and y + vspeed when checking for collision. You should change hspeed and vspeed to whatever your movement variables are, if you're using custom variables. The reason for this is because checking at (x, y) will trigger infinite character swaps, because character A will touch character B, then character B will see that they're touching character A, and so on. Instead, we look ahead to where we will be in the next frame, and if we will be touching someone, then we stop moving and change the current character to the one we were touching.

You can also put the first code snippet into a script called scrChangeCharacters() or something and just call that at the beginning or end of your controlling code.
 
Top