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

[SOLVED] Array changing it's values for no reason

Didjynn

Member
Hello everybody, it's me again !

I don't know if I'm crazy, clumsy or stupid but softwares don't stop acting strange with me, so do Game Maker Studio 2.

First let me say something without any link with my bug but I have a "if" with 2 conditions "condition1 = 1 && condition2 = 1 that is not acting like it should in fact it's acting like if I wrote "if condition1 = 1 or condition2 = 1".
I replaced the "&&" with a "or" and now it's acting like if I wrote "&&"... is it me or is it broken sometimes ? (it happens only once but.... strange.)

Ok now I'll speak about my new problem. I generate a map and I use arrays for this. The code below creates 3 arrays : one for each square kind of ground, one for the density and one for the sprite it should use.

Here is the problem : it remembers perfectly the type of ground but reset randomly the density and the sprite to the orinignal state !

I used show_debug_message and the values are good as soon as it is in the "for". The last part of the code, the double for check each array in the same order and... ground type are kept but not the 2 others !
All this code is in the "create" event of the only object I use.

And it's not so random... Don't be affraid there is a lot of code because the more I go to the left on the map, the less there is tress,etc. Much code is just a repetition.

Code:
for(var m = room_width/16; m >= 0; m --) {
   for(var n = room_height/16; n >= 0; n --) {
       terrain[m, n] = ""
       densite[m, n] = 0
       carresprite[m, n] = socean
   }
}

for (var i = 0; i < room_width/size; i ++) {
   for (var j = 0; j < room_height/size; j ++) {
       var xpourcent = round(i * 100 /(room_width/16))
       var ypourcent = round(j * 100 /(room_height/16))
       var pourcent = round(random(100))
       var pourcentcomp = round(random(100))
      
       if j = 0 or i = room_width/size or (pourcent > 60 && (terrain[i, j-1] = "ocean" or terrain[i+1, j] = "ocean")){
           terrain[i, j] = "ocean"
       }else if ypourcent <= pourcent {
           //show_debug_message("plutôt au dessus")
           if xpourcent > pourcent/3*2 {
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 30 {
                   terrain[i, j] = "foretdense"//30
               }else if pourcentcomp < 40 + 30 + 20 {
                   terrain[i, j] = "foretclairsemée" //20
               }else{
                   terrain[i, j] = "clairiere" //10
               }
      
           }else if xpourcent > pourcent/3 {
      
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 15 {
                   terrain[i, j] = "foretdense"//15
               }else if pourcentcomp < 40 + 15 + 25 {
                   terrain[i, j] = "foretclairsemée" //25
               }else{
                   terrain[i, j] = "clairiere" //20
               }
      
           }else{
      
               if pourcentcomp < 50 {
                   terrain[i, j] = "plaine"
               }else if pourcentcomp <= 50 + 20 {
                   terrain[i, j] = "foretclairsemée"
               }else{
                   terrain[i, j] = "rocher"
               }
           }
  
       }else{
           //show_debug_message("plutôt en dessous")
           if xpourcent > pourcent/3*2 {
               if pourcentcomp < 30 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 30 + 35 {
                   terrain[i, j] = "marais"//30
               }else if pourcentcomp < 40 + 30 + 20 {
                   terrain[i, j] = "plaine" //20
               }else{
                   terrain[i, j] = "foretclairsemée" //10
               }
      
           }else if xpourcent > pourcent/3 {
      
               if pourcentcomp < 40 {
                   terrain[i, j] = "plaine"
               }else if pourcentcomp <= 40+30 {
                   terrain[i, j] = "montagne"
               }else{
                   terrain[i, j] = "rocher"
               }
      
           }else{
               //show_debug_message("plutôt au milieu")
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 15 {
                   terrain[i, j] = "foretdense"//15
               }else if pourcentcomp < 40 + 15 + 25 {
                   terrain[i, j] = "foretclairsemée" //25
               }else{
                   terrain[i, j] = "clairiere" //20
               }
           }

           show_debug_message(terrain[i, j])
          // HERE it just gie a different density and sprite depending of the ground's type
          
           if terrain[i, j] = "ocean" {
               carresprite[i, j] = socean
               densite[i, j] = .5
           }else if terrain[i, j] = "riviere" {
               carresprite[i, j] = sriviere
               densite[i, j] = .5
           }else if terrain[i, j] = "foretclairsemée"{
               densite[i, j] = .5
               carresprite[i, j] = sforetclairsemee
           }else if terrain[i, j] = "foret" {
               densite[i, j] = 1
               carresprite[i, j] = sforet
           }else if terrain[i, j] = "foretdense"{
               densite[i, j] = 1.5
               carresprite[i, j] = sforetdense
           }else if terrain[i, j] = "plaine"{
               densite[i, j] = .5
               carresprite[i, j] = splaine
           }else if terrain[i, j] = "marais"{
               if i * 16 < room_width/2 {
                   terrain[i, j] = "geyser"
                   densite[i, j] = 1
                   carresprite[i, j] = sgeyser
               }else{
                   densite[i, j] = 1.5
                   carresprite[i, j] = smarais
               }
           }else if terrain[i, j] = "montagne"{
               //var rand = round(random(10))
               //if rand = 5 souslieu = "grotte"
               densite[i, j] = 1.5
               carresprite[i, j] = smontagne
           }else if terrain[i, j] = "rocher"{
               densite[i, j] = 1
               carresprite[i, j] = splainerocheuse
           }else if terrain[i, j] = "clairiere"{
               densite[i, j] = .5
               carresprite[i, j] = sclairiere
           }else{
               show_debug_message("pas de case, pas de sprite")
           }
          
           show_debug_message(string(densite[i, j]))
           show_debug_message(string(carresprite[i, j]))
          
           if i = rw/2/16 && j = rh/2/16 {
               lieu[i, j] = "arbremere"
           }else{
               lieu[i, j] = ""
           }
       }
   }
}

for (var i = 0; i < room_width/size; i ++) {
   for (var j = 0; j < room_height/size; j ++) {
       show_debug_message("densite = " + string(densite[i, j]))
       show_debug_message(string(carresprite[i, j]))  
   }
}

When I told you it's not so random... The more we go to the bottom the less density is equal to 0 (and it can only be equal to 0 if the base value haven't been changed) and the less it uses the "basic" sprite.


Like Lilu would say in the fifth element, "Please, Help".

Thx for your help and your time !

Edit : I don't know why there is 2 attached files to this post, I don't know how to delete them and they are useless since the first pictures already shows everything. Sorry about that
 

Attachments

M

maratae

Guest
Hello everybody, it's me again !

I don't know if I'm crazy, clumsy or stupid but softwares don't stop acting strange with me, so do Game Maker Studio 2.

First let me say something without any link with my bug but I have a "if" with 2 conditions "condition1 = 1 && condition2 = 1 that is not acting like it should in fact it's acting like if I wrote "if condition1 = 1 or condition2 = 1".
I replaced the "&&" with a "or" and now it's acting like if I wrote "&&"... is it me or is it broken sometimes ? (it happens only once but.... strange.)

Ok now I'll speak about my new problem. I generate a map and I use arrays for this. The code below creates 3 arrays : one for each square kind of ground, one for the density and one for the sprite it should use.

Here is the problem : it remembers perfectly the type of ground but reset randomly the density and the sprite to the orinignal state !

I used show_debug_message and the values are good as soon as it is in the "for". The last part of the code, the double for check each array in the same order and... ground type are kept but not the 2 others !
All this code is in the "create" event of the only object I use.

And it's not so random... Don't be affraid there is a lot of code because the more I go to the left on the map, the less there is tress,etc. Much code is just a repetition.

Code:
for(var m = room_width/16; m >= 0; m --) {
   for(var n = room_height/16; n >= 0; n --) {
       terrain[m, n] = ""
       densite[m, n] = 0
       carresprite[m, n] = socean
   }
}

for (var i = 0; i < room_width/size; i ++) {
   for (var j = 0; j < room_height/size; j ++) {
       var xpourcent = round(i * 100 /(room_width/16))
       var ypourcent = round(j * 100 /(room_height/16))
       var pourcent = round(random(100))
       var pourcentcomp = round(random(100))
     
       if j = 0 or i = room_width/size or (pourcent > 60 && (terrain[i, j-1] = "ocean" or terrain[i+1, j] = "ocean")){
           terrain[i, j] = "ocean"
       }else if ypourcent <= pourcent {
           //show_debug_message("plutôt au dessus")
           if xpourcent > pourcent/3*2 {
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 30 {
                   terrain[i, j] = "foretdense"//30
               }else if pourcentcomp < 40 + 30 + 20 {
                   terrain[i, j] = "foretclairsemée" //20
               }else{
                   terrain[i, j] = "clairiere" //10
               }
     
           }else if xpourcent > pourcent/3 {
     
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 15 {
                   terrain[i, j] = "foretdense"//15
               }else if pourcentcomp < 40 + 15 + 25 {
                   terrain[i, j] = "foretclairsemée" //25
               }else{
                   terrain[i, j] = "clairiere" //20
               }
     
           }else{
     
               if pourcentcomp < 50 {
                   terrain[i, j] = "plaine"
               }else if pourcentcomp <= 50 + 20 {
                   terrain[i, j] = "foretclairsemée"
               }else{
                   terrain[i, j] = "rocher"
               }
           }
 
       }else{
           //show_debug_message("plutôt en dessous")
           if xpourcent > pourcent/3*2 {
               if pourcentcomp < 30 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 30 + 35 {
                   terrain[i, j] = "marais"//30
               }else if pourcentcomp < 40 + 30 + 20 {
                   terrain[i, j] = "plaine" //20
               }else{
                   terrain[i, j] = "foretclairsemée" //10
               }
     
           }else if xpourcent > pourcent/3 {
     
               if pourcentcomp < 40 {
                   terrain[i, j] = "plaine"
               }else if pourcentcomp <= 40+30 {
                   terrain[i, j] = "montagne"
               }else{
                   terrain[i, j] = "rocher"
               }
     
           }else{
               //show_debug_message("plutôt au milieu")
               if pourcentcomp < 40 {
                   terrain[i, j] = "foret"//40
               }else if pourcentcomp < 40 + 15 {
                   terrain[i, j] = "foretdense"//15
               }else if pourcentcomp < 40 + 15 + 25 {
                   terrain[i, j] = "foretclairsemée" //25
               }else{
                   terrain[i, j] = "clairiere" //20
               }
           }

           show_debug_message(terrain[i, j])
          // HERE it just gie a different density and sprite depending of the ground's type
         
           if terrain[i, j] = "ocean" {
               carresprite[i, j] = socean
               densite[i, j] = .5
           }else if terrain[i, j] = "riviere" {
               carresprite[i, j] = sriviere
               densite[i, j] = .5
           }else if terrain[i, j] = "foretclairsemée"{
               densite[i, j] = .5
               carresprite[i, j] = sforetclairsemee
           }else if terrain[i, j] = "foret" {
               densite[i, j] = 1
               carresprite[i, j] = sforet
           }else if terrain[i, j] = "foretdense"{
               densite[i, j] = 1.5
               carresprite[i, j] = sforetdense
           }else if terrain[i, j] = "plaine"{
               densite[i, j] = .5
               carresprite[i, j] = splaine
           }else if terrain[i, j] = "marais"{
               if i * 16 < room_width/2 {
                   terrain[i, j] = "geyser"
                   densite[i, j] = 1
                   carresprite[i, j] = sgeyser
               }else{
                   densite[i, j] = 1.5
                   carresprite[i, j] = smarais
               }
           }else if terrain[i, j] = "montagne"{
               //var rand = round(random(10))
               //if rand = 5 souslieu = "grotte"
               densite[i, j] = 1.5
               carresprite[i, j] = smontagne
           }else if terrain[i, j] = "rocher"{
               densite[i, j] = 1
               carresprite[i, j] = splainerocheuse
           }else if terrain[i, j] = "clairiere"{
               densite[i, j] = .5
               carresprite[i, j] = sclairiere
           }else{
               show_debug_message("pas de case, pas de sprite")
           }
         
           show_debug_message(string(densite[i, j]))
           show_debug_message(string(carresprite[i, j]))
         
           if i = rw/2/16 && j = rh/2/16 {
               lieu[i, j] = "arbremere"
           }else{
               lieu[i, j] = ""
           }
       }
   }
}

for (var i = 0; i < room_width/size; i ++) {
   for (var j = 0; j < room_height/size; j ++) {
       show_debug_message("densite = " + string(densite[i, j]))
       show_debug_message(string(carresprite[i, j])) 
   }
}

When I told you it's not so random... The more we go to the bottom the less density is equal to 0 (and it can only be equal to 0 if the base value haven't been changed) and the less it uses the "basic" sprite.


Like Lilu would say in the fifth element, "Please, Help".

Thx for your help and your time !

Edit : I don't know why there is 2 attached files to this post, I don't know how to delete them and they are useless since the first pictures already shows everything. Sorry about that
(on you 'm' and 'n' there's gonna be a whole row and collumn of stuff outside the room)
- If you're gonna use a bunch of 'ands' and 'ors' you need parenthesis.
- If you're gonna use a lot of if/else, you're better off with a switch.
- Beware that 'random()' returns a double. Use irandom() for integers.
- don't forget to use randomize() at the beginning of the code, cause if not, the result is always gonna be the same.
 

Didjynn

Member
Thank you for your answer :)

For the 'm' and 'n', it gives me no troubles but if I change it to
Code:
for(var m = room_width/16 - 16; m >= 0; m --) {
   for(var n = room_height/16 - 16; n >= 0; n --) {
It tells me my array is out of range. If I write
Code:
for(var m = room_width/16; m > 0; m --) {
   for(var n = room_height/16; n > 0; n --) {
I have a strange new bug: my first sprite in the assets of the game show up and it seems linked to the bug I'm trying to get rid of.



I'm not using randomize for now to better handle bug situations, almost all the game is random.

I'm using parenthesis in my bug with the '&&' and 'or', I even separate it from everything else by creating a 'if' only for these but still the same strange situation. Not a big problem since I know I just have to revert te signs.
 
M

maratae

Guest
Thank you for your answer :)

For the 'm' and 'n', it gives me no troubles but if I change it to
Code:
for(var m = room_width/16 - 16; m >= 0; m --) {
   for(var n = room_height/16 - 16; n >= 0; n --) {
It tells me my array is out of range. If I write
Code:
for(var m = room_width/16; m > 0; m --) {
   for(var n = room_height/16; n > 0; n --) {
I have a strange new bug: my first sprite in the assets of the game show up and it seems linked to the bug I'm trying to get rid of.



I'm not using randomize for now to better handle bug situations, almost all the game is random.

I'm using parenthesis in my bug with the '&&' and 'or', I even separate it from everything else by creating a 'if' only for these but still the same strange situation. Not a big problem since I know I just have to revert te signs.
Code:
if j = 0 or i = room_width/size or (pourcent > 60 && (terrain[i, j-1] = "ocean" or terrain[i+1, j] = "ocean"))
Are you absolutely positive this line couldn't use some more parenthesis?
 

Didjynn

Member
Yes I just read it 3 times, it should make a square to be an ocean if it's the first line, last colomn of the map or if there is an ocean upward or on the right of the square with a 40% chances. Sounds good to me. The only mistake was the
i = room_width/size that I made i = room_width/size - 1 so now it's really showing the ocean on the right side. Still nothing to do with my bug :'(

It's strange something is true in the "for" when I check it and if I check it again once the for is over it's not true anymore... no code (or any other action) could happen between the 2 if I don't mistake about how game maker works.
 
Last edited:
M

maratae

Guest
Yes I just read it 3 times, it should make a square to be an ocean if it's the first line, last colomn of the map or if there is an ocean upward or on the right of the square with a 40% chances. Sounds good to me. The only mistake was the
i = room_width/size that I made i = room_width/size - 1 so now it's really showing the ocean on the right side. Still nothing to do with my bug :'(

It's strange something is true in the "for" when I check it and if I check it again once the for is over it's not true anymore... no code (or any other action) could happen between the 2 if I don't mistake about how game maker works.
Although GML is rather liberal, in my experience doing stuff by the book usually gets the job done. That includes not only parenthesis everywhere even when you'd think it's not necessary but also comparing with == instead of =, and every other C derived language rule. I'd still change that big if/else into a switch statement.
Truth is I can't find right away what is it that's wrong with your code, and I'm just trying to exclude some basic stuff that might have been overlooked.
Good luck and might someone more qualified than me help you, hopefuly.
 

Didjynn

Member
I tried to listen to you and use a switch but I don't know how to formulate it... I mean, what should he in the HERE of the switch(HERE) statement ? And can we make a switch inside of a switch ?

Anyway I found the problem, the code to create the depth and sprite used is one '}' too early that's why it wasn't activating all the time.

Problem solved (still if you answer me I'll try this switch :) )
 
M

maratae

Guest
I tried to listen to you and use a switch but I don't know how to formulate it... I mean, what should he in the HERE of the switch(HERE) statement ? And can we make a switch inside of a switch ?

Anyway I found the problem, the code to create the depth and sprite used is one '}' too early that's why it wasn't activating all the time.

Problem solved (still if you answer me I'll try this switch :) )
Code:
switch (terrain[i, j])
{
    case "ocean":
        carresprite[i, j] = socean;
        densite[i, j] = .5;
    break;
    case "riviere":
        carresprite[i, j] = sriviere;
        densite[i, j] = .5;
    break;
    case "foretclairsemée":
        carresprite[i, j] = sforetclairsemee;
        densite[i, j] = .5;
    break;
    case "foret":
        carresprite[i, j] = sforet;
        densite[i, j] = 1;
    break;
    case "foretdense":
        carresprite[i, j] = sforetdense;
        densite[i, j] = 1.5;
    case "plaine":
        carresprite[i, j] = splaine;
        densite[i, j] = .5;
    case "marais":
        if (i * 16 < room_width/2)
        {
            densite[i, j] = 1;
            terrain[i, j] = "geyser";
            carresprite[i, j] = sgeyser;
        }
        else
        {
            carresprite[i, j] = smarais;
            densite[i, j] = 1.5;
        }
    break;
    case "montagne":
        carresprite[i, j] = smontagne;
        densite[i, j] = 1.5;
    break;
    case "rocher":
        carresprite[i, j] = splainerocheuse;
        densite[i, j] = 1;
    break;
    case "clairiere":
        carresprite[i, j] = sclairiere;
        densite[i, j] = .5;
    break;
    default: show_debug_message("pas de case, pas de sprite");
    break;
}
But that's not even a problem. I see other stuff that might get buggy in the future, like this if/else that makes no sense that's an 'else'

Code:
if (j == 0 || i == room_width/size || (pourcent > 60 && (terrain[i, j-1] == "ocean" || terrain[i+1, j] == "ocean")))
{
    terrain[i, j] = "ocean";
}
else if (ypourcent <= pourcent)
{
...
Good luck!
 

Didjynn

Member
Thank you very much, you did not have to do all the work but you did so thanks again for that !

I thought I already answered but it seems I closed my browser before I sent this message... writting it again is so fun.

The switch seems much easier once I see it but I still wonder does it work for a condition like var > 10 ?
Would it be

Code:
switch (var) {
   case > 10 :
        dosomething
    case <= 10 :
        dosomething
}
Good luck for your projects too ;)
 
Last edited:

chamaeleon

Member
Thank you very much, you did not have to do all the work but you did so thanks again for that !

I thought I already answered but it seems I closed my borwer before I sent this message... writting it again is so fun.

The switch seems much easier once I see it but I still wonder does it work for a condition like var > 10 ?
Would it be

Code:
switch (var) {
   case > 10 :
        dosomething
    case <= 10 :
        dosomething
}
Good luck for your projects too ;)
That is not how switch works at all. The cases need to be constant values only (a number or an expression involving only constant values, no variables),and a simple comparison is made between the value for the case statement and what provided for switch(). There is no further computations taking place with the case parts (their values need to be computable at compile time, using constants only, not relying on any vars of any kind, macros that result in constant numbers would be I imagine). switch() handles exact matching only, and as such makes sense when you have a well-defined set of cases (the set of keyboard keys to check, 4 or 8 directional checks, anything else that you can enumerate explicitly in your game that makes sense to have different "cases" for, etc.) Other than that, there is no syntax resembling what you write. You can't write "case var > 10:", but you can write "case 11 > 10:", but then you might as well write true (or 1).
 
Top