Forum

> > CS2D > Scripts > Code with the hook "attack"
Forums overviewCS2D overview Scripts overviewLog in to reply

English Code with the hook "attack"

15 replies
To the start Previous 1 Next To the start

old Code with the hook "attack"

mozilla1
User Off Offline

Quote
Hello guys. Cookies for you cookie .

All I need is when my code hooks the command "attack", it saves the last weapon used in a variable. The only problem is when it hooks something throwable and unstackable (Grenades, etc, it works only with Flashbang cause it has a stack of two items), it doesn't save the last weapon.

What I did so far:

1
2
3
4
5
6
7
function attack(id)
weapon = player(id,"weapon")
	if p_infammo[id] then
		local weapon = weapon
		equip(id,weapon)
	end
end

Phew.

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
user Suprise has written
I can't really help you, but Did you added
1
addhook("attack","attack")
?
Maybe use another function name.



Nah. I like the function with the same name than the hook.

old Re: Code with the hook "attack"

Alistaire
User Off Offline

Quote
user mozilla1 has written
Hello guys. Cookies for you cookie .

All I need is when my code hooks the command "attack", it saves the last weapon used in a variable. The only problem is when it hooks something throwable and unstackable (Grenades, etc, it works only with Flashbang cause it has a stack of two items), it doesn't save the last weapon.

What I did so far:

1
2
3
4
5
6
7
function attack(id)
weapon = player(id,"weapon")
	if p_infammo[id] then
		local weapon = weapon
		equip(id,weapon)
	end
end

Phew.


To hook in CS2D Lua means to add a function to an event in the game. Hooking a function to an attack-hook means that function happens on the event 'attack'.

You say you like 'the function with the same name than the hook', but you don't understand how it works. The function 'attack' is never called by the hook 'attack' if there is no addhook('attack', 'attack'), therefor your post made no sense. BlauBurger asked you if you forgot an addhook(), not if you preferred hooked functions called the same as the events they are hooked to.

----

To answer your question, your script makes completely no sense;

1
2
3
4
5
6
7
8
9
10
function attack(id)
	weapon = player(id,"weapon")
-- better use local here so that weapon isn't saved all over the script.
	if p_infammo[id] then
		local weapon = weapon
-- weapon is declared before this function. you literally wrote "DERP is 15, and DERP is DERP" which is obvious. Lua won't forget what weapon means after 1 if statement.
		equip(id,weapon)
-- EQUIP() ISNT A FUNCTION! IT'S parse('equip '..id..' '..weapon)!! 
	end
end

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
Alistaire , i used "Wrapper" so the function equip(id,weapon) is the same as I was parsing("equip "..id.." ".." weapon"). BTW check the wrapper script in your lua folder.

I know that the hook function works after an event in CS2D. But my real problem is when you throw a grenade and the code try to save the ID on the variable, cause the function comes after the event.

Edit: And yes, Blau Burger, I used addhook("attack","attack").

old Re: Code with the hook "attack"

Alistaire
User Off Offline

Quote
user mozilla1 has written
I know that the hook function works after an event in CS2D. But my real problem is when you throw a grenade and the code try to save the ID on the variable, cause the function comes after the event.


1. The code 'saves' nothing.
2. The function always comes after the event.
3. player(id, 'weapon')? You mean player(id, 'weapontype')?

----

Also; why do you even have local weapon = weapon.

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
user Alistaire has written
user mozilla1 has written
I know that the hook function works after an event in CS2D. But my real problem is when you throw a grenade and the code try to save the ID on the variable, cause the function comes after the event.


1. The code 'saves' nothing.
2. The function always comes after the event.
3. player(id, 'weapon')? You mean player(id, 'weapontype')?

----

Also; why do you even have local weapon = weapon.


1) I wrote this code as an example.
2) I put local weapon because I dont want to change the global one in the function.
3) Yes, sorry, I meant player(id,'weapontype')

old Re: Code with the hook "attack"

Alistaire
User Off Offline

Quote
user mozilla1 has written
1) I wrote this code as an example.
2) I put local weapon because I dont want to change the global one in the function.
3) Yes, sorry, I meant player(id,'weapontype')


1. If you want people to fix your script, why did you not post it.
2.1 weapon is changed every time anyone clicks lmb
2.2 Variables never change if you don't declare anything about them
2.2.1 Which means that if you use equip(id, weapon) it won't change
3. Your script could be written like this:
1
2
3
4
5
6
function f_attack(id)
	local a = player(id, 'weapontype')
	if p_infammo[id] then
		equip(id, a)
	end
end
and do the exact same thing without unnecessary stuff like declaring a local variable with an unaltered global variable.

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
Everything in your script is alright, but if you throw a grenade, then a = 0. I only want to fix that, and there isn't a need to see the whole code.

I want something like this pseudo-code:

1
2
3
If a player uses a item then save its ID to "weapon".
If the item is throwable (grenades, molotovs, snowballs..), then save its id to "weapon" before using it.
Don't change the global weapon, only the local weapon.

Sorry if I'm asking too much, but I didn't find a way to this. Maybe using hook always, but it will lag as hell.

old Re: Code with the hook "attack"

EngiN33R
Moderator Off Offline

Quote
I'm not sure if this works in all cases, but I think it would do the trick with weapons that disappear when you use them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wih = {} --Weapon In Hand
wu = {} --Weapon Used

addhook("select","select")
function select(id,w)
	wih[id]=w --Set the weapon in hand to the weapon the player switched to
end

addhook("attack","attack")
function attack(id)
	wu[id]=player(id,"weapontype")
	if wu[id]==0 then --If the weapon disappears upon using it, wu would be set to 0, so in that case
		wu[id]=wih[id] --Set the used weapon to the weapon that was last in the player's hand
	end
end

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
user EngiN33R has written
I'm not sure if this works in all cases, but I think it would do the trick with weapons that disappear when you use them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wih = {} --Weapon In Hand
wu = {} --Weapon Used

addhook("select","select")
function select(id,w)
	wih[id]=w --Set the weapon in hand to the weapon the player switched to
end

addhook("attack","attack")
function attack(id)
	wu[id]=player(id,"weapontype")
	if wu[id]==0 then --If the weapon disappears upon using it, wu would be set to 0, so in that case
		wu[id]=wih[id] --Set the used weapon to the weapon that was last in the player's hand
	end
end



Thanks user EngiN33R , That is exactly what I was looking for.
Sorry if I wasted your time. (You can lock this topic if you want)

old Re: Code with the hook "attack"

omg
User Off Offline

Quote
i dont think that would work, since throwing something with 1 ammo would automatically switch to a different weapon

did u test it yet?

old Re: Code with the hook "attack"

mozilla1
User Off Offline

Quote
user omg has written
i dont think that would work, since throwing something with 1 ammo would automatically switch to a different weapon

did u test it yet?



I'm testing and seeing good results as the var wu never getting 0. Thats enough, and I can fix the rest.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview