Script finesse - How to test for Modulus (%)
#1
Posté 09 mai 2011 - 09:56
In the end the test will only fire if the test vs %2 is TRUE.
Thanks
#2
Posté 09 mai 2011 - 10:10
if(nRoll <= 2) // If the roll is equal to 2 or less
{
// Do Stuff
}
#3
Posté 09 mai 2011 - 10:14
#4
Posté 09 mai 2011 - 10:20
int nRand = Random(7); // Choose a random number between 0 and 7
if((nRand == 0) || (nRand == 2) || (nRand == 4) || (nRand == 6))
{
// Do Stuff if roll is 0 or an even number
}
else // It is not 0, it is an odd number
{
// Do something else
}
Modifié par _Knightmare_, 09 mai 2011 - 10:21 .
#5
Posté 09 mai 2011 - 10:28
Morbane wrote...
I would like to script a test vs a random number but with the added modifier of being divisible by 2 (%2). Is there a way to script with some sort of algorithm for that?
In the end the test will only fire if the test vs %2 is TRUE.
Thanks
I don't know what the remainder operator is for nwscript, but there is one in C. You would test if the remainder when divided by 2 equals 0.
#6
Posté 09 mai 2011 - 10:40
string sResult;
// determine if number is odd or even using modulus
if (nNumber % 2)
{
sResult = "Odd";
}
else
{
sResult = "Even";
}- from nwn.wikia.com
#7
Posté 09 mai 2011 - 10:45
int iX = Random(7)+1;
float fX = IntToFloat(iX);
float fX2 = fX / 2;
if (fX2 == abs(fX2)// number is even
[do something]
else // number is odd
[do something else]
Modifié par DannJ, 09 mai 2011 - 11:56 .
#8
Posté 09 mai 2011 - 10:47
// return true if number is even (note: 0 is considered even by this function)
int IsEven (int nValue)
{
return (!(nValue%2));
}
If you need a conversation script that does the check:
// gc_bb_is_even
// return true if number is even (note: 0 is considered even by this function)
int StartingConditional(int nValue)
{
return (!(nValue%2));
}
This return line will also work:
return ( nValue / 2 == (1 + nValue) / 2 );
Modifié par Kaldor Silverwand, 10 mai 2011 - 02:22 .
#9
Posté 10 mai 2011 - 03:12
sneaky sneaky ..Kaldor Silverwand wrote...
This return line will also work:
return ( nValue / 2 == (1 + nValue) / 2 );
#10
Posté 10 mai 2011 - 07:11
Just the same I like the options you guys posted - I will test them and see what happens.
Thanks Kaldor and Dan
Modifié par Morbane, 10 mai 2011 - 07:12 .
#11
Posté 10 mai 2011 - 07:35
www.nwnlexicon.com/compiled/primer.arithmaticandstringoperators.html
Modifié par GhostOfGod, 10 mai 2011 - 07:35 .
#12
Posté 10 mai 2011 - 07:39
// test_modulo
void main(int iInteger)
{
object oPC = GetFirstPC();
string sResult;
// determine if number is odd or even using modulus
if (iInteger %2) sResult = "Odd";
else sResult = "Even";
SendMessageToPC(oPC, sResult);
}came up with the right answers inGame, too.
#13
Posté 10 mai 2011 - 07:57
I must go back and try it again, Mayhap I got an associated error that the error checker just flagged as the %?
#14
Posté 10 mai 2011 - 08:56
Essentially it spawns a kuoa-toa every other heartbeat - I didnt want them swarming out of their pool but to pile up slowly.void main()
{
if(!GetGlobalInt("kuo_trig")) return;
location lLoc = GetLocation(GetObjectByTag("ip_kuo_spawn"));
if(GetGlobalInt("kuo_count") >= 7) return;
int nVar = GetGlobalInt("kuo_count");
if(!nVar || nVar%2)
{
CreateObject(OBJECT_TYPE_CREATURE, "c_kuoa_toa", lLoc);
SetGlobalInt("kuo_count", nVar+1);
}
else
SetGlobalInt("kuo_count", nVar+1);
}
So - yippie! I works now.
Modifié par Morbane, 10 mai 2011 - 09:01 .
#15
Posté 12 mai 2011 - 07:47
Morbane wrote...
I tried to use the "%" to test for modulo 2 or (%2) - the error was trapped at the "%" - I forget the exact message. So I am wondering, is it supposed to work in NWScript? Or not? Because I have recently installed Skywing's Advanced Script Compiler http://social.biowar...4/index/7324714 - so if that is causing me grief I might have to just not do the modulo operation because I like the ASC. I was just trying to be fancy.
Just the same I like the options you guys posted - I will test them and see what happens.
Thanks Kaldor and Dan
Yes, the modulus operator % is supported in NWScript. Were you able to get this working? The operator should be recognized in the compiler plugin, as with the standard script compiler.
#16
Posté 12 mai 2011 - 08:20
SkywingvL wrote...
Morbane wrote...
I tried to use the "%" to test for modulo 2 or (%2) - the error was trapped at the "%" - I forget the exact message. So I am wondering, is it supposed to work in NWScript? Or not? Because I have recently installed Skywing's Advanced Script Compiler http://social.biowar...4/index/7324714 - so if that is causing me grief I might have to just not do the modulo operation because I like the ASC. I was just trying to be fancy.
Just the same I like the options you guys posted - I will test them and see what happens.
Thanks Kaldor and Dan
Yes, the modulus operator % is supported in NWScript. Were you able to get this working? The operator should be recognized in the compiler plugin, as with the standard script compiler.
All is well - I was getting an error but the flag must have been something different not the % operator. I posted the script above of the working code.
#17
Posté 12 mai 2011 - 11:17
Of course, modulus does work just fine (I've used it myself, in the past) so there's no need, but it seems like you were overcomplicating things... (something I'm very familiar with myself).
#18
Posté 12 mai 2011 - 11:18
#19
Posté 13 mai 2011 - 02:43
#20
Posté 13 mai 2011 - 07:55
#21
Posté 13 mai 2011 - 11:52
int nSwitch = GetLocalInt(OBJECT_SELF, "<VarnameHere>");
if(nSwitch)
{
//Do stuff
}
SetLocalInt(OBJECT_SELF, "<VarnameHere>", !nSwitch);Modulus works just fine, though.





Retour en haut







