With the help of facepunch a while back, I tried to make pointshop crosshairs but still haven't managed to get it to work.
The code is:
[lua]ITEM.Name = 'Crosshair Test'
ITEM.Price = 0
ITEM.Material = 'crosshair/crosshair_001.vmt'
ITEM.AllowedUserGroups = {"owner"}
function ITEM:OnEquip(ply, modifications)
if string.find(self.Material) then
ply.Crosshair = Material(self.Material, "nocull")
else
ply.Crosshair = Material(self.Material)
end
end
function ITEM:OnHolster(ply)
ply.Crosshair = nil
end
if CLIENT then
function ITEM:HUDPaint()
if !LocalPlayer().Crosshair then return end
surface.SetMaterial(LocalPlayer().Crosshair)
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end[/lua]
I'm not sure how to fix it but there is a lua error that shows:
[code]
[ERROR] addons/pointshop-master/lua/items/trails/crosshairtest.lua:7: bad argument #2 to 'find' (string expected, got no value)
1. find - [C]:-1
2. OnEquip - addons/pointshop-master/lua/items/trails/crosshairtest.lua:7
3. PS_EquipItem - addons/pointshop-master/lua/sv_player_extension.lua:320
4. PS_BuyItem - addons/pointshop-master/lua/sv_player_extension.lua:242
5. func - addons/pointshop-master/lua/sv_pointshop.lua:4
6. unknown - lua/includes/modules/net.lua:32[/code]
How can I fix this? Thanks for any help in advance :)
[url=http://wiki.garrysmod.com/page/string/find]string.find[/url] requires two arguments; the string to search in and the string to search for. I have no idea what you're intent is with it, so either remove it or fix it.
I basically want the script to put a crosshair in the centre of the screen where the crosshairs usually are :)
bump
[QUOTE=Baron von Hax;46150380]bump[/QUOTE]
You need to draw inside a hudpaint hook, it doesn't look like you are doing that and I would guess that is why it isn't working.
After reading the docs I think that [lua]string.find(self.Material)[/lua] should be [lua]string.find(self.Material, ".png")[/lua] as Material()'s second parameter seems only to apply to png materials.
I have:
[lua]ITEM.Name = 'Crosshair Test'
ITEM.Price = 0
ITEM.Material = 'crosshairs/rocket.png'
function ITEM:OnEquip(ply, modifications)
if string.find(self.Material, ".png") then
ply.Crosshair = Material(self.Material, "nocull")
else
ply.Crosshair = Material(self.Material)
end
end
function ITEM:OnHolster(ply)
ply.Crosshair = nil
end
if CLIENT then
function ITEM:HUDPaint()
if !LocalPlayer().Crosshair then return end
surface.SetMaterial(LocalPlayer().Crosshair)
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end[/lua]
This doesn't seem to cause any errors but it doesn't seem to show the crosshair either
OnEquip is a server function, it's not going to do anything for a clientside script. Network it.
[QUOTE=Nookyava;46175003]OnEquip is a server function, it's not going to do anything for a clientside script. Network it.[/QUOTE] *Noob response initiated*: How do I do that?
I don't think you need networking. I don't see why you're setting a variable on a player when the information you need is already on the ITEM object and shared. Just try this:
[lua]ITEM.Name = 'Crosshair Test'
ITEM.Price = 0
ITEM.Material = 'crosshairs/rocket.png'
if CLIENT then
function ITEM:HUDPaint()
if isstring( self.Material ) then -- Sadly there's no hook to make this more efficient
if string.find( self.Material, ".png" ) then
self.Material = Material( self.Material, "nocull" )
else
self.Material = Material( self.Material )
end
end
surface.SetMaterial( self.Material )
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end[/lua]
I'm assuming that a Material is not a string, not so sure about that.
Hmm. I seem to be getting
[code]
[ERROR] addons/pointshop-master(clean skin)/lua/pointshop/items/crosshairs/crosshairtest.lua:20: bad argument #1 to 'SetMaterial' (IMaterial expected, got string)
1. SetMaterial - [C]:-1
2. unknown - addons/pointshop-master(clean skin)/lua/pointshop/items/crosshairs/crosshairtest.lua:20
3. v - addons/pointshop-master(clean skin)/lua/pointshop/sh_init.lua:146
4. unknown - lua/includes/modules/hook.lua:84
[/code]
Dammit remove the "not" in "if not isstring"
Sorry about that
Try changing
[lua]
if not isstring( self.Material ) then
[/lua]
to
[lua]
if isstring( self.Material ) then
[/lua]
Otherwise it's not going to change the variable from a string to an IMaterial.
Unless I'm reading something wrong here.
Ah yes! That worked!
Only issues that remain now are that when going into that section of the pointshop, i get
[code]
[ERROR] lua/vgui/dimage.lua:129: bad argument #1 to 'Material' (string expected, got userdata)
1. Material - [C]:-1
2. SetImage - lua/vgui/dimage.lua:129
3. SetMaterial - lua/vgui/dimagebutton.lua:49
4. SetData - addons/pointshop-master(clean skin)/lua/pointshop/vgui/dpointshopitem.lua:131
5. CreateItemList - addons/pointshop-master(clean skin)/lua/pointshop/vgui/dpointshopmenu.lua:249
6. SetShopTab - addons/pointshop-master(clean skin)/lua/pointshop/vgui/dpointshopmenu.lua:415
7. DoClick - addons/pointshop-master(clean skin)/lua/pointshop/vgui/dpointshopmenu.lua:402
8. unknown - lua/vgui/dlabel.lua:206
[/code]
ITEM.Material might be overridden by pointshop, try giving it another name.
That may be because it uses ITEM.Material as the image for that item, and it's being changed into an IMaterial, although I may be wrong. Try this instead.
[lua]
ITEM.Name = 'Crosshair Test'
ITEM.Price = 0
ITEM.Material = 'crosshairs/rocket.png'
ITEM.CrosshairMaterial = Material( "crosshairs/rocket.png" )
if CLIENT then
function ITEM:HUDPaint( )
if not ( self.CrosshairMaterial ) then return end
surface.SetMaterial( self.CrosshairMaterial )
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end
[/lua]
[lua]ITEM.Name = 'Crosshair Test'
ITEM.Price = 0
local CrossMat = 'crosshairs/rocket.png'
ITEM.Material = CrossMat
if CLIENT then
function ITEM:HUDPaint()
if isstring( CrossMat ) then
if string.find( CrossMat, ".png" ) then
CrossMat = Material( CrossMat, "nocull" )
else
CrossMat = Material( CrossMat )
end
end
surface.SetMaterial( CrossMat )
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end[/lua]
This probably will work better since you'll only have to define it once
[editline]7th October 2014[/editline]
Also, how would one remove the Crosshair on death?
[lua]if not ply:Alive() then return end[/lua] on the first line of the HUDPaint hook
[lua] if ply:team() == Team_Spec then return end [/lua]
Disclaimer:
This is PSEUDO Code. It COULD work, but mostly NOT.
It's just there to give you an idea, not spoonfeeding.
Please Think twice before posting "this doesnt work"
Just check if he is Spectator or Alive.
Yeah if you meant to hide it while the player is dead, you'd do what Internet1001 said.
If you'd want to remove it permanently after death, you may have to do some networking and usage of a variable set on the local player. Or you could use the entity_killed game event which is shared. There's an example on this page.
[url]http://wiki.garrysmod.com/page/gameevent/Listen[/url]
[QUOTE=Internet1001;46175633][lua]if not ply:Alive() then return end[/lua] on the first line of the HUDPaint hook[/QUOTE]
[code][ERROR] addons/pointshop-master(clean skin)/lua/pointshop/items/crosshairs/crosshairtest.lua:8: attempt to index global 'ply' (a nil value)
1. unknown - addons/pointshop-master(clean skin)/lua/pointshop/items/crosshairs/crosshairtest.lua:8
2. v - addons/pointshop-master(clean skin)/lua/pointshop/sh_init.lua:146
3. unknown - lua/includes/modules/hook.lua:84
[/code]
I didn't mean for you to use it as exact code. In this case you'd use LocalPlayer() in the place of ply.
-snip-
Ninja'd
Finished fully working script below:
[lua]ITEM.Name = 'Crosshair Test' --Name
ITEM.Price = 0 --Price
local CrossMat = 'crosshairs/rocket.png' --Crosshair Material
ITEM.Material = CrossMat
if CLIENT then
function ITEM:HUDPaint()
if not LocalPlayer():Alive() then return end
if LocalPlayer():Team() == TEAM_SPEC then return end
if isstring( CrossMat ) then
if string.find( CrossMat, ".png" ) then
CrossMat = Material( CrossMat, "nocull" )
else
CrossMat = Material( CrossMat )
end
end
surface.SetMaterial( CrossMat )
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end[/lua]
Sorry to bring this back again, but there is a pretty big issue with the crosshairs appearing on all players screens if only one person buys it. How would I limit it to only that client?
Add in a
[code]if LocalPlayer()/ply:PS_HasItemEquipped('NAME') then[/code]
check
seem to be getting 'attempt to call method 'PS_HasItemEquipped' (a nil value)' from
[lua]--Edit these!
local CrossMat = "crosshairs/rocket.png"
--Pointshop Vars
ITEM.Price = 0
ITEM.Name = "Crosshair Test"
ITEM.Material = CrossMat
--Crosshair Code
if CLIENT then
if LocalPlayer():PS_HasItemEquipped('crosshairtest') then
function ITEM:HUDPaint()
if not LocalPlayer():Alive() then return end
if LocalPlayer():Team() == TEAM_SPEC then return end
if isstring( CrossMat ) then
if string.find( CrossMat, ".png" ) then
CrossMat = Material( CrossMat, "nocull" )
else
CrossMat = Material( CrossMat )
end
end
surface.SetMaterial( CrossMat )
surface.SetDrawColor(255,255,255)
surface.DrawTexturedRectRotated(ScrW()/2,ScrH()/2,32,32,0)
end
end
end[/lua]
Sorry, you need to Log In to post a reply to this thread.