Author Topic: Script causes a buffer overrun  (Read 1530 times)

I'm writing a water wave script, and I need to use a round function for it. The documentation refers to an mRound(); function, but when I try and use it in a script it's not found. Is it because I'm not including the math library? If so, how do I?

Nevermind, I just used mFloor(); instead. Unfortunately, now my script instantly crashes the game :(
Anyone mind helping me figure out why? The script does use an infinite loop, but it does so with a schedule of 100 and it should only have one scheduled at a time.

Code: [Select]
// Water waves, by TristanBomb.
$wavesFreq = 1;
$wavesAmp = 1;
$wavesBase = 0;
$wavesEnabled = false;

function servercmdwaves(%client,%func,%val)
{
if(%client.isadmin)
{
switch$(%func)
{
case "freq":
$wavesFreq = (mFloor(%val * 10)/10);
case "amp":
$wavesAmp = %val;
case "base":
if(%val $= "") $wavesBase = $EnvGuiServer::WaterHeight;
else $wavesBase = %val;
case "set":
if($wavesEnabled == false)
{
$wavesEnabled = true;
wavecycle();
}
else $wavesEnabled = false;

default:
messageclient(%client,'',"\c2Usage:");
messageclient(%client,'',"\c3  /waves freq #\c6: Sets the wave period to #, in seconds.");
messageclient(%client,'',"\c3  /waves amp #\c6: Sets the wave amplitude to #.");
messageclient(%client,'',"\c3  /waves base #\c6: Sets the normal water height to #.");
messageclient(%client,'',"\c3  /waves base\c6: Sets the normal water height to the current height.");
messageclient(%client,'',"\c3  /waves set\c6: Enables or disables waves.");
}
}
else
{
messageclient(%client,'',"<color:FF0000>You must be an admin to use this command.");
}
}

function wavecycle()
{
serverCmdEnvGui_SetVar(%client, WaterHeight, ($wavesAmp*mSin(%t/($wavesFreq*$pi*2))+$wavesBase));
%t += 0.1;
if(%t >= $wavesFreq) %t = 0;
if($wavesEnabled = true) schedule(100, wavecycle(), 0);
}

//serverCmdEnvGui_SetVar(%client, WaterHeight, %set);
« Last Edit: December 02, 2014, 07:08:15 PM by TristanLuigi »

The documentation you linked is for Torque3D, which is a different version than what BL uses, which is why it's not found
There's no such thing as "including libraries" in Torque (except for player made resources), everything is just there
A better alternative to rounding (mFloor will always round down, mFloor(1.9999999) will return 1, which often times isn't ideal) would be mFloatLength(%num,0)


The problem is your schedule call at the end of wavecycle
It should be schedule(100,0,wavecycle);
The reason your way crashes is because it's trying to get the return value of wavecycle() to put in the second argument, which just calls wave cycle again, etc
Infinite recursion
« Last Edit: December 02, 2014, 07:18:59 PM by Headcrab Zombie »

if($wavesEnabled = true) schedule(100, wavecycle(), 0);
You didn't do the schedule right.

When calling the regular function (not on an object, such as %obj.schedule(MS,function,args[,]),
It's actually called schedule(MS,0 (this is usually called on an object, but I don't understand, so it's usually set to 0), func, args[,]);
When calling schedules, you don't need the () for the function you use.

example: schedule(3000, 0, talk, "Hello!");

if($wavesEnabled = true) schedule(100, wavecycle(), 0);
You didn't do the schedule right.

When calling the regular function (not on an object, such as %obj.schedule(MS,function,args[,]),
It's actually called schedule(MS,0 (this is usually called on an object, but I don't understand, so it's usually set to 0), func, args[,]);
When calling schedules, you don't need the () for the function you use.

example: schedule(3000, 0, talk, "Hello!");
Thanks! That fixed it, and thanks for the mFloatLength thing.
It still doesn't work, but at least it doesn't crash. I'll try and figure it out first and I'll post back if I can't.

You didn't define what %client is in the waterCycle function.

Also, in the function, you are setting the variable in if($wavesEnabled = true), if you want it to check something, you would do something like these:
$= : Checking if the string/number exactly matches the other string [Exact opposite as !$=, which checks to make sure it does not exactly match the other string/number]
!= : Checking if the number does not match the other number, same for objects
== : Checking if the number matches the other number, same for objects
>= : Checking if the number is larger than or equal to than the other
<= : Checking if the number is smaller than or equal to than the other
< : Checking if the number is smaller than the other
> : Checking if the number is larger than the other
« Last Edit: December 02, 2014, 07:35:13 PM by Advanced Bot »

>= : Checking if the number is larger than the other
<= : Checking if the number is smaller than the other
larger than or equal to, smaller than or equal to*

Just larger or smaller is > or <

larger than or equal to, smaller than or equal to*

Just larger or smaller is > or <
Whoops, lol

You didn't define what %client is in the waterCycle function.

Also, in the function, you are setting the variable in if($wavesEnabled = true), if you want it to check something, you would do something like these:
-snip-
Yeah, I ended up catching both of those mistakes.
Can't always rely on other people for this stuff. I'm going to package this and submit it to Add-Ons.
Thanks so much for your help!

mFloatLength(%number, 0) can be used to round a number properly, by the way.