Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Doing something randomly or every other...
Doing something randomly or every other...
2005-07-26, 7:36 AM #1
Hey,

In cog, how do I do something:

1) Randomly
2) Every other time
3) Every two times
4) Every three times
5) And so on...

For example, I have a cog that does something when you activate it, and every other time you activate it, it does something extra.

[EDIT] I just realized this could probally be done with a toggle. Is that the best way to do it?

Thanks!

ReT
2005-07-26, 7:53 AM #2
Have an int variable (e.g. switchvar) that is initially set to 0. Then, add the following lines to your activated message:
Code:
if(switchvar % 2)
{
 // Insert your stuff here
}
switchvar = switchvar + 1;

This does your stuff every other time you activate the cog. If you want to have it done every three, four, whatever times, you have to adjust the if condition (e.g. switchvar % 3, etc.). You can also determine if it's done the first time, and not the second, or vice versa, by appropriately initializing the variable. Here (switchvar = 0), it's not done the first time. I know, clumsy description, sue me.

Note that this method can lead to a variable value overflow. It shouldn't be used for pulses or other messages that get processed a lot of times. Activated is fine, though.

[Edit] Yeah, it can also be done using a toggle with some if clauses. But it takes up more lines (there's no overflow problem, though).
"Häb Pfrässe, süsch chlepfts!" - The coolest language in the world (besides Cherokee)
2005-07-26, 8:38 AM #3
Very cool. So I would do something like this...?
Code:
activated:
Print("Something that happens every time");
if(switchvar % 2)
   {
   Print("Something that happens every other time");
   }
switchvar = switchvar + 1;
Return;

And I'm gonna int switchvar as 0 in symbols, right?

What happens in a variable value overflow?

Thank you very much!

ReT
2005-07-26, 10:42 AM #4
Yeah, that should work.
If you constantly increase the value a variable should hold, you will eventually reach the maximum value, and if you go beyond it, bad things can happen. It's not very dangerous, usually, because in most cases, it's value will just start over at the minimum value (e.g. if max was 127 and you increased it, the new value held by the variable would be -128 or something). To have the same functionality without the danger of overflow, just do:
Code:
// Using this method, switchvar has to be initialized with 1
if(switchvar == x)
{
 // Will be processed every x times
}
switchvar = switchvar + 1;
if(switchvar > x)
 switchvar = switchvar - x;

It's just...I love the modulo operator. Sorry about it. Use the new code.

Two answer 1) (missed that, sorry): Rand() generates a random number between 0 and 1. You can multiply this result in a clever way to simulate any chance you want.
"Häb Pfrässe, süsch chlepfts!" - The coolest language in the world (besides Cherokee)
2005-07-26, 2:01 PM #5
Thank you very much, I'm gonna try this when I get a chance.

ReT

↑ Up to the top!