ULX

Author Topic: Lua n00b questions  (Read 18990 times)

0 Members and 2 Guests are viewing this topic.

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Lua n00b questions
« on: December 17, 2012, 03:38:07 PM »
That's right, the official n00b thread!

Here's what I'm dealing with, I want a message to print on the screen when someone is killed, but I don't want it to be in the chat box, I'd rather it be in the middle of the screen.
Here's a code block,

Code: [Select]
function ShowKiller(victim,inflictor,killer)
  victim:ULib.csayDraw("You were been killed by "..killer:Nick()..", a "..killer:GetRoleString().."!")
  --victim:draw.WordBox(8, scrW() /2, ScrH() /2, killer.Nick().." killed you. He was a "..killer.GetRoleString().."!", "ScoreboardText", Color(255,100,100,255), Color(255,255,255,100))
  --victim:PrintMessage(HUD_PRINTTALK, killer:Nick().." killed you! He was a "..killer:GetRoleString())
end
hook.Add("PlayerDeath","killer name and roll",ShowKiller)
There are a couple things commented out,
Code: [Select]
victim:PrintMessage(HUD_PRINTTALK, killer:Nick().." killed you! He was a "..killer:GetRoleString())Is the only one that works correctly without throwing an error.

On the current line of code in use, I'm getting:
Quote
...function argument expected near " ."
« Last Edit: December 17, 2012, 03:47:14 PM by krooks »
My TTT server. Join the fun!

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua n00b questions
« Reply #1 on: December 17, 2012, 07:15:41 PM »
Hmm. What happens if you take out the comma at that ", a" and just, for testing, make it " a"
I'm curious if maybe ULib csay is choking, somehow, even though it's concatenated as part of the 'msg' string, on the comma, expecting the optional color, duration, or fade arguments.
If not ULib, then perhaps Lua itself. (To those more experienced than I, are commas supposed to be /escaped/ out in such a string?)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Re: Lua n00b questions
« Reply #2 on: December 17, 2012, 09:17:39 PM »
I tried both escaping it with "\," and removing it, same error.
My TTT server. Join the fun!

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Lua n00b questions
« Reply #3 on: December 18, 2012, 12:29:50 PM »
The problem is here:
Code: [Select]
victim:draw.WordBox(...)
Recall (or learn!) that the ":" is syntactical sugar which is translated by Lua. For example:
Code: [Select]
ply:DoSomething(a, b)
Is translated to:
Code: [Select]
ply.DoSomething(ply, a, b)
This makes working with classes nicer since you can pass around state in a fashion that isn't insanely verbose. So the error you're getting:

Quote
function argument expected near " ."

Makes sense because the ':' operator must always be followed by a function and then function arguments, where you've got "victim:draw.WordBox".

Another problem here is that draw.WordBox is a client-side function which you're attempting to run server side. SendLua might be what you're looking for in this case.
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua n00b questions
« Reply #4 on: December 18, 2012, 08:38:03 PM »
Meg, that's all fine and dandy, but even I'm confused.
He said
Code: [Select]
victim:ULib.csayDraw("You were been killed by "..killer:Nick()..", a "..killer:GetRoleString().."!") is the line he's currently getting error with..right?
Does csayDraw in ULib use the draw.WorldBox command?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Lua n00b questions
« Reply #5 on: December 19, 2012, 05:17:09 AM »
I missed that first line, ha ha. Even that line isn't going to work for the same reasons. csayDraw is our client side function, but we have an equivalent server side one that should be used here instead.
Experiencing God's grace one day at a time.

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Re: Lua n00b questions
« Reply #6 on: December 19, 2012, 03:01:22 PM »
Thanks for the replies, I will give it a go when I get some spare time here, hopefully tonight.

One thing that I found is as I had the running code (that printed in chat), it actually caused an exploit!
If dieing while holding shift, you would be able to speak to live people while floating around dead. In TTT, that is a big no-no.

Any idea why that might have happened? Did I leave something open or?
I wonder if it will still do this once using csay? Maybe the hud_printtalk is staying open somehow for people to use while dead?
My TTT server. Join the fun!

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Re: Lua n00b questions
« Reply #7 on: December 19, 2012, 04:25:10 PM »
This is an edited post.
So I tried to do this a whole bunch of ways, none worked, then I realized I needed to use SendLua.
Here's what I currently have:
Code: [Select]
victim:SendLua('ULIB.csayDraw(victim, "CLUMSY!")')and I get
Quote
LuaCmd:1: attempt to index global 'ULIB' (a nil value)

I then tried
Code: [Select]
victim:SendLua("function ShowKiller(victim,inflictor,killer)WordBox( 8, scrW() /2, ScrH() /2, \"CLUMSY!\", Color(255,100,100,255), Color(255,255,255,100))end")Which doesnt give me any errors (woo!), but nothing happens.
« Last Edit: December 19, 2012, 05:47:37 PM by krooks »
My TTT server. Join the fun!

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua n00b questions
« Reply #8 on: December 19, 2012, 08:24:33 PM »
Well, first, there is no such thing as ULIB unless you've specifically created it.
(Lua is 100% case sensitive...ULIB is not the same as ULib) :)

And second, for whatever reasons I can't fully now remember, SendLua may not be the best practice.
(Meg/Stickly/Isnipe/others may correct me here).
I thought User messages were better, until I went to look them up on the Gmod wiki and see that 'net' library is now preferred.
Basic theory...have a function on the client side that includes the drawcommand...send a net message from server to client when it's needed.
Or, if the hook you're using is already shared or client, have the client function called. :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Lua n00b questions
« Reply #9 on: December 19, 2012, 09:23:08 PM »
Please use this function instead of csayDraw.
Experiencing God's grace one day at a time.

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Re: Lua n00b questions
« Reply #10 on: December 19, 2012, 10:57:01 PM »
@Magiddo Sorry I didn't notice your post, using csay solved the problem without having to go the SendLua route!

Completed code:
Code: [Select]
function ShowKiller(victim,inflictor,killer)
  if killer:IsPlayer() then
    ULib.csay(victim, "You were been killed by "..killer:Nick()..". A "..killer:GetRoleString().."!")
  elseif not killer:IsPlayer() then
    ULib.csay(victim, "CLUMSY!")
  end
end
hook.Add("PlayerDeath","killer name and roll",ShowKiller)
This will now tell a player who killed them, and what their role was in a TTT game!

I haven't done any intense testing yet, but the bug mentioned above seems to have been solved.

Thanks everyone, learned a lot on this first simple project, I'll keep all my noob questions in this thread when they come up, anyone else is welcome to use this thread, too.
My TTT server. Join the fun!

Offline krooks

  • Sr. Member
  • ****
  • Posts: 382
  • Karma: 32
  • I don't like video games.
    • Diamond Krooks
Re: Lua n00b questions
« Reply #11 on: January 01, 2013, 02:28:28 PM »
Is it possible to target a game function, remove it, then replace it with your own edited version added as an addon in the addon's folder?

I took a look at the sui_scoreboard and it looks to be doing something like this, but I don't fully understand it. I think its a lot more in-depth because that is a complete scoreboard overhaul, when I'm looking to modify just one part of it.

Basically, what I've done is I edited one part of the default scoreboard by adding a new row to player_row.lua. After some trial and error I got it working nicely, but I would rather not have to re-edit the game file every time I update gmod.
My TTT server. Join the fun!

An Error Has Occurred!

array_keys(): Argument #1 ($array) must be of type array, null given