I Wanna Community

Fangames => Game Design => Programming Questions => Topic started by: l3agu32 on July 12, 2016, 12:51:35 AM

Title: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: l3agu32 on July 12, 2016, 12:51:35 AM
I made an object be destroyed when I have all 8 secrets. But when I try to make an object be destroyed when I have any amount of secrets less than 8, it is destroyed only when the amount of secrets is 7. Thank you!
Title: Re: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: Kyir on July 12, 2016, 01:37:21 AM
Since you haven't provided any code, I can only speculate that the problems is being caused by the indexes used for most secrets starting at 0, and your count starting at 1. You should really provide code when asking a question like this though.
Title: Re: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: l3agu32 on July 12, 2016, 09:05:40 AM
Since you haven't provided any code, I can only speculate that the problems is being caused by the indexes used for most secrets starting at 0, and your count starting at 1. You should really provide code when asking a question like this though.
This is the code I tried, and as I said I want it to make the object be destroyed if the amount of true secrets is less than 8, but the object is being destroyed only when the amount is exactly 7 true secrets. I didn't find some way to count variables like we can count instances using "instance_number".
Code: [Select]
if global.secretItem[1] = true
if global.secretItem[2] = true
if global.secretItem[3] = true
if global.secretItem[4] = true
if global.secretItem[5] = true
if global.secretItem[6] = true
if global.secretItem[7] = true
if global.secretItem[8] = true{
//nothing happens
}
else{
instance_destroy()}
Title: Re: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: Nogard on July 12, 2016, 09:25:54 AM
This is the code I tried, and as I said I want it to make the object be destroyed if the amount of true secrets is less than 8, but the object is being destroyed only when the amount is exactly 7 true secrets. I didn't find some way to count variables like we can count instances using "instance_number".
Code: [Select]
if global.secretItem[1] = true
if global.secretItem[2] = true
if global.secretItem[3] = true
if global.secretItem[4] = true
if global.secretItem[5] = true
if global.secretItem[6] = true
if global.secretItem[7] = true
if global.secretItem[8] = true{
//nothing happens
}
else{
instance_destroy()}

I suggest you to start indenting your code. What happens is this:
Code: [Select]
if global.secretItem[1] = true
  if global.secretItem[2] = true
    if global.secretItem[3] = true
      if global.secretItem[4] = true
        if global.secretItem[5] = true
          if global.secretItem[6] = true
            if global.secretItem[7] = true
              if global.secretItem[8] = true
               {
                  //nothing happens
               }
              else
              {
                 instance_destroy()
              }
The else statements affects only the last if, so the code gets executed if and only if the first seven secrets are true and the last one is false.

You could use this to simplify things:
Code: [Select]
if(!(global.secretItem[1] == true &&
     global.secretItem[2] == true &&
     global.secretItem[3] == true &&
     global.secretItem[4] == true &&
     global.secretItem[5] == true &&
     global.secretItem[6] == true &&
     global.secretItem[7] == true &&
     global.secretItem[8] == true))
  {
    instance_destroy();
  }
! is the logic negation, && the logic AND
The instance_destroy() line will get executed if any of the secretsItems is false.
Title: Re: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: l3agu32 on July 12, 2016, 09:36:10 AM
Thank you, Nogard, it worked. I forget that codes can have more details than I think.
Title: Re: Make object be destroyed if the player has any amount of secrets less than 8?
Post by: L4Vo5 on July 12, 2016, 09:46:38 PM
Or you could use a for statement. Because if ANY of the values is false the object gets destroyed, this is quite simple:

Code: [Select]
for(i = 0;i<8;i++){
      if global.secretItem[i] == false {
         instance_destroy();
      }
}