Forum

> > CS2D > Scripts > Perks lua
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Perks lua

9 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Perks lua

Jarates
User Off Offline

Zitieren
Hi

I need a little help with my code, I am trying to add perks for players like hats, when you press a button a perk appears else if the player has already a perk then freeimage. But when a player leaves or joins my perk dissapears how to fix it? Thanks!

Mehr >

alt Re: Perks lua

Rainoth
Moderator Off Offline

Zitieren
1
2
3
4
5
6
7
function updatePerks(id)
    for i = 1,#sprite do
        freeimage(sprite[i])
    end
     sprite = {}
     freetimer()
end

You loop all present sprites and freeimage all of them. How is that removing only the needed one?
I was a bit too lazy to look into it too much but if you assign images as "sprite[id]" where id is assigned for each unique player, just go for "freeimage(sprite[id])" and you're done. Seeing as you want to free all images, you could add additional check
1
if id == 0 then
and then do what your code does, that I posted before.

alt Re: Perks lua

Jarates
User Off Offline

Zitieren
Okay as you said I added freeimage and freetimer for each perk. But it gives error

LUA ERROR: sys/lua/perks.lua:16: bad argument #1 to 'freeimage' (number expected, got nil)

Mehr >

alt Re: Perks lua

Rainoth
Moderator Off Offline

Zitieren
That's because when you're doing it for the first time, you don't have an image present and yet you're trying to 'free' it.

The code >


I added the IF check to prevent that. Here's some things that you might want to note.
• Freetimer will free everything if you write nothing inside ( ) brackets. I don't know why you're writing freetimer(id) because it needs strings to remove timers and not player IDs.
• Glow function assigns 'sprite[id]' to be an image, however, in every button call, you make 2-3 glow function calls. Meaning that you assign few images to glow. Now I'm not exactly sure but either you will be able to freeimage only the LAST image or something else might happen. If it works for you then ok I guess >.>
• I didn't add "IF" checks for other spots where you free images, so lets say you pick no image and then new round begins. It should throw an error there.

Please mind that I looked through this in a hurry so I might be wrong or I did something unncessary/incorrect.

alt Re: Perks lua

Jarates
User Off Offline

Zitieren
When I try to use a perk I get this error :

LUA ERROR: sys/lua/perks.lua:84: attempt to perform arithmetic on local 'position' (a string value)

alt Re: Perks lua

Rainoth
Moderator Off Offline

Zitieren
1
function glow(location,position
Position argument is second.
Location argument is first.
1
glow("ptop","gfx/sprites/flare2.bmp"
Location argument is second
Position argument is first.

Also, they are all strings like
1
"this is a string because it's enclosed in quote symbols"
1+1 == 2
but
"1"+1 == Error

Fix that and it'll work. The script seems like you got it from someone else and now you have no idea what each part does. I'd advise you to ask the person to fix it or if you're doing it yourself (and maybe you downloaded the original script) to pick an easier script to edit.

alt Re: Perks lua

Jarates
User Off Offline

Zitieren
I fixed it but when I press remove perk the perk wont remove.
No errors in console...

Mehr >
1× editiert, zuletzt 17.10.14 19:55:47

alt Re: Perks lua

Rainoth
Moderator Off Offline

Zitieren
I already told you why. Go read it again if you don't understand.

P.S. It won't work unless you improve how your image placing works.

alt Re: Perks lua

Jarates
User Off Offline

Zitieren
If I make something like this:

1
2
glow = image(id,"gfx/sprites/flare2.bmp",200,id,000,000,255,1,.4,.5)
			glow = image(id,"gfx/gui_load.bmp",100,id,0,0,255,1,1,2.3,2000,7)

gives error : LUA ERROR: sys/lua/perks.lua:37: bad argument #2 to 'image' (number expected, got string)

I have no idea how to assign images properly, that glow function is confusing me.

alt Re: Perks lua

Rainoth
Moderator Off Offline

Zitieren
How about you use cs2d.com then? Pretty simple huh..
cs2d lua cmd image clearly shows how the parameters are.
Here's an example of setting up an image:
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
img = {}

addhook("join","_j")
function _j(id)
	img[id] = {}
end

addhook("serveraction","_s")
function _s(id,b)
	if b == 1 then
		menu(id,"Images,1")
	elseif b == 2 or b == 3 then
		if img[id][b]~= nil then
			freeimage(img[id][b])
			img[id][b] = nil
			msg2(id,"One image was taken off")
		end
	end
end

addhook("menu","_m")
function _m(id,t,b)
	if t == "Images" then
		img[id][2] = image("gfx/sprites/flare2.bmp",0,0,200+id)
		img[id][3] = image("gfx/sprites/flare2.bmp",0,0,200+id)
	end
end

Shows how to have multiple images on one person and being able to take them off. If you want to take them all off then you can do
1
2
3
4
for i = 1,#img[id] do
	freeimage(img[id][i])
	img[id][i] = nil
end
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht