English Perks lua

9 replies
Goto Page
To the start Previous 1 Next To the start
15.10.14 09:36:58 pm
Up
Jarates
User
Offline Off
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!

More >
15.10.14 10:35:36 pm
Up
Rainoth
Moderator
Offline Off
Code:
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
Code:
1
if id == 0 then
and then do what your code does, that I posted before.
17.10.14 01:22:42 pm
Up
Jarates
User
Offline Off
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)

More >
17.10.14 03:28:45 pm
Up
Rainoth
Moderator
Offline Off
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.
17.10.14 06:21:52 pm
Up
Jarates
User
Offline Off
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)
17.10.14 07:26:32 pm
Up
Rainoth
Moderator
Offline Off
Code:
1
function glow(location,position

Position argument is second.
Location argument is first.
Code:
1
glow("ptop","gfx/sprites/flare2.bmp"

Location argument is second
Position argument is first.

Also, they are all strings like
Code:
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.
17.10.14 07:45:03 pm
Up
Jarates
User
Offline Off
I fixed it but when I press remove perk the perk wont remove.
No errors in console...

More >
edited 1×, last 17.10.14 07:55:47 pm
17.10.14 08:08:36 pm
Up
Rainoth
Moderator
Offline Off
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.
17.10.14 08:31:20 pm
Up
Jarates
User
Offline Off
If I make something like this:

Code:
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.
17.10.14 09:48:48 pm
Up
Rainoth
Moderator
Offline Off
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:
Code:
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
Code:
1
2
3
4
for i = 1,#img[id] do
     freeimage(img[id][i])
     img[id][i] = nil
end
To the start Previous 1 Next To the start