• Print

Author Topic: Lua brush  (Read 12303 times)

0 Members and 1 Guest are viewing this topic.

Offline Tophat Man

  • Newbie
  • *
  • Posts: 17
  • Karma: 2
Lua brush
« on: June 20, 2008, 02:15:09 am »
Hello everyone, I hope I'm welcome here or whatever. I'm from G4P and I'm trying to understand lua, I already have most of the basic stuff like setting variables and making functions and stuff.

anyways I'm making this map and I want to make a lua brush for a certain room that when people enter it, it prints the players name in chat and I also want to have it keep track of how many players are in the brush. I've already tried to make this using the gmod wiki and such but It doesn't work, and there's a lack of coders in g4p that can help me so I came here.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Lua brush
« Reply #1 on: June 20, 2008, 08:24:20 am »
Why didn't you just ask me? =(


I can tell you how to do this very easily.



1. Printing Players Names to chat when they enter the brush.

Create a new entity folder called like "garrysmod/lua/entities/mynewbrush"
Inside this folder create an init.lua file and include the following in the file.
Code: Lua
  1. ENT.Base = "base_brush"
  2. ENT.Type = "brush"
  3.  
  4. function ENT:StartTouch( entity )
  5.         if(entity:IsPlayer() and entity:IsValid() then
  6.                 for _, v in pairs(player.GetAll()) do
  7.                         v:PrintMessage(HUD_PRINTTALK, entity:GetName().. " has entered the lua brush area.")
  8.                 end
  9.         end
  10. end
  11.  

This will print the message every time a player STARTS to touch the brush. It will not display for the SAME player again until the player leaves the brush and returns.


2. How many players are in the brush.

Put this in a lua file that is run at startup. Either in your init.lua file, if you are making a gamemode.. or in a lua file in garrysmod/lua/autorun/server/

Code: Lua
  1.         local numberofplayers = 0
  2.         for _,v in pairs(player.GetAll()) do
  3.                 local pos = v:GetPos()
  4.                 for _,v in pairs (ents.FindInSphere( pos, 15 )) do
  5.                         if v:GetClass() == "mynewbrush" then
  6.                                 numberofplayers = numberofplayers + 1
  7.                         end
  8.                 end
  9.         end
  10.  

Where it says v:GetClass() == "mynewbrush"... obvioulsy mynewbrush is whatever you called your entity from above. With entities, whatever you call the folder that the init file is in is the name of the entity.


also.. I set up a lua help forum on our forum a long time ago JUSt for this type of thing. Myself or B!G would have helped you if you would have just asked =)
« Last Edit: June 20, 2008, 08:26:32 am by MrPresident »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Lua brush
« Reply #2 on: June 20, 2008, 12:23:17 pm »
We don't mind. ;)
Experiencing God's grace one day at a time.

Offline Tophat Man

  • Newbie
  • *
  • Posts: 17
  • Karma: 2
Re: Lua brush
« Reply #3 on: June 20, 2008, 12:51:52 pm »
Yeah pres I was talking to big for a while last night and he sort of taught me basic lua, or atleast answered my questions. And when I was trying to do this no one was on and I just wanted to ask someone. Anyways yeah I did all that except I don't get how pairs() and ipairs() work or what they do really.

EDIT: also isn't there a way I can just add 1 every start touch and subtract one every end touch so I can include it in the entities init? or do I need to make another file in order to access the number it prints or something.
« Last Edit: June 20, 2008, 12:54:37 pm by Tophat Man »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Lua brush
« Reply #4 on: June 20, 2008, 12:59:36 pm »
just put the count code into a function somewhere and call the function whenever you need to know the number.

adding1 and subtracting 1 would Probably work, but Im not sure if it would BE and STAY accurate.

Offline Tophat Man

  • Newbie
  • *
  • Posts: 17
  • Karma: 2
Re: Lua brush
« Reply #5 on: June 20, 2008, 01:21:23 pm »
Ok I'm basically making a game mode. I figure its the easiest way for me at least to understand lua. It's going to be like the sassilization lounge server but more based on mini-games than like making money to play sassilization. So far I have the spawn and lounge parts of the map and I'm using this lua brush for the game room.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua brush
« Reply #6 on: June 20, 2008, 02:47:30 pm »
Why didn't you just ask me? =(
<clip>
Because you scare him. You military men can be so intimidating.

<clip>
Create a new entity folder called like "garrysmod/lua/entities/mynewbrush"
Inside this folder create an init.lua file and include the following in the file.
<clip>
Put this in a lua file that is run at startup. Either in your init.lua file, if you are making a gamemode.. or in a lua file in garrysmod/lua/autorun/server/
<clip>

Though I'm not sure the entities folder would act like any other addon, I strongly recommend using addon format.
gmod/addons/<myaddonname>/lua/entities blah blah
Just place an info.txt file in <myaddonname>.

President has a tendency to like editing straight gmod directories/code at times...but, one day, this will (if not already) bite him in the butt because he'll forget where a change is located that he needs to fix/put back (if overwritten by update).

:P :D

The addons folder is your friend. I recommend using it for any addition.
I've even gone so far as to convert some maps that had extra materials/sounds into addon format, so I wouldn't have to worry/remember where/what stuff came with the map.
« Last Edit: June 20, 2008, 02:49:09 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Tophat Man

  • Newbie
  • *
  • Posts: 17
  • Karma: 2
Re: Lua brush
« Reply #7 on: June 20, 2008, 06:54:34 pm »
Yeah when I have stuff that uses things like sounds or materials I use the addons folder. I already was using it for this, I have some custom textures and stuff.

  • Print