I Wanna Community
Fangames => Game Design => Programming Questions => Topic started 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!
-
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.
-
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".
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()}
-
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".
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:
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:
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.
-
Thank you, Nogard, it worked. I forget that codes can have more details than I think.
-
Or you could use a for statement. Because if ANY of the values is false the object gets destroyed, this is quite simple:
for(i = 0;i<8;i++){
if global.secretItem[i] == false {
instance_destroy();
}
}