Forum

> > CS2D > Scripts > Automatic hat by kill
Forums overviewCS2D overview Scripts overviewLog in to reply

English Automatic hat by kill

14 replies
To the start Previous 1 Next To the start

old Automatic hat by kill

Thenini7
User Off Offline

Quote
As is the script for this?
For kill, the player automatically receive 1 Hat and the other killing another hat.

What is script for this?
edited 4×, last 06.04.16 02:02:18 am

old Re: Automatic hat by kill

Rainoth
Moderator Off Offline

Quote
First of all, the title "how to do this?" does not describe what you want to ask, only that you want to ask something. It's too vague.
Your English is hardly understandable but judging by the word "skin", I'm going to assume that you want to place images on players when they enter server.

Here's how you'd want to do it:
• Upon spawn create a new entry in your table for players (not default player table!) and assign it a new image with appropriate mode.
• When the player leaves, make sure to freeimage the entry that the player was using and then set it to nil.

These are the basics of what you need. Use CS2D.com/help for reference when you write code. We'll make sure to help you out if you don't succeed in something and point out whatever mistakes you may make.

Cheers.

old Re: Automatic hat by kill

Dousea
User Off Offline

Quote
Here's the code, complete with comments. Try to understand it. Learn Lua so you don't have to ask other members for scripts anymore. This code may not work as expected.
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
-- Create a table that holds player hat images
local p = {}

-- A function to remove a player's hat image
function freehatimage(i)
	-- If this player (i) has any hat image
	if (p[i]) then
		-- Remove the hat image from this player (i)
		freeimage(p[i])
		
		-- Set the element's value to nil for no image
		p[i] = nil
	end
end

-- A function to add hat image to a player
function addhatimage(i)
	-- If this player (i) does not have any hat image
	if (not p[i]) then
		-- Put a hat image on this player (i)
		p[i] = image("to/your/hat.png", 1, 0, i + 200)
	end
end

-- When every start of round
function startroundhook(m)
	-- Iterate through all players (32 players)
	for i = 1, 32 do
		freehatimage(i)
	end
end

-- Add hook for kill & startround
addhook("kill", "addhatimage")
addhook("startround", "startroundhook")

-- Add hook for die & leave
for i, v in ipairs({"die", "leave"}) do
	addhook(v, "freehatimage")
end
edited 4×, last 08.04.16 04:08:16 pm

old Re: Automatic hat by kill

GeoB99
Moderator Off Offline

Quote
@user Dousea: Pretty sure he wants also when you killed another enemy then you'll get another hat for this case. Like getting a hat randomly.

I just customized this snippet of code a bit, it's up to yourself to see if it works at the fullest.
The Code >

Your only task is by changing those paths of Hat_paths table inside the quotes with actual ones whose the hat files stay to make sure everything works as it should be expected. To understand clearly how in 35 line works, I'll explain it to you:

It creates a local variable calling it randomHat whose it is assigned a mathematical function named math.random which basically generates a pseudo number. With arguments inside the parenthesis of math.random, 1 and #Hat_paths, when math.random is called it'll generate an uniformed pseudo-random number in its range. We'll consider that number the hat for our case.

The randomHat which is a local variable it can be used ONLY for a specific scope and not in many sides like in global variables. Copied & pasted that name inside the cs2d lua cmd image parameters and that's it.

It's advisable to read the math.random reference (link - you can see the math.random part below) if you want to grasp out how this function works. You might want also to read about local variables and many things too to cope with Lua. I'd recommend this Lua Tutorial Pil for absolute beginners. There are other tutorials which are helpful too, you can check them.

old Re: Automatic hat by kill

Yates
Reviewer Off Offline

Quote
You still need to fix round restart and set the table content for the hat to nil (not 0).

old Re: Automatic hat by kill

Dousea
User Off Offline

Quote
@user Yates: I don't know but I feel like creating a new element inside a table every configured time is like an expensive call. But sure as you wish.

old Re: Automatic hat by kill

Yates
Reviewer Off Offline

Quote
I'll tell you what to always do with images instead

When you free an image, set it to nil straight after - does't matter where you free it, even on round end or leave.
1
2
freeimage(image)
image = nil
Thing with round start is that the map can change before round start is triggered, people leave because they don't want to download the map, so free all images on round end instead and make sure to be extra safe that you free images when someone leaves as well.

If you use a lot amount of images, and sometimes these images may not even be used by a player, do this:
1
2
3
4
5
6
7
8
9
10
if (image1) then
 freeimage(image1)
 image1 = nil
end
if (image2) then
 freeimage(image2)
 image2 = nil
end

etc..
This makes sure that you don't get any shit errors returned thus the rest of the images are freed and variables set to nil.

The reason why you have to do this is that the variable used for the player with id 1 still exists even after he leaves and someone else takes his place. This causes a load of crap to happen like scaling and image for player 2 may also scale for player x simply because the image variable was not set to nil and still contained an id. I'd rather have freeimage to automatically set the variable given to nil, but I don't think DC saw my post in the idea thread or doesn't want to for some reason.

old Re: Automatic hat by kill

VADemon
User Off Offline

Quote
user Yates has written
I'd rather have freeimage to automatically set the variable given to nil, but I don't think DC saw my post in the idea thread or doesn't want to for some reason.

That's nearly impossible to achieve because Lua doesn't pass variable pointers but their actual values instead. What we could do is
1
imageId = freeimage(imageId)
if freeimage always returned nil. PS: I kinda begin to like this hack.

old Re: Automatic hat by kill

Dousea
User Off Offline

Quote
@user VADemon: It's possible but I think it's only in LuaJIT. I used a LÖVE library which my variables modified by it. It must be global variables to work I guess.

@user Yates: Did you post it for me or everyone? If it's for me then thanks. I like to set 0 for my image variables after freeing the images because if we add images, the image variables will be numbers. But I think it's a personal taste. And if you don't know how to do exactly as user VADemon do (not I want to be a smart-ass), here.
1
2
3
4
5
6
7
local f = freeimage

function freeimage(i)
    f(i)
    
    return nil
end

old Re: Automatic hat by kill

Yates
Reviewer Off Offline

Quote
@user Dousea: I posted it for everyone, thus including you. By the way, I'd rather have freeimage return nil by default and not have to hack it in everywhere. Having it in by default would stop these image bugs from causing in the first place and not have every one wondering why their images are bugs and have to look it up.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview