Forum

> > CS2D > Scripts > Can someone rate my script?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Can someone rate my script?

24 replies
Page
To the start Previous 1 2 Next To the start

old Can someone rate my script?

baRD
User Off Offline

Quote
Plz rate my script from 1-10 or reply to me what i need to improve?

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
47
48
--=======================--
--==+ Player Shop v.1 +==--
--====+ by bardecon +====--
--=======================--

--+ Variables +--
c=string.char(169)

--+ Hooks +--
addhook("say","sayShop1")
addhook("menu","sayShop2")
addhook("buy","buy")

--+ Functions +--
function sayShop1(id,txt)
	if(txt=="!Shop")then
		menu(id,"Shop,Laser|$16000,Molotov|$1000")
	end
return 1
end

function sayShop2(id,title,button)
	if(title=="Shop")then
		if(button==1)then
			if(player(id,"money")>=16000)then
			parse("equip "..id.." laser")
			parse("setmoney "..id..""..player(id,"money")-16000)
			msg2(id,c.."255255000»You have succesfully bought a Laser!«")
		else
			msg2(id,c.."255000000»You dont have enought Money!«")
			end
		end
		
		if(button==2)and(player(id,"money")>=1000)then
			if(player(id,"money")>=1000)then
			parse("equip "..id.." molotov")
			parse("setmoney "..id..""..player(id,"money")-1000)
			msg2(id,c.."255255000»You have succesfully bought a Molotov!«")
		else
			msg2(id,c.."255000000»You dont have enought Money!«")
			end
		end
	end
end

function buy()
	return 1
end
This script is not yet done...
Quote
I love lua scripting and do you have a problem with that?

old Re: Can someone rate my script?

Mora
User Off Offline

Quote
Dont upload it on file archive.
On question what you need to improve i tell you:
What's actually this lua planned to be? What do you want to see there yourself? What's your thoughts about to do with this lua after you done?

old Re: Can someone rate my script?

Dousea
User Off Offline

Quote
What kind of rating do you want? Performance, beauty?

In terms of performance, well you don't use anything "that" heavy besides several function calls and creations, and conditions, so nothing to worry about performance, except one global variable that's used for nothing other than holding character 169 (you could use
\169
inside string dammit).

Contrawise for the beauty, you need to properly tab your scripts and add necessary whitespaces between words (not symbols). It would improve human reading for you, and for the coders who want to learn. Also remember that there's no convention you need to follow, like you need to use pascal case for global vars. Here's what I call a beauty.
function Person(name)
	local new = {}
	new.name = name
	
	return setmetatable(new, {
		__index = {
			call = function(self)
				print('Hey, ' .. self.name .. '!')
			end
		}
	})
end

local person = Person 'Orlando Hutapea'

person.call()


Also there's no rating scripts with scale like 0-10. Just saying.

old Re: Can someone rate my script?

Bowlinghead
User Off Offline

Quote
Let´s pretend you have a lot of other items in the shop. And you want to change the price of 5 items or sth. You would have to change it in line 17 (menu) and in line 27 (where you loose it) for each weapon (lot of work and everyone makes mistakes)

You could zip your offers into a table:
1
2
3
4
5
myWeapons = { 
	["Laser"] = 16000,
	["Molotov"] = 1000,
	-- todo
}

And the rest is all code with no real values whatsoever.
It should look like this:
1
2
3
4
if (player(id,"money") >= myWeapons[v]) then
	parse("equip "..id.." "..myWeapons[k])
	parse("setmoney "..id.." "..player(id,"money") - myWeapons[v])
end

This makes the script way to complex and long.
But once its done its very easy to edit weapons with pricing and stuff

old Re: Can someone rate my script?

GeoB99
Moderator Off Offline

Quote
user Dousea has written
except one global variable that's used for nothing other than holding character 169 (you could use
\169
inside string dammit).

What's the matter on using
string.char()
? I indeed understand he could've declared his variable as local and that he should move the variable inside
sayShop2
function since he's using it for a specific block scope but whether you use \169 or string.char() you get the same final result.

old Re: Can someone rate my script?

baRD
User Off Offline

Quote
Hey bowling head what does this means?
1
2
3
4
if (player(id,"money") >= myWeapons[v]) then
     parse("equip "..id.." "..myWeapons[k])
     parse("setmoney "..id.." "..player(id,"money") - myWeapons[v])
end

I actualy need to know this because im actualy working in my custom player shop script
Spoiler >

Its just a menu...
Still working in the buttons...
edited 1×, last 07.07.17 02:45:40 pm

old Re: Can someone rate my script?

Dousea
User Off Offline

Quote
@user GeoB99: Yes, it would get the same result. But in terms of performance, there must be a difference between creating a variable and not creating a variable at all, right? Although it's unnoticable, it wouldn't be a good practice if you let yourself creating variables for unnecessary things (like for character 169 in this case) if there're other better ways. And... if you look closely,
string.char(169)
would do more work: accessing table; calling function; and creating a literal. While if you do the
\169
, it's already part of the string.
edited 1×, last 07.07.17 02:52:12 pm

old Re: Can someone rate my script?

GeoB99
Moderator Off Offline

Quote
@user baRD: Here's an explanation for you, piece by piece how this code works:

• 1st line: It'll create a conditional IF statement which checks if the player's money is greater than or equal the amount required to purchase a certain item. If the condition is deemed as true then it'll pass to the conditional body code (the further lines below);

• 2nd line: The regular
parse()
function to execute ordinary CS2D commands. What it does is to tell CS2D to equip you with the item you've purchased.

• 3rd line: Finally, it'll set the player's money based on purchase acquisition.

By the way,
[k]
stands for key which in our case is the item string you see in the
myWeapons
table while
[v]
stands for value which is the actual money value. Keep in mind that tables in Lua are somewhat associative arrays. You should take a look at Lua pil or other tutorials to see how tables actually work.

@user Dousea: I see, you're right. But still, it will only apply to bigger scripts where performance is a critical step, for smaller scripts it doesn't really make sense.

old Re: Can someone rate my script?

Avo
User Off Offline

Quote
Creating a variable for the color character would make sense only if it could change in the future (editing all occurrences in a big script isn't what one wants to do).

Moreover, name "c" isn't a good name for a variable. A global variable with such name (in terms of length) is a bad habbit. Short names are good for a local-scope variables (loop interating ones, like "i" in for-loops). And what is important, it says nothing about purpose of this variable.
colorSign = string.char(169)
is more clear when you see it in the middle of someone's code.

old Re: Can someone rate my script?

baRD
User Off Offline

Quote
umm i need one question too...
what does local vars do?

and i need help!!!
Spoiler >

its says
1
'<eof>' expected near 'end'

old Re: Can someone rate my script?

Bowlinghead
User Off Offline

Quote
The error means you miss an end. Every for, if, etc. needs an end.
EDIT: See line 62. You start with an "elseif" which is wrong, you always start with an if.
Solution: Delete the end on line 61
(EDIT2: My edit is complete bullshit. You TABed wrong and I didnt read carefully)

Answer to your question which user GeoB99 already answered:
Instead of equipping "laser" like you do, I equip whatever is in the table.
As I explained, I´d do this to easily edit the code later on.

Loops (for loops - you better read into it they help A LOT!!!) usually look like this:
1
2
3
4
5
6
for k,v in pairs(myWeapons) do
	-- now k and v are local variables
	-- k(ey) is like a counter, or in this case the name of the weapon
	-- v(alue) is the value inside, or in this case the price of the weapon (see my code above)
end
-- now, k and v do not exist anymore because they were local

Local variables only exist for the actual scope (or: they only exist until the 'end') like k and v above.

You are probably new to programming/lua so feel free to ignore my idea because you would have to rewrite almost everything.
Actually you asked where you can improve and I gave you an idea.

And btw, you can use elseif on line 25-51 to improve perfomance.
edited 1×, last 08.07.17 12:58:07 am

old Re: Can someone rate my script?

Dousea
User Off Offline

Quote
@user Avo: I must say that creating the variable is useless after all because what other character that could change the color of text?

@user Bowlinghead Using elseif doesn't improve performance. It just uses different logical condition.

EDIT:
Sorry, misclicked your username, user Mora.
edited 1×, last 07.07.17 05:28:24 pm

old Re: Can someone rate my script?

baRD
User Off Offline

Quote
i fixed that but this wont work!!!
Spoiler >

it wont show up the msg and wont make my money less...

old Re: Can someone rate my script?

Mora
User Off Offline

Quote
@user Dousea: k nvm
@user baRD: not that, you need to return after code(before end)
1
2
3
4
5
6
7
function open_sayShop(id,txt)
     if(txt=="!Shop")or(txt=="!shop")or(txt=="!pshop")or(txt=="!pShop")or(txt=="!playerShop")or(txt=="!playershop")or(txt=="!PlayerShop")then
          menu(id,"Player Shop,Handguns,Shotguns,SubMachineguns,Rifles,Machineguns,Grenades,Gears")

          return 1
     end
end

dont use multiple hooks please
1
2
3
4
5
6
7
function helpCommand(id,txt)
     if(txt=="!help")or(txt=="!hlp")or(txt=="!Help")then
          msg2(id,"©255255000--+| Commands |+--")
          msg2(id,"©255255000!shop or !Shop --+| To Open Shop Menu... |+--")
	return 1
     end
end
edited 1×, last 07.07.17 05:50:18 pm
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview