Skip to content

Little bit of script/function weirdness

So I decided to start fartn around with my OnPlayerChat script again. I'm adding dice rolls to it. But I ran into something which might be old behavior that I just never knew about. I'm not sure. This is the chunk specifically for the dice rolls:
//Dice rolls: else if (sSaid == "/flip a coin" || sSaid == "/flip coin" || (GetStringLeft(sSaid, 7) == "/roll d" && StringToInt(GetSubString(sSaid, 7, GetStringLength(sSaid))) != 0)) { int iDiceRoll; string sDiceRoll; string sFlip; if (sSaid == "/flip a coin" || sSaid == "/flip coin") { iDiceRoll = Random(2); if (iDiceRoll) { sFlip = " Heads!"; } else { sFlip = " Tails!"; } sDiceRoll = COLOR_GOLD+sName+" flipped a gold piece. The result was:"+sFlip+COLOR_END; } if (GetStringLeft(sSaid, 7) == "/roll d") { string sDiceParsed = GetSubString(sSaid, 7, GetStringLength(sSaid)); iDiceRoll = Random(StringToInt(sDiceParsed))+1; sDiceRoll = COLOR_GOLD+sName+" rolled a d"+sDiceParsed+". The result was:"+ IntToString(iDiceRoll)+COLOR_END; } //Output dice roll message SetPCChatMessage(sDiceRoll); return; }

The weird part is specifically the line StringToInt(GetSubString(sSaid, 7, GetStringLength(sSaid))) !=0.
In game and testing this you can type '/roll dxxx', xxx being any random number, and everything works fine. The weird part is that you can type, for example 'roll d20sdkfljd' and it will not see it as an error and will just roll a d20.

So I guess the question i have is, when you use StringToInt and there are a bunch of letters after the number does it just automatically drop all the letters after if finds a number? Seems like it should return a "0" (error) because there are a bunch of letters mixed in with it. Or is my logic a little goofy and I'm not seeing something. P.S. doing it this way so anyone can roll literally any random dice roll just because whatever.

Comments

  • highv_priesthighv_priest Member Posts: 50
    Normal string to int functionality in C drops the letters. This is because atoi() which is probably the C function nwn uses is programmed to only check for - or + signs (or lack of) and base 10 digits.
Sign In or Register to comment.