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

GML [SOLVED] Trying to compare scores of 10 players. Not working

  • Thread starter Kleber_Ferreira
  • Start date
K

Kleber_Ferreira

Guest
When the game ends, an object will be instantiated to show a congratulations message and the winner's name with his score.
The code shows no error messages but when this object is instatiated, the variables 'winner' (to hold the player's name after checking who won) and 'winnerPontos' (to hold the winner's score) don't get updated, thus displaying this text: "Congratulations null, you won with 0.


//Create event
GML:
winner = "null";
winnerPontos = 0;

if (obj_student1.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student1;
    winnerPontos = obj_student1.pontos;
} else if (obj_student2.pontos > obj_student1.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student2;
    winnerPontos = obj_student2.pontos;
} else if (obj_student3.pontos > obj_student1.pontos && obj_student2.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student3;
    winnerPontos = obj_student3.pontos;
} else if (obj_student4.pontos > obj_student2.pontos && obj_student3.pontos && obj_student1.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student4;
    winnerPontos = obj_student4.pontos;
} else if (obj_student5.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student1.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student5;
    winnerPontos = obj_student5.pontos;
} else if (obj_student6.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student1.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student6;
    winnerPontos = obj_student6.pontos;
} else if (obj_student7.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student1.pontos && obj_student8.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student7;
    winnerPontos = obj_student7.pontos;
} else if (obj_student8.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student1.pontos && obj_student9.pontos && obj_student10.pontos) {
    winner = global.student8;
    winnerPontos = obj_student8.pontos;
} else if (obj_student9.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student1.pontos && obj_student10.pontos) {   
    winner = global.student9;
    winnerPontos = obj_student9.pontos;
} else if (obj_student10.pontos > obj_student2.pontos && obj_student3.pontos && obj_student4.pontos && obj_student5.pontos && obj_student6.pontos && obj_student7.pontos && obj_student8.pontos && obj_student9.pontos && obj_student1.pontos) {
    winner = global.student10;
    winnerPontos = obj_student10.pontos;
}
//Draw event


draw_self();
draw_set_color(c_white);
draw_set_halign(fa_middle);
draw_set_font(coordinates);
draw_text(x, y, "CONGRATULATIONS!");
draw_set_font(segoeUI);
draw_text(x, y+40, winner + " you won with " + string(winnerPontos));

 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Your code is incorrectly formatted to start with... You can't do multiple comparisons that way and need to compare everything individually, eg:
GML:
(obj_student1.pontos > obj_student2.pontos) && (obj_student1.pontos > obj_student3.pontos) && (obj_student1.pontos > obj_student4.pontos) etc....
That said, since you are using objects for the points you can do all this in a MUCH simpler fashion using "with":
GML:
var _winner = noone;
var _highest= 0;
with (OBJ_STUDENT_PARENT)
{
if pontos > _highest
    {
    _winner = object_index;
    _highest = pontos;
    }
}
So, this code requires you to create a PARENT object for all the student/player objects, and will create two local variables, where one (_winner) will hold the object index of the winning object and the other (_highest) will hold the score. You can then use (for example) a "switch" to get the correct global variable based on the object_index, eg:
GML:
if _winner != noone
{
switch(_winner)
    {
    case obj_student1: winner = global.student1; break;
    }
pontos = _highest;
}
else
{
winner = "NULL";
pontos = 0;
}
Note that instead of using global variables for the student name or identifier, I'd simply have it in each of the objects then you can modify the "with" so it looks like this (and avoid using a "switch"):
GML:
var _winner = -1;
var _highest = 0;
var _name = "NULL"
with (OBJ_STUDENT_PARENT)
{
if pontos > _highest
    {
    _winner = object_index;
    _highest = pontos;
    _name = myname;
    }
}
if _winner != -1
{
winnerPontos = _pontos;
winner = _name;
}
 

TailBit

Member
you can't write it like that .. you have to do the full statement over again
if (obj_student1.pontos > obj_student2.pontos && obj_student1.pontos > obj_student3.pontos && obj_student1.pontos > obj_student4.pontos ...

I do like to use with .. but I don't think your variable system is fit for that .. you should have used arrays ..
GML:
winner = "null";
winnerPontos = 0

// if they have a parent object, then "with(obj_student_parent){" is enough
with(all) if string_pos("obj_student", object_get_name(object_index) ) != 0 { // search all object to see if they contain "obj_student"
    if pontos > other.winnerPontos {
        other.winner = variable_global_exists( string_delete( object_get_name(object_index), 1, 4) );
        other.winnerPontos = pontos;
    }
}
But this is a bit cleaner method .. find the max value of all of them fist .. then go through them all
GML:
winner = "null";
winnerPontos = 0;

pontos_highest =max(obj_student1.pontos,obj_student2.pontos, ... ,obj_student10.pontos)

if(pontos_highest = obj_student1.pontos){ winner = global.student1; winnerPontos = obj_student1.pontos; }else
if(pontos_highest = obj_student2.pontos){ winner = global.student2; winnerPontos = obj_student2.pontos; }else
...
 
K

Kleber_Ferreira

Guest
Thank you all.

It was the wrong formatting of my comparisson lines.

And the tips for a cleaner code were extremely helpful too.
 
Top