Forum

> > CS2D > Scripts > images and freeimage (over player)
Forums overviewCS2D overview Scripts overviewLog in to reply

English images and freeimage (over player)

19 replies
To the start Previous 1 Next To the start

old images and freeimage (over player)

ZaibatsuTEAM
User Off Offline

Quote
so - im sure there are threads about this already and i saw a few but wasn't too helpful - so i was like: "fuck it, i'll post and ask anyway"

So. yeah.. I have made a sort of class system. but I want it to include images as well. so when a player chooses a class, he will get a few guns but also an image over his head, like a hat. But then if he changes class, the image should disappear and the next image for the class he chose should be on his head.

Now, I've never learned to use freeimage too much, and even though I do for example "freeimage(id)" if the image name is id, it will get removed from everyone instead only the player who changed class, etc...

Anyone with pro coding skills who can show me how to make a proper image and freeimage script with classes so I can understand how to do it? Thanks

old Re: images and freeimage (over player)

ZaibatsuTEAM
User Off Offline

Quote
well the classes stuff are there and then some tween stuff - but i still didn't understand much haha

i have created the classes but the only thing i want is to be able to remove the image on class change.
i know freeimage should do the job, but its all messed up since it removes the pic from all players instead of only myself. and sometimes the pic gets bugged on one player as it wont get removed at all - only on round restart. xd

old Re: images and freeimage (over player)

Yates
Reviewer Off Offline

Quote
Let me give you a few tips and code examples instead of fully making it.

What you should do is create a variable which is saved per player for the same image.

What I normally do is the following:

1
2
3
4
5
6
Player = {} -- This defines that there will be a 'Player' table.

function _join(id) -- On join the Player table will be set to empty, this also covers a lot of errors that the table doesn't exist and what not.
	Player[id] = {}
end
addhook("join","_join")

Once I've got this, I start creating images like so:
1
Player[id].image = image() -- image() is cs2d stuff, cannot be arsed typing that you probably know how this works. You can call Player[id].image something else like Player[id].classhat

Now I have set the image into the Player table which is different for each player because it only sets to the id being used by one single player.

• Tips:

• 1. Remember when you free the image like so:
1
freeimage(Player[id].image)
You also do this straight after it:
1
Player[id].image = nil
This "removes" the image id from the Player table so no bugs can occur (every time you create an image, it gives the variable the image id. Once you free the image this id can be used again, so we make sure no variable can contain the same id by setting the variable to nil straight after).

• 2. On round end (not start, MUST be end), free all images and set the variable to nil:
1
2
3
4
5
6
for _,all in pairs(player(0,"table")) do
	if Player[all].image then
		freeimage(Player[all].image)
		Player[all].image = nil
	end
end
This will remove ALL images on round end making sure no bug can occur in the new round because of duplicate id's and such.

• 3. Free an image and set the variable to nil when someone leaves the game:
1
2
3
4
5
function _leave(id)
	freeimage(Player[id].image)
	Player[id].image = nil
end
addhook("leave","_leave")
This also makes sure no duplicate image id's can occur and makes sure when someone new joins and gets the same player id as the person that just left - no image is there and can be created again.

• 4. (Personal opinion) - I always like to set the image when someone spawns, and free it when someone dies. This makes sure everyone keeps getting the correct image they should and if a duplicate image id occurs I can easily figure it out.

Hope this helps.

old kinda working - still not

ZaibatsuTEAM
User Off Offline

Quote
well when i change class, then i die automatically - but the picture will not disappear *still*.. code looks like this:
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
Player = {}

function _join(id)
     Player[id] = {}
end
addhook("join","_join")

function _leave(id)
     freeimage(Player[id].image)
     Player[id].image = nil
end
addhook("leave","_leave")

addhook("die","diess")
function _diess(id)
     freeimage(Player[id].image)
     Player[id].image = nil
end


addhook("spawn","modelshit")
function modelshit(id)
	if sel_class[id]==20 then
	Player[id].image = image('gfx/HECU.png', 1, 0, 200+id)
end
end

old rofl

ZaibatsuTEAM
User Off Offline

Quote
oh god.. lol i suck hahahaha
edit: even though i did change it to _diess now, it still doesnt work............ wtf?! xDD

old Re: images and freeimage (over player)

Rainoth
Moderator Off Offline

Quote
Please stop with all the 'hahaha lol oh god xDDD' and similar. They're pretty needless and I would have marked your post as spam if you hadn't edited it about your problem. Lucky you.

Still, just saying 'doesnt work' won't give us any valuable information. You have to say which part is not working.
Is the image not set properly?
Image doesn't appear at all?
Image isn't removed upon death?
Image isn't removed when a player leaves?
You have problems when getting image multiple times?

Tell us what is wrong so we know what to help with. The code you provided above (besides the fixed part with misspelling) should work.

old shit

ZaibatsuTEAM
User Off Offline

Quote
well the image wont get removed on death, here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
model = {}

addhook("join","shitshit")
function shitshit(id)
	model[id] = {}
end

addhook("spawn","pieceofshit")
function pieceofshit(id)

if sel_class[id]==20 then
model[id] = image("gfx/HECU.png",1,0,200+id)
end

addhook("die","FUCKNG")
function FUCKNG(id)
freeimage(model[id])
model[id] = nil
end
end

old Re: images and freeimage (over player)

Ajmin
User Off Offline

Quote
Let me ask u,
How will the image gets removed?
as u added it in spawn hook,
When a player dies, the image get removed,
When he spawns again it is added -.-

I think its pretty buggy with die hook,
Here is a fixed code,
I hope it suits as well!
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
Player = {}
firstspawn = {}

function _join(id)
     Player[id] = {}
     firstspawn[id]=1
end
addhook("join","_join")

function _leave(id)
     freeimage(Player[id].image)
     Player[id].image = nil
end
addhook("leave","_leave")


addhook("spawn","modelshit")
function modelshit(id)
	if firstspawn[id]==0 then
		if Player[id].image~=nil then
	 		freeimage(Player[id].image)
			Player[id].image = nil
		end
	end
	if firstspawn[id]==1 then
     		Player[id].image = image('gfx/HECU.png', 1, 0, 200+id)
		firstspawn[id]=0
	end
	
end
edited 1×, last 21.08.15 04:01:21 pm

old Re: images and freeimage (over player)

VADemon
User Off Offline

Quote
pieceofshit doesn't have a closing end so the script shouldn't be running due to the parsing error.

Tell us what is shown in chat when you spawn/die
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
model = {}

addhook("join","shitshit")
function shitshit(id)
	model[id] = {}
end

addhook("spawn","pieceofshit")
function pieceofshit(id)

	if sel_class[id]==20 then
		model[id] = image("gfx/HECU.png",1,0,200+id)
	end
	msg("Set model[" .. id .. "] to " .. model[id]) --
end
addhook("die","FUCKNG")
function FUCKNG(id)
	msg("Removing... model[" .. id .. "] equals " .. model[id]) --
	freeimage(model[id])
	model[id] = nil
end

old it says...

ZaibatsuTEAM
User Off Offline

Quote
when spawn as class 20, it says model[1] to 2. but when i die it says model[1] to 2 as well. and picture still not removed.
Edit: Ajmin, i'll try your code.. i didn't see it until now..
edited 1×, last 21.08.15 05:02:38 pm

old Re: images and freeimage (over player)

Ajmin
User Off Offline

Quote
idk whats happening,
die hook is buggy,Unable to freeimage using die hook lol,

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
player = {}
remove_img = {}


addhook("join","_join")
function _join(id)
	player[id]={}
	player[id].imagee=0
	remove_img[id] = 0
end

addhook("spawn","sp")
function sp(id)
	if remove_img[id]==1 then
		removeimg(id)
	end
	if sel_class[id]==20 then
		removeimg(id)
		player[id].imagee = image("gfx/HECU.png", 1, 0, 200+id)
		remove_img[id]=1
	end
end



function removeimg(id)
	local id = tonumber(id)
	freeimage(player[id].imagee) 
end

The image is only removed,if the class is changed!

Edit: Perhaps the code is not working comfortable,So plz post ur class script to ensure it is fitted well.
edited 1×, last 21.08.15 05:14:07 pm

old ah

ZaibatsuTEAM
User Off Offline

Quote
YAY The code works perfectly! - Thank you so much for your help, Ajmin, and everyone else!

Thanks! <3<3<3<3
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview