Forum

> > CS2D > Scripts > Value organization & rounding
Forums overviewCS2D overview Scripts overviewLog in to reply

English Value organization & rounding

8 replies
To the start Previous 1 Next To the start

old closed Value organization & rounding

Powermonger
User Off Offline

Quote
I have 2 questions for you guys.


1. Is there a way to organize values?

Example:

ID#1 score = pl1
ID#2 score = pl2
ID#3 score = pl3

pl1 = 49
pl2 = 65
pl3 = 52

I would like to have variables "pl1", "pl2" and "pl3" to order from the highest to the lowest, and then I'd like to parse setpos to players, like this:

1st place ==> parse("setpos place1 50 50")
2nd place ==> parse("setpos place2 50 70")
3rd place ==> parse("setpos place3 50 90")


2. And is there a way round a calculated value?

Example, calculating KpD:

kpdkills = player(variable1,"score")
kpddeaths = player(variable1,"deaths")
kpdmeter = (kpdkills)/((kpddeaths)+1)

parse('hudtxt2 '..p..' 7 "©255255128KpD: '..kpdmeter..'" 75 170 1')

Hudtext above gives sometimes (mostly) gives a number with lots of decimals. Can it be rounded to show only 2 or 3 decimals?

Like 3,1415926535 ==> 3,14
Oh and I don't mind if it's not rounded properly (5 up, 4 down), I just want it shown shorter.


Thank you for your attention

old Re: Value organization & rounding

Apache uwu
User Off Offline

Quote
1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
places={
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90},
	{50,50},
	{50,70},
	{50,90}
}

addhook("endround","_endround")

function _endround()
	players=player(0,"table")
	table.sort(players,
		function(a,b)
			return player(a,"score")>player(b,"score") 
		end
	)
	for place,id in pairs(players) do
		parse("setpos "..id.." "..table.concat(places[place]," "))
	end
end

At the end of the round, the player with the highest score teleports to x:50|y:50, second place goes to the second item in the places table.

2.

1
2
3
4
5
6
7
8
9
function round(val,decimal)
	if decimal then
		return math.floor((val * 10^decimal)+0.5)/(10^decimal)
	else
		return math.floor(val+0.5)
	end
end

print(round(3.1415,1))

Should print 3.1, the syntax of round is:

round( your number, to how many decimal places )

old Re: Value organization & rounding

Powermonger
User Off Offline

Quote
Tested the first one shortly, it worked fine.
Thanks!

About the second:

Where do I put the round function?

I have a ms100 hook to show players KpD.
If I put into it or make another hook
==> "attempt to perform arithmetic on a string value"
or "attempt to perform arithmetic on local 'val' (a nill value)"

This is shown ms100, and I want the '..kpdmeter..' rounded.

parse('hudtxt2 '..p..' 7 "©255255128KpD: '..kpdmeter..'" 75 170 1')

So what do I do?

old Re: Value organization & rounding

Apache uwu
User Off Offline

Quote
Make sure you convert 'kpdmeter' into a number.

kpdmeter=tonumber(kpdmeter)
kpdmeter=round(kpdmeter,1)
parse('hudtxt2 '..p..' 7 "©255255128KpD: '..kpdmeter..'" 75 170 1')


Edit: A better way would be to write the function to automatically do that.

1
2
3
4
5
6
7
8
function round(val,decimal)
	val=tonumber(val)
	if decimal then
		return math.floor((val*10^decimal)+0.5)/(10^decimal)
	else
		return math.floor(val+0.5)
	end
end
edited 1×, last 27.06.12 03:24:33 am

old Re: Value organization & rounding

Powermonger
User Off Offline

Quote
I made a hook addhook("startround","round"),
but I always get the same message:

"attempt to perform arithmetic on local 'val' (a nill value)"

I understood that these belong to ms100 hook,

1
2
3
kpdmeter=tonumber(kpdmeter)
kpdmeter=round(kpdmeter,1)
parse('hudtxt2 '..p..' 7 "©255255128KpD: '..kpdmeter..'" 75 170 1')

but what about round function?

I made it to look like this:

1
2
3
4
5
6
7
8
9
addhook("startround","round")
function round(val,decimal)
val=tonumber(val)
    if decimal then
        return math.floor((val*10^decimal)+0.5)/(10^decimal)
    else
        return math.floor(val+0.5)
    end
end

Is that right?
If yes, could it be that I am missing "val" somewhere in the lua? Anywhere else I need to insert it?

I always get lua error for not determing the "val".
==> "attempt to perform arithmetic on local 'val' (a nill value)"

ps. I love scripting, but I ain't very good at it. I am a beginner

old Re: Value organization & rounding

Alistaire
User Off Offline

Quote
user Powermonger has written
I made a hook addhook("startround","round"),
but I always get the same message:

"attempt to perform arithmetic on local 'val' (a nill value)"

I understood that these belong to ms100 hook,

1
2
3
kpdmeter=tonumber(kpdmeter)
kpdmeter=round(kpdmeter,1)
parse('hudtxt2 '..p..' 7 "©255255128KpD: '..kpdmeter..'" 75 170 1')

but what about round function?

I made it to look like this:

1
2
3
4
5
6
7
8
9
addhook("startround","round")
function round(val,decimal)
val=tonumber(val)
    if decimal then
        return math.floor((val*10^decimal)+0.5)/(10^decimal)
    else
        return math.floor(val+0.5)
    end
end

Is that right?
If yes, could it be that I am missing "val" somewhere in the lua? Anywhere else I need to insert it?

I always get lua error for not determing the "val".
==> "attempt to perform arithmetic on local 'val' (a nill value)"

ps. I love scripting, but I ain't very good at it. I am a beginner


You are the worst scripter I've seen in my entire life. startround is a hook. Round is a standard Lua function. Please, PLEASE understand that it's just all wrong.

1
2
3
4
5
6
7
8
9
10
function round(val,decimal)
val=tonumber(val)
    if decimal then
        return math.floor((val*10^decimal)+0.5)/(10^decimal)
    else
        return math.floor(val+0.5)
    end
end

round(value you want, how many decimals you want) --rounds a number to the ammount of decimals you want. YOU DON'T NEED A HOOK FOR A NORMAL FUCKING FUNCTION.

old Re: Value organization & rounding

Apache uwu
User Off Offline

Quote
Everything is as is. Please copy and paste

1
2
3
4
5
6
7
8
function round(val,decimal)
	val=tonumber(val)
	if decimal then
		return math.floor((val*10^decimal)+0.5)/(10^decimal)
	else
		return math.floor(val+0.5)
	end
end

Somewhere above your ms100 hook for good organization.

old Re: Value organization & rounding

Powermonger
User Off Offline

Quote
Thank you both , you guys made it again

Quote
You are the worst scripter I've seen in my entire life. startround is a hook. Round is a standard Lua function. Please, PLEASE understand that it's just all wrong.

Alistaire you don't have to be so mean : /

It works now, so this thread can be closed now.
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview