Fangames > Programming Questions

Make object be destroyed if the player has any amount of secrets less than 8?

(1/2) > >>

l3agu32:
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!

Kyir:
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.

l3agu32:

--- Quote from: 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.

--- End quote ---
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: ---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()}
--- End code ---

Nogard:

--- Quote from: l3agu32 on July 12, 2016, 09:05:40 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: ---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()}
--- End code ---

--- End quote ---

I suggest you to start indenting your code. What happens is this:

--- Code: ---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()
              }

--- End code ---
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: ---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();
  }

--- End code ---
! is the logic negation, && the logic AND
The instance_destroy() line will get executed if any of the secretsItems is false.

l3agu32:
Thank you, Nogard, it worked. I forget that codes can have more details than I think.

Navigation

[0] Message Index

[#] Next page

Go to full version