• Print

Author Topic: I don't understand why this isn't working...  (Read 4830 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
I don't understand why this isn't working...
« on: September 09, 2016, 07:31:46 pm »
I'm a lead dev for a community, and I'm trying to write something really quickly, mainly for other purposes, but it would be nice to have. Basically I have this:
Code: Lua
  1. function ulx.getPos( target_ply )
  2.  
  3.  
  4.    local target_ply_posx, target_ply_posy, target_ply_posz = target_ply:GetPos()
  5.    Msg( "Player Pos = " .. tostring( target_ply_posx ) .. " " .. tostring( target_ply_posy ) .. " " .. tostring( target_ply_posz ) )
  6.  
  7.  
  8. end
  9. local getpos = ulx.command( "Utility", "ulx getpos", ulx.getPos, "!getpos", true, true )
  10. getpos:defaultAccess( ULib.ACCESS_SUPERADMIN )
  11. getpos:addParam{ type=ULib.cmds.PlayerArg, ULib.cmds.optional }
  12. getpos:help( "The vector you are located." )
and I'm not understanding why this is happening:
Code: [Select]
Lua Error: [ERROR] addons/getpos/lua/ulx/modules/sh/getpos.lua:3: Tried to use a NULL entity!
1. GetPos - [C]:-1
  2. call - addons/getpos/lua/ulx/modules/sh/getpos.lua:3
    3. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
      4. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
        5. unknown - lua/includes/modules/concommand.lua:54

What's wrong with it? I'm really tired and can't understand... why is the target (me, in this case) being interpreted as null?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: I don't understand why this isn't working...
« Reply #1 on: September 09, 2016, 08:00:43 pm »
First, target_ply is always going to be the calling player. You forget, ULX first variable passed is always the player calling the command.
Even if you pass it the optional 'player arg' after "ulx getpos <someone>", someone will be ignored.
As for the current error, I'm guessing -
I think GetPos() doesn't return '3' variables x y z.
It returns '1' (table?) with .x , .y, .z inside of it.
You're attempting to assign 3 variables from one command/table.
So you might have better luck in one of two ways
Code: [Select]
local target_ply_pos = target_ply:GetPos()target_ply_pos.x would be x, target_ply_pos.y would be y, and target_ply.z would be z.
Or
Code: [Select]
local target_ply_posx, target_ply_posy, target_ply_posz = target_ply:GetPos().x, target_ply:GetPos().y, target_ply:GetPos().z
That's my guess.
A person more recently involved in lua and Gmod would likely blow my theory away, but, feel free to test while you wait.
« Last Edit: September 09, 2016, 08:03:44 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: I don't understand why this isn't working...
« Reply #2 on: September 10, 2016, 07:00:37 am »
Entity:GetPos() returns a Vector, with number fields x, y, and z. Those are what you want, accessed like Jam said, var.field.

And, yes, don't forget about the order and position of arguments. First one is always the calling player, which is implicit, and the rest are the arguments you need to define, like PlayerArg (target player).
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: I don't understand why this isn't working...
« Reply #3 on: September 10, 2016, 08:05:49 am »
Code: Lua
  1. function ulx.getPos( calling_ply, target_ply )
  2.  
  3.         local target_ply_posx, target_ply_posy, target_ply_posz = target_ply:GetPos().x, target_ply:GetPos().y, target_ply:GetPos().z
  4.         Msg( "Location: " .. target_ply_posx .. " " .. target_ply_posy .. " " .. target_ply_posz .. "." )
  5.  
  6. end
  7. local getpos = ulx.command( "Utility", "ulx getpos", ulx.getPos, "!getpos", true, true )
  8. getpos:defaultAccess( ULib.ACCESS_SUPERADMIN )
  9. getpos:addParam{ type=ULib.cmds.PlayerArg, ULib.cmds.optional }
  10. getpos:help( "The vector you are located." )
using this now... I can call it from the console, but not the client... I know I have the calling_ply variable even though it isn't doing anything in this case, but I mean I guess it works, so I can't complain too much.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: I don't understand why this isn't working...
« Reply #4 on: September 10, 2016, 10:38:32 am »
Is there some reason why I can't use it in-game? I use the command, but no console output, if I use it from the console, it has output.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: I don't understand why this isn't working...
« Reply #5 on: September 10, 2016, 12:36:08 pm »
Because you're using Msg. This prints to the console. If run serverside, this will print to the server's console. The script doesn't know who the calling player is when printing to the console.
I bet if you use it in game, it prints to the server's console still.

Try using this function to print to the player who ran the command's console: http://wiki.garrysmod.com/page/Player/PrintMessage
Take a look at the HUD_Enums for how to get it to print to console instead of chat.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: I don't understand why this isn't working...
« Reply #6 on: September 10, 2016, 12:38:50 pm »
No, I've used it in game and no output to server console. And I tried using PrintMessage before but I was using something different. Ill test in a bit.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: I don't understand why this isn't working...
« Reply #7 on: September 10, 2016, 12:45:29 pm »
Was it in a ULX command that you used Msg? Maybe inside a concommand hook it'll call on the calling player, but it may not work the same inside a ULX command function. I don't actually know for sure.

  • Print