Forum

> > CS2D > Scripts > why error? a nil value
Forums overviewCS2D overview Scripts overviewLog in to reply

English why error? a nil value

15 replies
To the start Previous 1 Next To the start

old why error? a nil value

kalis
User Off Offline

Quote
here my code :
i leave cs2d 2, 3 week ! help me @ @
i want remove some image near player:
Error :
1
attempt to perform arithmetic on filed '?' (a nil value)
Lua :
create image
1
2
3
4
5
6
7
8
9
function create_light(l_x,l_y)
	if l_x and l_y then
		i = i + 1
		save_x[i] = l_x
		save_y[i] = l_y
		l_img[i] =  image("gfx/sprites/flare3.bmp",save_x[i],save_y[i],1)
		imagepos(l_img[i],save_x[i],save_y[i],math.random(1,360))
	end
end

remove 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
addhook("attack","remove")
function remove(id)
	if id ~= nil then
		remove_light(id)
	end
end




function remove_light(id)
	local x = player(id,"x")
	local y = player(id,"y")
	if i then
		if x < (save_x[i] + 16) and x > (save_x[i] - 16) then
			if (y < save_y[i] + 16) and y > (save_y[i] - 16) then
				if l_img[i] ~= nil then
					save_x[i] = nil
					save_y[i] = nil
					freeimage(l_img[i])
				end
			end
		end
	end
end

old Re: why error? a nil value

EngiN33R
Moderator Off Offline

Quote
But that's not an error connected with math functions, it's connected with tables.

Was the line where the error occurred specified? If so please provide that line from the code.

old Re: why error? a nil value

DannyDeth
User Off Offline

Quote
@EngiN33R:
Just saw it. Didn't read the error message properly. Anyway, it means that a field of a table doesn't exist, but you are trying to use it. This will take a while to debug.

old Re: why error? a nil value

Yasday
User Off Offline

Quote
"i" is used for the "save_x" and "save_y" table.
1
2
... (save_x[i] - 16) ...
... (save_y[i] - 16) ...
Could you show us the whole code and tell us what it's expected to do?

old Re: why error? a nil value

EngiN33R
Moderator Off Offline

Quote
@Yasday

user kalis has written
1
2
3
4
5
6
7
8
9
function create_light(l_x,l_y)
	if l_x and l_y then
		i = i + 1
		save_x[i] = l_x
		save_y[i] = l_y
		l_img[i] =  image("gfx/sprites/flare3.bmp",save_x[i],save_y[i],1)
		imagepos(l_img[i],save_x[i],save_y[i],math.random(1,360))
	end
end

old Re: why error? a nil value

kalis
User Off Offline

Quote
i want make it like minecraft
make a light in somethings and remove it with hook "attack" (but player must near it)
here full 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
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
55
56
57
save_x = {}
save_y = {}
l_img = {}

l_x = {}
l_y = {}
local i

addhook("say","hehe")
function hehe(id,txt)
	if i == nil then
		i = 0
	else
		i = i
	end
	if txt == "test" then
		l_x = player(id,"x")
		l_y = player(id,"y")
		create_light(l_x,l_y)
	end
end

function create_light(l_x,l_y)
	if l_x and l_y then
		i = i + 1
		save_x[i] = l_x
		save_y[i] = l_y
		l_img[i] =  image("gfx/sprites/flare3.bmp",save_x[i],save_y[i],1)
		imagepos(l_img[i],save_x[i],save_y[i],math.random(1,360))
	end
end

addhook("attack","remove")
function remove(id)
	if id ~= nil then
		remove_light(id)
	end
end




function remove_light(id)
	local x = player(id,"x")
	local y = player(id,"y")
	if i then
		if x < (save_x[i] + 16) and x > (save_x[i] - 16) then
			if (y < save_y[i] + 16) and y > (save_y[i] - 16) then
				if l_img[i] ~= nil then
					save_x[i] = nil
					save_y[i] = nil
					freeimage(l_img[i])
				end
			end
		end
	end
end
any one reply ?
edited 1×, last 01.09.11 01:53:17 pm

old Re: why error? a nil value

kalis
User Off Offline

Quote
still error man =.=
i changed and still error
1
attampt to perform arithmetic on field '?' (a nil value)

old Re: why error? a nil value

DannyDeth
User Off Offline

Quote
Testing the script... I get no errors whatsoever, however I do not see the flare over any players. Are you possibly insane, kalis?

old Re: why error? a nil value

kalis
User Off Offline

Quote
this lua still making
in start not appear error but
Lua have hook "say"
when you create light with cmd "test"
And try to create 2,3 light ,go remove all light you created
It will error !
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview