UGX-Mods

Call of Duty 5: World at War => Downloadable Items for Mappers => Custom Maps, Mods & Tools => Scripts => Topic started by: daedra descent on October 02, 2015, 10:32:22 pm

Title: [FUNCTION] roundOff()
Post by: daedra descent on October 02, 2015, 10:32:22 pm
Code Snippet
Plaintext
/*
--SCRIPT DOC BEGIN--

Purpose: to round a uneven integer(int) to a even number based off of the given mod(mod) and using the provided middle integer(mid) to determine if the integer should be incremented positively or negatively.

Variables:
-int = Any number.
-mod = the modulus number to use.
-mid = the value to compare to determine if the function should add or subtract.

Example Usage:
iprintln(roundOff(375, 20, 10));

Example Usage Result:
360

--SCRIPT DOC END--
*/

roundOff(int, mod, mid)
{
if(!isDefined(int) || !isDefined(mod) || !isDefined(mid))
return undefined;

while(int % mod != 0)
{
if(int % mod >= mid)
int++;
else
int--;
}
return int;
}
Credit me if used.
Title: Re: [FUNCTION] roundOff()
Post by: alaurenc9 on October 03, 2015, 12:33:04 am
cool. very simple, with script documentation just like treyarch's utility functions. can you teach me what the % symbol does again?
Title: Re: [FUNCTION] roundOff()
Post by: daedra descent on October 03, 2015, 12:40:58 am
cool. very simple, with script documentation just like treyarch's utility functions. can you teach me what the % symbol does again?

Modulus? Its the remainder when dividing.

Code Snippet
Plaintext
13 %  10 = 3
Title: Re: [FUNCTION] roundOff()
Post by: Gabrielle on October 03, 2015, 12:42:02 am
cool. very simple, with script documentation just like treyarch's utility functions. can you teach me what the % symbol does again?

It returns the modulo of the division. So for example:

10 % 2 returns 0 (as 10/2=5, not a double/float but an integer)
9 % 2 returns 1 (as 9/2 returns a double/float, 8/2 doesn't. 9-8 = 1, so 1 is the answer)

19 % 4 returns 3 (as 19/4 returns a double/float, 16/4 doens't. 19-16=3, so 3 is the answer)

Bad explaination but it works this way. It'll check how much times the number after the % can be in the number before the %, and it'll return the rest (the number before the % minus the number of the last time the number after % could be inserted)
Title: Re: [FUNCTION] roundOff()
Post by: alaurenc9 on October 03, 2015, 12:45:41 am
Modulus? Its the remainder when dividing.

Code Snippet
Plaintext
13 %  10 = 3

OOOOOOOOOOOOOO that makes sense. thank you for telling me this.

It returns the modulo of the division. So for example:

10 % 2 returns 0 (as 10/2=5, not a double/float but an integer)
9 % 2 returns 1 (as 9/2 returns a double/float, 8/2 doesn't. 9-8 = 1, so 1 is the answer)

19 % 4 returns 3 (as 19/4 returns a double/float, 16/4 doens't. 19-16=3, so 3 is the answer)

Bad explaination but it works this way. It'll check how much times the number after the % can be in the number before the %, and it'll return the rest (the number before the % minus the number of the last time the number after % could be inserted)

yeah i get it. not really that bad of an explanation. thanks guys for teaching me this, ive been wondering for so long....
Title: Re: [FUNCTION] roundOff()
Post by: DeletedUser on October 03, 2015, 12:49:20 am
Modulus? Its the remainder when dividing.

Code Snippet
Plaintext
13 %  10 = 3
ive always thought that modulus (%) wasnt in GSC well now i know it is :D
Title: Re: [FUNCTION] roundOff()
Post by: DidUknowiPwn on October 04, 2015, 04:30:32 am
Maybe should just cast the int as an actual int.

int = int(int);
10/10 variable names by the way.

(Just in case someone passes a double.)
Title: Re: [FUNCTION] roundOff()
Post by: JBird632 on October 04, 2015, 04:34:14 am
10/10 variable names by the way.
Haha ya, nothing like using keywords as your own variables.
Title: Re: [FUNCTION] roundOff()
Post by: daedra descent on October 04, 2015, 05:33:09 am
Maybe should just cast the int as an actual int.

int = int(int);

I personally don't think its necessary to check for all input cases just so the function would work or return if the person couldn't be bothered to check the function args.

Quote
10/10 variable names by the way.

(Just in case someone passes a double.)

Well, it is meant to round off integers.

Haha ya, nothing like using keywords as your own variables.

To my knowledge, int is not a keyword in GSC. The reason its highlighted in the code box is probably because its set to highlight C language keywords, which GSC is based off of.
Title: Re: [FUNCTION] roundOff()
Post by: JBird632 on October 04, 2015, 05:36:13 am
To my knowledge, int is not a keyword in GSC. The reason its highlighted in the code box is probably because its set to highlight C language keywords, which GSC is based off of.
You would be correct, but it is still bad practice to do so.
Title: Re: [FUNCTION] roundOff()
Post by: daedra descent on October 04, 2015, 06:00:56 am
You would be correct, but it is still bad practice to do so.

First off, what is consider 'bad practice'(aka Coding conventions) depends entirely on the language. GSC != C(or other languages) and therefor may not abide by the same 'practices'. Especially since GSC is so straightforward and doesn't even need you to declare what a variable type actually is before using it, which most languages require.

Secondly, with Leviathan released in the state that it was, like if conditions using undefined variables, who are you to question what is or isn't 'good practice'? And don't even try to say running code on undefined variables is OK in any shape or form. World at War in developer mode practically screams 'YOU SHOULDN'T BE RUNNING CODE ON UNDEFINED VARIABLES' every other developer error on Leviathan.
Title: Re: [FUNCTION] roundOff()
Post by: JBird632 on October 04, 2015, 06:05:33 am
Woah dude, why are you attacking Leviathan? Why would you run it in developer mode in the first place? I couldn't care less what happens in developer mode as long as it runs fine for everyone - besides Awesome Pieman did the majority of scripting for Leviathan and he doesn't know proper scripting practices since he is self taught.

I would also like to counter your argument with why would Treyarch allow you to use undefined variables in the first place (ie the isDefined function, which is quite handy) without them actually working.
Title: Re: [FUNCTION] roundOff()
Post by: daedra descent on October 04, 2015, 06:35:22 am
Woah dude, why are you attacking Leviathan?

Because its a good example(or the scripts it uses rather).

Quote
I couldn't care less what happens in developer mode as long as it runs fine for everyone

My car makes a bunch of funny noises but it runs!

Alright that was a bit dickheadish, but really just because you are given the option to ignore script screwups doesn't mean you should. While many of World at War's developer errors might seem trivial, they do matter. For instance, if for whatever reason, a function isn't able to delete the zombie's glowing eye model thing because the way the code tried to get the model returned undefined, developer WILL point that out.

Quote
e - besides Awesome Pieman did the majority of scripting for Leviathan and he doesn't know proper scripting practices since he is self taught.

So was i. When i started out i didn't even know what a integer, boolean, double, etc was let alone running code on specific players but eventually i learned about developer which made debugging almost painless(in SP anyway).

Quote
I would also like to counter your argument with why would Treyarch allow you to use undefined variables in the first place (ie the isDefined function, which is quite handy) without them actually working.

If i had to guess it was due to keeping the language as simple and straightforward as possible.
Title: Re: [FUNCTION] roundOff()
Post by: Scobalula on October 04, 2015, 07:22:02 am
Only recently found out about modulus in JS while learning it, didn't know you could use it in GSC, cool none the less, I know I some people will have good use for this.