Forum

> > CS2D > Scripts > Setting image on player.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Setting image on player.

13 replies
To the start Previous 1 Next To the start

old Setting image on player.

Rainoth
Moderator Off Offline

Quote
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
function initArray(m)
local array = {}
      for i = 1,m do
        array[i]=0
	end
return array
end

img = initArray(32)
imgb = initArray(32)

addhook("second","check")
function check()
	for p=1,32 do
		if player(p,"weapontype")~=50 or player(p,"weapontype")~=53 then
		img[p] = 1
	else
		img[p] = 0
		end
	end
end

addhook("second","checkb")
function checkb()
	if (img[p]==1) then
		image("gfx/sprites/text.bmp",1,1,201)
	elseif (img[p]==0) then
		image("gfx/sprites/text2.bmp",1,1,201)
	end
end

No errors but image doesn't appear.
I also want only one image to appear but I don't know how to do it.

old Re: Setting image on player.

Avo
User Off Offline

Quote
At first - you use two functions with the same hook, what will lead to crashes/bugs if third argument isn't passed.

In second function you presented us here you will draw image only at player with ID 1.

The worst thing you did here:
images drawn every second without removing them.

The very first question I should ask is what are you trying to do?

old Re: Setting image on player.

Alistaire
User Off Offline

Quote
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
function array()
	local a = {}
	for i = 1, 32 do
		a[i] = 0
	end
	return a;
end

plr = {images = {weapon = {image = array(), used = array()}}}

addhook('join', 'joinHook')
addhook('select', 'selectHook')

function joinHook(id)
	if plr.images.weapon.used[id] == 1 then
		plr.images.weapon.used[id] = 0
	end
end

function selectHook(id, type)
	local a = 2
	if type == 50 or type == 53 then
		a = 1
	end
	if plr.images.weapon.used[id] == 1 then
		freeimage(plr.images.weapon.image[id])
	end
	plr.images.weapon.image[id] = image('gfx/sprites/text'..a..'.bmp', 1, 0, 200 + id)
	plr.images.weapon.used[id] = 1
end

The join hook is just to prevent some errors when people join on an already used ID. Not really needed tbh, but it works.

old Re: Setting image on player.

Rainoth
Moderator Off Offline

Quote
user Avo has written
1. Remove this annoying meme.

2. Read it one more time http://www.cs2d.com/help.php?luacmd=image

3. You didn't explain what your script is going to do.


1. It disappeared.
2. I read it.
3. It's quite obvious when you look at hook and IF.

It's not quite what I have in my mind that I scripted but similar.
Idea :
• If player has harmful weapons/equipment in inventory he gets image that says "armed"
• If player doesn't have such weapons he gets image "unarmed"
• This is checked every second
• I hope there is a way to check if has player has items in inventory..

old I am cool

Gajos
BANNED Off Offline

Quote
@user Rainoth Hey, I did it on my Android and I hope that it will work.
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
arm = {}

arm.images = {
	armed = 'gfx/sprites/text.bmp'
	unarmed = 'gfx/sprites/text2.bmp'
}

arm.initArray = function(value)
     local array = {}
     for _ = 1,32 do
          array[_] = value
     end
     return array
end
arm.img.armed = arm.initArray(nil)
arm.img.unarmed = arm.initArray(nil)

addhook('second','arm.second')
arm.second = function()
	for v, id in pairs(player(0,'table')) do
		for _, i in pairs(item(0,'table')) do
			if playerweapons(id) ~= 50 and playerweapons(id) ~= 53 and playerweapons(id) == i then
				if arm.img.unarmed[id] ~= nil then
					freeimage(arm.img.unarmed[id])
					arm.img.unarmed[id] = nil
				end
				if arm.img.armed[id] == nil then
					arm.img.armed[id] = image(arm.images.armed,1,1,200 + id)
				end
			else
				if arm.img.armed[id] ~= nil then
					freeimage(arm.img.armed[id])
					arm.img.armed[id] = nil
				end
				if arm.img.unarmed[id] == nil then
					arm.img.unarmed[id] = image(arm.images.unarmed,1,1,200 + id)
				end
			end
		end
	end
end

You must use hook "leave":
1
2
3
4
5
6
7
8
9
10
11
addhook('leave','arm.leave')
arm.leave = function(id)
	if arm.img.armed[id] ~= nil then
		freeimage(arm.img.armed[id])
		arm.img.armed[id] = nil
	end
	if arm.img.unarmed[id] ~= nil then
		freeimage(arm.img.unarmed[id])
		arm.img.unarmed[id] = nil
	end
end
edited 3×, last 08.03.13 07:24:50 am

old Re: Setting image on player.

Dousea
User Off Offline

Quote
This is very simple code and didn't tested yet, but should be work.
1
2
3
4
5
6
7
8
9
10
11
12
addhook("second","_image")
function _image()
	for id = 1, 32 do
		local imageid = {}
		if player(id,"weapon") ~= 50 and player(id,"weapon") ~= 53 then
			imageid = image("gfx/sprites/flare2.bmp",0,0,id+200)
		else
			imageid = image("gfx/sprites/flare3.bmp",0,0,id+200)
		end
		imagepos(imageid,320,120,0)
	end
end

old Re: Setting image on player.

Gajos
BANNED Off Offline

Quote
user Dousea has written
This is very simple code and didn't tested yet, but should be work.
1
2
3
4
5
6
7
8
9
10
11
12
addhook("second","_image")
function _image()
	for id = 1, 32 do
		local imageid = {}
		if player(id,"weapon") ~= 50 and player(id,"weapon") ~= 53 then
			imageid = image("gfx/sprites/flare2.bmp",0,0,id+200)
		else
			imageid = image("gfx/sprites/flare3.bmp",0,0,id+200)
		end
		imagepos(imageid,320,120,0)
	end
end

@user Dousea user Alistaire has already done a similar script to yours. My script is other.

old Re: Setting image on player.

Rainoth
Moderator Off Offline

Quote
user Gajos has written
@user Rainoth Hey, I did it on my Android and I hope that it will work.
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
arm = {}

arm.images = {
	armed = 'gfx/sprites/text.bmp'
	unarmed = 'gfx/sprites/text2.bmp'
}

arm.initArray = function(value)
     local array = {}
     for _ = 1,32 do
          array[_] = value
     end
     return array
end
arm.img.armed = arm.initArray(nil)
arm.img.unarmed = arm.initArray(nil)

addhook('second','arm.second')
arm.second = function()
	for v, id in pairs(player(0,'table')) do
		for _, i in pairs(item(0,'table')) do
			if playerweapons(id) ~= 50 and playerweapons(id) ~= 53 and playerweapons(id) == i then
				if arm.img.unarmed[id] ~= nil then
					freeimage(arm.img.unarmed[id])
					arm.img.unarmed[id] = nil
				end
				if arm.img.armed[id] == nil then
					arm.img.armed[id] = image(arm.images.armed,1,1,200 + id)
				end
			else
				if arm.img.armed[id] ~= nil then
					freeimage(arm.img.armed[id])
					arm.img.armed[id] = nil
				end
				if arm.img.unarmed[id] == nil then
					arm.img.unarmed[id] = image(arm.images.unarmed,1,1,200 + id)
				end
			end
		end
	end
end

You must use hook "leave":
1
2
3
4
5
6
7
8
9
10
11
addhook('leave','arm.leave')
arm.leave = function(id)
	if arm.img.armed[id] ~= nil then
		freeimage(arm.img.armed[id])
		arm.img.armed[id] = nil
	end
	if arm.img.unarmed[id] ~= nil then
		freeimage(arm.img.unarmed[id])
		arm.img.unarmed[id] = nil
	end
end


" "}" expected to close "{" at line 3 near unarmed"
The error I get. Although I can see there is "}" in next line ...

old Re: Setting image on player.

EngiN33R
Moderator Off Offline

Quote
1
2
3
4
arm.images = {
     armed = 'gfx/sprites/text.bmp',
     unarmed = 'gfx/sprites/text2.bmp'
}

Is the correct version. You need commas in table constructors.

user Gajos has written
1
2
3
4
5
6
7
8
9
arm.initArray = function(value)
     local array = {}
     for _ = 1,32 do
          array[_] = value
     end
     return array
end
arm.img.armed = arm.initArray(nil)
arm.img.unarmed = arm.initArray(nil)

That is a waste of code! If a table field is set to nil, the field is empty, that is it doesn't exist! You don't need this, it will work if you just replace it with:

1
2
arm.img.armed = {}
arm.img.unarmed = {}

Unless I missed something you also lack a declaration for arm.img. So you'll need to place this after arm = {} :

1
arm.img = {}

old Re: Setting image on player.

Rainoth
Moderator Off Offline

Quote
I did everything that EngiN33R said.
2 hooks are added but it doesn't work.
When I appear I get the "text2" image. That's fine since I own knife but when I take weapon like m4a1 it still says I'm unarmed even though I have lot's of things that are weapons and harmful..

old Trololololo

Gajos
BANNED Off Offline

Quote
This work! I found bug in lua about playerweapons(id). When player spawn playerweapons(id) doesn't check his inventory.
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
49
50
51
52
53
54
arm = {}

arm.images = {
	armed = 'gfx/sprites/text.bmp';
	unarmed = 'gfx/sprites/text2.bmp';
}

arm.initArray = function(value)
	local array = {}
	for _ = 1,32 do
		array[_] = value
	end
	return array
end
arm.img = {}
arm.img.armed = arm.initArray(nil)
arm.img.unarmed = arm.initArray(nil)

addhook('second','arm.second')
arm.second = function()
	for _, id in pairs(player(0,'table')) do
		for k, v in pairs(playerweapons(id)) do
			if v ~= 50 then
				if arm.img.unarmed[id] ~= nil then
					freeimage(arm.img.unarmed[id])
					arm.img.unarmed[id] = nil
				end
				if arm.img.armed[id] == nil then
					arm.img.armed[id] = image(arm.images.armed,1,1,200 + id)
				end
			else
				if arm.img.armed[id] ~= nil then
					freeimage(arm.img.armed[id])
					arm.img.armed[id] = nil
				end
				if arm.img.unarmed[id] == nil then
					arm.img.unarmed[id] = image(arm.images.unarmed,1,1,200 + id)
				end
			end
		end
	end
end

addhook('leave','arm.leave')
arm.leave = function(id)
     if arm.img.armed[id] ~= nil then
          freeimage(arm.img.armed[id])
          arm.img.armed[id] = nil
     end
     if arm.img.unarmed[id] ~= nil then
          freeimage(arm.img.unarmed[id])
          arm.img.unarmed[id] = nil
     end
end

old Re: Setting image on player.

Avo
User Off Offline

Quote
Quote
I found bug in lua about playerweapons(id)

It's not a bug, just normal. Spawn hook is called when player is being spawned what mean, in that moment player doesn't have any weapons. After some microseconds yes, that's the reason player can't always get items with parsing equip command, but by returning string with items to equip.

You didn't listen what Engin33r had said.
1
arm.img.armed = arm.initArray(nil)
Lua is dynamically typed language what means that all variables can be declared in every part of script. Setting nil as value is stupid, becouse all undeclared variables have "nil" as value by default.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview