Making doors open and stuff with lua


I’m wondering if I can make a map in hammer with a door and a tracktrain and then activate them through code,

Like set the tracktrains speed and open up the door when a timer reaches 0.

(User was banned for this post ("Wrong section" - Gran PC))

timer.Simple( DELAY , function() MY_DOOR:Fire( “Open” , 0 ) end )

Seems simple enough, but I am not sure how to establish the connection between hamer editor and lua.

Like in hammer I would have a door with a certain name, such as myDoor. Then how would I search the map for myDoor and then do a Fire on it? I just don’t understand how you got the MY_DOOR part.

ents.FindByName(“myDoor”)[1]:Fire(“Open”, 0);

Thank you.

Firstly, I didn’t mean to rate you dumb, but it just so happens that the dumb rating is right next to the reply button. I’ll try and see if I can change that rating

Back on topic. MY_DOOR is just a variable. In Lua you can have many types of variables, notably, entities. As the user above as posted, you can use the following code to find your door

MY_DOOR = ents.FindByName(“myDoor”)[1]

so in basic English, what I am telling the engine to do is, Find every entity that has “myDoor” as it’s name. In hammer you can set the name of entities, and this is one of many links we have with hammer. This function ( ents.FindByName ) returns an array of entities that it has found ( if any ). the “[1]” tells the engine to use the first one ( It’s a tad more complicated, its more about getting the entry of the table having 1 as it’s key, but that’s the more complicated explination).

Now that we have your door, we can use a function on it called Fire. In hammer you have inputs and outputs, in Lua, we have this function. We fire the input called “Open” with no flags.

If you were to call that, it would work, but you are asking for something with a delay. For delays, we have timers. the most simple timer is timer.Simple
timer.Simple has 2 arguments : Delay, and function. In other words, once the delay has passed, it is going to call the function. We can then do something like this:

timer.Simple( 5 , function() ents.FindByName(“myDoor”)[1]:Fire(“Open”, 0); end)

you can see we have the previous code, inside the function argument. This function will be called after 5 seconds. Once you have worked out the delays you need for the doors to open when you need them too, this could look pretty awesome.

To close the doors afterwards, you would use ( I’m not sure ) “Close” instead of “Open” in the Fire function.

I hope this helped.

It helped very much. Thank you.

I may come off as “dumb” but I’m just new to lua. I do stuff in Java and a bit of C#, but just wanted to experiment a bit with Lua since I see all these awesome garrys mod gamemodes.

I knew that MY_DOOR is a variable, of course, I just wanted to know how you got the variable in the first place.

Thank you once again.