Basic Lua

I’m attempting to make some basic lua STOOLs to return the values of attributes of objects the user is pointing at. I have read the majority of the lua tutorial series on the GMod wiki: http://wiki.garrysmod.com/?title=Lua_Tutorial_Series

However this kinda dives right into the general programming syntax of lua as a language (such as loops & conditions). I need help on the specifics for Garry’s Mod scripting, returning the mass of an object for example.

So far I have:


TOOL.Category		= "Danilo's Tools"
TOOL.Name		= "#ObjMass"
TOOL.Command		= nil
TOOL.ConfigName		= ""

function TOOL:LeftClick( trace )

end

All of which I’ve learned from other STOOLs. And I barely even know what any of this means! I can select the tool in game, however stuff like “#gmod_tool_name” still appear on the HUD when selected and I have no idea how to change it. I’ve also read the “Dirty STOOL” guide on the wiki:
http://wiki.garrysmod.com/?title=Basic_STOOL. However this explains little.

Any help would be greatly appreciated, and please excuse me if I have missed anything obvious.

**[PhysObj.GetMass

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=PhysObj.GetMass)**

Think this will help you.

My code is currently:


TOOL.Category		= "Danilo's Tools"
TOOL.Name		= "#ObjMass"
TOOL.Command		= nil
TOOL.ConfigName		= ""

function TOOL:LeftClick( trace )
    local tr = ply:GetEyeTrace()
    if not tr.Entity then return end
    ply:PrintMessage(HUD_PRINTTALK, tr.Entity:GetPhysicsObject():GetMass())
end

Thanks for the help, Flawless, but it still doesn’t work. It says the ply on line 7 is a nil value. And I’m still not sure what much of that means. Also how do you change the name and description of the tool in the HUD when it’s selected?

ply isn’t defined as anything, you need to define it as the owner of the tool, also why are you getting a new trace when the argument passed to LeftClick is a trace?


TOOL.Category		= "Danilo's Tools"
TOOL.Name		= "#ObjMass"
TOOL.Command		= nil
TOOL.ConfigName		= ""

function TOOL:LeftClick( trace )
    if not trace.Entity then return end
    self.Owner:PrintMessage(HUD_PRINTTALK, trace.Entity:GetPhysicsObject():GetMass())
end

Unfortunately that doesn’t work! Says the message to be printed is a nil value. Also, what even is a trace? Still haven’t really found anywhere it explains it clearly…

Then the trace didn’t see anything with a physics object.

A trace is basically a line from point A to point B.

Player:GetEyeTrace( )

nuff said

A ‘trace’, in the terms of game engines, is a physics operation that, in concept, creates a ‘line’ that goes in a single direction until it hits something.
The util.TraceLine function is the GMod Lua function to perform a trace.
Player:GetEyeTrace() is just there for convenience; it performs a trace from the eyes in the direction they are looking.

The first argument to TOOL.LeftClick (which you have called ‘trace’ in your code) is also there for convenience. It is (roughly) the same as doing self.Owner:GetEyeTrace().

In your example, you are attempting to do stuff with the variable ‘ply’.
In your mind, this is obviously the player using the tool. However, if you look at it from a coding perspective, you will notice that ‘ply’ does not exist in this situation (No ‘ply’ local in the function, nor in the file above).
For convenience, you can access the player using the tool through TOOL.Owner.
But, due to a whole load of confusing (higher level) stuff to do with Lua, metatables and Garry’s ability to confuse learners, you cannot access ‘TOOL’ when the function is running.
However, you can use ‘self’ in place of ‘TOOL’ (only within the function).

Here’s a rewrite of the function:
[lua]
function TOOL:LeftClick(trace)
if not trace.Entity then return end – This is just in case the trace doesn’t hit anything. In this case, ‘trace.Entity’ would be nil, and the code below would go KABLAA at this: so you ‘return’ (stop the function running)
local physobj = trace.Entity:GetPhysicsObject()
if physobj then – I did this just in case the entity is a strange one and doesn’t have a physics object!
self.Owner:PrintMessage(HUD_PRINTTALK, physobj:GetMass())
end
end
[/lua]

You may be wondering things, such as “Why is TOOL inaccessible?”, “What does ‘self’ mean?” or “What’s ‘trace’? How is it made, and why does ‘trace.Entity’ work?”, after reading this.

At this stage, try to keep your understandings as basic as possible. Only extend when you need to understand.
For example, you should consider ‘trace.Entity’ as a ‘single term’. Like the fact that ‘if CONDITION then CODE end’ just works, ‘YOUR_TRACE.Entity’ just works.
(Very primitive example, if you understand tables then you probably understand this :P)

Also, a tip: Copy and paste error messages directly as they appear in your console. You’ll have a much better chance of getting a helpful reply.

I frequently read the threads in Newbie Questions. Most of them do not have the slightest bit of care in their requests. Poor grammar and bad spelling implies they don’t give a shit about the people answering the questions, and just want an answer. Of course, I may be judging… but from what I’ve observed, the ones who have lazy original posts do not care in the end (no thanks).

You are one of the few to actually care about the people answering, and not just getting an answer.
(I’m


not

saying that my replies are ‘extremely precious’!)

Good luck, and enjoy programming in GMLua!

(Add me on Steam if you need anything: DecoDaMan)