Forum

> > CS2D > Scripts > Freeimage(ct_img[id])
Forums overviewCS2D overview Scripts overviewLog in to reply

English Freeimage(ct_img[id])

7 replies
To the start Previous 1 Next To the start

old Freeimage(ct_img[id])

kalis
User Off Offline

Quote
hey every one
i make more thread because not any reply
sorry =.=
i have code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function free_all()
     for n, w in ipairs(ct_img) do
          freeimage(w)
     end
end

function ct_gfx(id)
	if player(id,"health") > 0 then
		x_img[id] = 600
		y_img[id] = (y_img[id]) + 36
		for i = 1,10 do
			ct_img[i] = image("gfx/z_kalis/100 score.bmp", x_img[id], y_img[id], 2,id)
		end
	else
		free_all()
		x_img[id] = 600
		y_img[id] = 80
	end

end
i use function free_all() to free all image but cant
help me ? thank for read ! =.=

old Re: Freeimage(ct_img[id])

kalis
User Off Offline

Quote
i used free_all()
and last image remove :))

i think lua must like this :
1
2
3
4
5
function free_all()
     for id = 1,10 do
          freeimage(ct_img[id])
     end
end

old Re: Freeimage(ct_img[id])

Lee
Moderator Off Offline

Quote
You don't have a contiguous list, for example, if ct_gfx is called in the following order:

1, 2, 32, 5

and then free_all is called, then only player 1 and player 2's images will be freed because ipairs is an incremental iterator.

You can try to use a variable for loop

1
for id=1,32 do freeimage(ct_img[id]) end

However, this will return errors if ct_img[id] doesn't exist yet. For example, say that we again have ct_gfx called in this order:

1, 2, 32, 5

then calling freeimage(ct_img[3]) will result in an error complaining about how the first parameter is expected to be a number but got nil instead.

Now you can try to enclose the whole thing within an if statement, but it's more idiomatic to write

1
2
3
4
for id, img in pairs(ct_img) do
	freeimage(img)
	ct_img[id] = nil -- remember to remove old ids so we don't accidentally free the same resource twice.
end

old Re: Freeimage(ct_img[id])

Apache uwu
User Off Offline

Quote
Ah yes at the end of the loop you need to reset all the items in the array.

1
2
3
4
5
6
function free_all()
	for _,key in ipairs(ct_img) do
		freeimage(key)
	end
ct_img={}
end

old Re: Freeimage(ct_img[id])

kalis
User Off Offline

Quote
local :
1
local ct_img = {}

here my code
Create Image
1
2
3
4
5
fucntion ct_gfx(id)
		for i = 1,10 do
			ct_img[i] = image("gfx/z_kalis/100 score.bmp", x, y, 2,id)
		end
	end

@lee i try to use u r code and Textual too
But it not work and not any error
my ct_img is array of each player

Free Image :
1
2
3
4
5
6
7
function free_all()
for id, img in pairs(ct_img) do
     freeimage(img)
     ct_img[id] = nil -- remember to remove old ids so we don't accidentally free the same resource twice.
end
ct_img={}
end

Remove Image when die :
1
2
3
4
addhook("die","randomhook")
function randomhook(victim,killer,weapon,x,y)
	free_all()
end
hey ! on hook "DIE" i can use this code ?
1
free_all(victim)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview