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 → Rand() error!
Rand() error!
2003-07-19, 1:57 AM #1
Hi!
The problem is that after I do something like slash=rand()*3; the if() won't register it afterwords!!! Why is that?
Here are a few examples:
Code:
slash=rand()*3;
// QUICK slash.
printint(slash);
if (slash == 0)
{
	// If after a second, we haven't attacked again, reset slash to 0.
	jkEnableSaber( player, 40, 0.15, 0.25 );
	povSlashAnimID = jkPlayPOVKey( player, povSnapAnim1, 2, 0x38);
	slashAnimID = PlayKey(player, snapAnim1, 1, 0x38);
	PlaySoundThing(swingSound01[rand()*7], player, 1.0, -1, -1, 0x80);
	shakePos = VectorScale(RandVec(),.001);
	shakeAngle = VectorScale(RandVec(),.5);
	SetPOVShake(shakePos, shakeAngle, .05, 80.0);
		
	SetFireWait(player, 0.25);
}
else if (slash == 1)
{
	// Allow another second for the 3rd attack.
	jkEnableSaber( player, 40, 0.15, 0.25 );
		
	povSlashAnimID = jkPlayPOVKey( player, povSnapAnim2, 2, 0x38 );
	slashAnimID = PlayKey(player, snapAnim2, 1, 0x38);
	PlaySoundThing(swingSound01[rand()*7], player, 1.0, -1, -1, 0x80);
		
	shakePos = VectorScale(RandVec(),.001);
	shakeAngle = VectorScale(RandVec(),.5);
	SetPOVShake(shakePos, shakeAngle, .05, 80.0);
		
	SetFireWait(player, 0.25);
}
else if ((slash == 2) || (slash==3))
{
	jkEnableSaber( player, 60, 0.2, 0.25 );
	povSlashAnimID = jkPlayPOVKey( player, povSnapAnim3, 2, 0x38 );
	slashAnimID = PlayKey(player, snapAnim3, 1, 0x38);
	PlaySoundThing(swingSound01[rand()*7], player, 1.0, -1, -1, 0x80);
		
	shakePos = VectorScale(RandVec(),.001);
	shakeAngle = VectorScale(RandVec(),.5);
	SetPOVShake(shakePos, shakeAngle, .05, 80.0);
	
	SetFireWait(player, 0.25);
}

Code:
i=(rand()*5);
printint(i);
if(i < 4)
{
	PlaySoundLocal(hubba0,1,0,0xC0);
	sleep(GetSoundLen(hubba0));
}
else if(i == 4)
{
	PlaySoundLocal(hubba1, 1, 0, 0xC0);
	PlaySoundLocal(hubba4, 1, 0, 0xC0);
	if(GetSoundLen(hubba1) > GetSoundLen(hubba4)) sleep(GetSoundLen(hubba1));
	else sleep(GetSoundLen(hubba2));
}
else if(i == 5)
{
	PlaySoundLocal(hubba2, 1, 0, 0xC0);
	PlaySoundLocal(hubba3, 1, 0, 0xC0);
	if(GetSoundLen(hubba3) > GetSoundLen(hubba2)) sleep(GetSoundLen(hubba3));
	else sleep(GetSoundLen(hubba4));
}
Return;

And also, the printint says 0!
HELP!

/Edward


[This message has been edited by Edward (edited July 19, 2003).]
Edward's Cognative Hazards
2003-07-19, 3:44 AM #2
Rand() produces a random flex between 0-1.

So when you do slash = Rand()*3; you are really getting a number like 2.293942

Cog functions will automatically truncate it, if it expects an INT and you give it a flex. That's why PrintInt is showing you a nice round integer. I dont think you can really force a type merely by defining it in symbols.

So when you attempt to do a comparison like

if(slash == 1)

its really doing a test like if(2.293942 == 1)

Try doing this:

if (slash <= 1)
{
// stuff
}
else if ((slash > 1 && slash <= 2))
{
// stuff
}
else if ((slash > 2 && slash <= 3))
{
// stuff
}

same deal for your: i if()'s

------------------
- Wisdom is 99% experience, 1% knowledge. -
- Wisdom is 99% experience, 1% knowledge. -
2003-07-19, 9:38 AM #3
He's right.

I was really hoping to have more to contribute, but he got to it first.

------------------
Brutally honest since 1998
2003-07-19, 10:08 AM #4
But if slash is an int, then it will remove all decimals. ie .1 and 0.999999... would both be 0. End of story. So if slash is int, == works. If flex or whatnot > < is the way to go. BTW the chances of getting a 1 on just rand() is about 1 in 25,000 or so...

------------------
The Sniper Missions. Current project, The Sniper Missions

The Magician Saber System.

The 2 riddle!
Major projects working on:
SATNRT, JK Pistol Mod, Aliens TC, Firearms

Completed
Judgement Day (HLP), My level pack
2003-07-19, 10:27 AM #5
OK! That helped!
Although, this COG could use some work.
Code:
# All kinds of music!
# Hubba Hubba Zoot Zoot by Caramba
# Rayman's Music World
#
# By Edward
symbols

message		startup
message		activated

sound		hubba0
sound		hubba1
sound		hubba2
sound		hubba3

surface		switch

surface		name

sound		rayman1

int		i	local
int		s=0	local

end
#
code
startup:
	sleep(1);
	PlaySoundLocal(hubba0,1,0,0x0);
	sleep(GetSoundLen(hubba0));
	PlaySoundLocal(hubba1,1,0,0x0);
	sleep(GetSoundLen(hubba1));
	PlaySoundLocal(hubba2,1,0,0x0);
	sleep(GetSoundLen(hubba2));
	PlaySoundLocal(hubba3,1,0,0x0);
	sleep(GetSoundLen(hubba3));
	while(1)
	{
		If(s==0)
		{
			i=(rand()*5);
			printint(i);
			if(i < 4)
			{
				PlaySoundLocal(hubba0,1,0,0xC0);
				sleep(GetSoundLen(hubba0));
			}
			else if(i >= 4 && i<5)
			{
				PlaySoundLocal(hubba1, 1, 0, 0xC0);
				PlaySoundLocal(hubba4, 1, 0, 0xC0);
				if(GetSoundLen(hubba1) > GetSoundLen(hubba4)) sleep(GetSoundLen(hubba1));
				else sleep(GetSoundLen(hubba4));
			}
			else if(i >= 5 && i<6)
			{
				PlaySoundLocal(hubba2, 1, 0, 0xC0);
				PlaySoundLocal(hubba3, 1, 0, 0xC0);
				if(GetSoundLen(hubba3) > GetSoundLen(hubba2)) sleep(GetSoundLen(hubba3));
				else sleep(GetSoundLen(hubba2));
			}
			Return;
		}
		Else if(s==1)
		{
			PlaySoundLocal(rayman1,1,0,0xC0);
			sleep(GetSoundLen(rayman1)+1);
		}
		SetWallCel(name,s);
	}
	return;

activated:
	If(GetSenderRef()!=switch) return;
	If(s==0)
	{
		s=1;
	}
	Else if(s==1)
	{
		s=0;
	}
return;
end

After it goes through the first 4 pieces of a chopped up song, it takes one random sound and then never loops! Doesn't return to the rand() command! Help please...

/Edward
Edward's Cognative Hazards
2003-07-19, 7:57 PM #6
Quote:
<font face="Verdana, Arial" size="2"> I dont think you can really force a type merely by defining it in symbols.</font>


It really doesn't make a difference what type a variable has. JK will change a variable's type on assignment. Because JK does this, it's really impossible to tell whether JK treats ints and flexes any differently.

Doesn't really apply to the example, but the not equal to (!=) operator does not use the decimal portion of a number. So (0.1 != 0) returns false.

Quote:
<font face="Verdana, Arial" size="2">So if slash is int, == works. If flex or whatnot > < is the way to go.</font>


Since intvar=0.1; will make intvar a flex, that wont work.

------------------
Author of the JK DataMaster, Parsec, Scribe, and the EditPlus Cog Files.
Author of the JK DataMaster, Parsec, Scribe, and the EditPlus Cog Files.

↑ Up to the top!