Four Times a Day [SOLVED]

Started by Atelier, Mon 29/03/2010 19:22:17

Previous topic - Next topic

Atelier

I'm coding four different times of day for my text game; morning, daytime, evening, and nightfall.

I have int timescale, which increases in value by 1 every game loop. When timsescale reaches 24000 (600 seconds), it will be reset at 0 and everything will start again (giving it the impression of a day's cycle).

I want to constantly check for the 4 boundaries. When timescale equals:

0-6000 (morning) - the morning effects will be activated
151-12000 (daytime) - the daytime effects will be activated
12040-18000 (evening) - the evening effects will be activated
18040-24000 (nightfall) - the nighttime effects will be activated

As you can see each time zone lasts roughly 150 seconds. But how do I check whether a value is in the range of something? I've played around with the >= and <= operators, but common sense told me it wouldn't work, as when it came round to night time, there would be three values which are all true! So is there a quick piece of code to check whether a variable is in a certain boundary?

Many thanks,
AtelierGames

Calin Leafshade

use a series of else ifs

Code: ags

if (t < 6000) { //morning

}
else if (t < 12000) { //afternoon

}
else if (t<18000) { // evening

}

etc etc

Atelier

Cheers Calin, much appreciated.

Khris

In general, you can use
  if (a >= 6000 && a < 12000)
to check if a value is inside a boundary.

Dualnames

Quote from: Khris on Mon 29/03/2010 22:05:19
In general, you can use
  if (a >= 6000 && a < 12000)
to check if a value is inside a boundary.

Seconded. Calin's approach shows the way, but it's not 100% correct.
if the integer you're using is 4000 all the if statements will actually be processed, unless you use a return or just keep them in boundaries. I'm not trying to be a smartass, Cal and you know it.  :D
Code: ags

if (t < 6000) { //morning
}
if (t < 12000) { //afternoon
}
if (t<18000) { // evening
}

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Ryan Timothy B

Dual, but Calin has else if's, and would work without issue.
It'll check the first one, if it's not lower than 6000, it'll check the second, and so forth.

suicidal pencil

Quote from: Dualnames on Tue 30/03/2010 02:56:27
if the integer you're using is 4000 all the if statements will actually be processed...
Code: ags

if (t < 6000) { //morning
}
if (t < 12000) { //afternoon
}
if (t<18000) { // evening
}



ok, this is pretty funny :P

you just got it reversed. The code you put will check the variable every time if t = 4000, not Cal's code :P else if is a wonderful thing :D. I'd rather Calin's code than Khris's code. Khris's will check t twice before moving into the if statement, whereas Cal's only checks once.

edit: Damn, Ryan, you beat me to the punch  

monkey0506

I know it's already been pointed out (twice), but Dualnames you're speaking complete bullocks.

If you have a series of if statements then every one will be processed. If you have a single if followed by else if(s) and/or a final else clause then they will be processed in order until one is true and the rest will be skipped.

Further, if you were to use the example you provided of t = 4000 then every if statement would result in true since 4000 is less than 6000, 12000, and 18000, which would cause the code in all three statements to be executed. Calin's example using the else clauses means that although all three if statements may be processed (for values of t >= 12000) only a maximum of one (minimum of zero) will result in true, meaning that you're not running a bunch of unneeded code.

And pencil, regarding what you said about Calin's code vs. Khris', I would say that they serve different purposes (albeit it logically the same thing). The if/else if/else route works best if you need to compare multiple possible values for the same variable (such as several different ranges). If you just want to check that a variable is within a single range (such as between 6000 and 12000) then I would say that the && route works better.

With the else if route, as soon as you reach the condition of the else if you already know that the if and all previous else ifs have returned false, so rechecking them is just redundant. But if you're just using a single if statement then you would need to use the && to check for a range.

Dualnames

#8
Anyone else with a stone?

"Number One, push off will you?"
EDIT: Also, I know where my towel is..so beware. On my behalf, I didn't notice the hidden else if's.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Calin Leafshade

Code: ags

else if (username == "Dualnames") ThrowStone();

monkey0506

I was just trying to clarify why you were wrong. ::) Oh, and also I find it funny that a post which begins:

Quote from: Calin Leafshade on Mon 29/03/2010 19:42:12use a series of else ifs

Somehow inexplicably contains:

Quote from: Dualnames on Tue 30/03/2010 03:40:49hidden else if's.

:=

Dualnames

Quote from: monkey_05_06 on Tue 30/03/2010 03:54:45
I was just trying to clarify why you were wrong. ::) Oh, and also I find it funny that a post which begins:

Quote from: Calin Leafshade on Mon 29/03/2010 19:42:12use a series of else ifs

Somehow inexplicably contains:

Quote from: Dualnames on Tue 30/03/2010 03:40:49hidden else if's.

:=

They're ninja else ifs.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

There's another loose end in this otherwise tightly knit thread, so
(;))

Quote from: suicidal pencil on Tue 30/03/2010 03:09:58I'd rather Calin's code than Khris's code. Khris's will check t twice before moving into the if statement, whereas Cal's only checks once.

I was actually considering to point out that Calin's route is best here while mine applies best to other situations; I only posted here because the OP seemed to not know about && and its colleagues.
(And I did put "In general" at the beginning.)

SMF spam blocked by CleanTalk