Scripting Refire

Avatar
Goldenknighttim
182 Posts
Posted Jul 15, 2014
Hey guys,

I'm pretty new to writing scripts, but I have one in mind that I think would be pretty cool. For this script, I want to keep a variable up to date on the position of an object. My current plan is to write the script in a way that expects a logic_timer to continually run the script. However, I would rather do as much of this in the actual script as possible. Is there a way to get a script to refire at a steady interval by only using the script itself?

Advertisement
Registered users don't see ads! Register now!
Avatar
FelixGriffin
2,680 Posts
Posted Jul 15, 2014
Replied 1 hour later

There is, in fact. VALVe used something called a "think function" in their scripts, but the following does the same thing and is rather easier to work with.

function refire(){
    EntFireByHandle(self,"RunScriptCode","refire();",0.1,null,null);
}
Avatar
Goldenknighttim
182 Posts
Posted Jul 15, 2014
Replied 58 minutes later
Lol, that's brilliant. Thanks.

I have another question though. In the same script, I want to get an object to move forward. There is a SetVelocity() function. How do I deturmine whether the specified vector is local to the entity, or a global vector?

Avatar
FelixGriffin
2,680 Posts
Posted Jul 15, 2014
Replied 12 hours later
Velocity is global, but you can get the Euler angles of the entity and use them to make a rotation matrix.
Avatar
Goldenknighttim
182 Posts
Posted Jul 16, 2014
Replied 7 hours later
Thanks Felix, but all I got from that was that velocity is always global. That's fine though, I've got another idea for moving an object in a locally oriented direction. It's based on the assumption that SetOrigin() can be used to move an object around. I'll test that out later this morning.
Avatar
FelixGriffin
2,680 Posts
Posted Jul 16, 2014
Replied 2 hours later

Goldenknighttim wrote:
Thanks Felix, but all I got from that was that velocity is always global. That's fine though, I've got another idea for moving an object in a locally oriented direction. It's based on the assumption that SetOrigin() can be used to move an object around. I'll test that out later this morning.

You can use SetOrigin, but it'll be jerky and can put the object inside walls. Changing velocity is better.

Avatar
Goldenknighttim
182 Posts
Posted Jul 16, 2014
Replied 59 minutes later
I did use SetOrigin() this morning, and that's exactly what happened. But I found that I could make it smoother by setting the refire rate to a higher rate, and I plan to use the TraceLine function to tell whether it runs into a wall, and if there is ground beneath it. Unfortunately, setvelocity does not work at all. I'm using a pedistal button entity for testing. I'm guessing that setvelocity only works with a few entities.
Avatar
FelixGriffin
2,680 Posts
Posted Jul 16, 2014
Replied 12 hours later

Goldenknighttim wrote:
I did use SetOrigin() this morning, and that's exactly what happened. But I found that I could make it smoother by setting the refire rate to a higher rate, and I plan to use the TraceLine function to tell whether it runs into a wall, and if there is ground beneath it. Unfortunately, setvelocity does not work at all. I'm using a pedistal button entity for testing. I'm guessing that setvelocity only works with a few entities.

TraceLine is glitchy and only detects world brushes (as well as one particular brush entity, but that's another bug).

SetVelocity only works on things which can move. Pedestal buttons cannot unless they are parented.

Avatar
Goldenknighttim
182 Posts
Posted Jul 17, 2014
Replied 7 hours later
Ok, so an object parented to an invisible func_physbox would work with SetVelocity?

I already finished the code, but as it is, it would go through other entities, so some editing would help.

Avatar
FelixGriffin
2,680 Posts
Posted Jul 17, 2014
Replied 3 hours later
SetVelocity will work on any object which is physically simulated and thus can have velocity. So yes, a func_physbox would work.
Avatar
Goldenknighttim
182 Posts
Posted Jul 17, 2014
Replied 1 hour later
Ok, I'll mess with that this evening. Thanks for your help Felix. I'm a bit afraid of the object tipping over though. . I'm sure I can find a way to stop that from happening.
Avatar
TeamSpen210
608 Posts
Posted Jul 17, 2014
Replied 3 hours later
Using a phys_keepupright will make it right itself.
Avatar
Goldenknighttim
182 Posts
Posted Jul 17, 2014
Replied 46 minutes later
Thanks, That will be useful. While on the thought of using entities and scripting together, Does anyone know if there is a way to use scripts to create an entity?
Avatar
FelixGriffin
2,680 Posts
Posted Jul 17, 2014
Replied 5 hours later
local ent = Entities.CreateByClassname("game_text");
Avatar
TeamSpen210
608 Posts
Posted Jul 17, 2014
Replied 40 minutes later
There's also special functions for env_entity_maker entities to spawn the template at a specified locations and angle. You might want to use EntFireByHandle with the AddOutput input to change keyvalues and add outputs if needed. (Unless there' some better way to do this, I haven't seen one though). There's also ConnectOutput/DisconnectOutput, which sets up a RunScriptCode input pointing to a function in your script.
Advertisement
Registered users don't see ads! Register now!
Avatar
Goldenknighttim
182 Posts
Posted Jul 18, 2014
Replied 12 hours later
Thanks, I'll experiment with those in my next script. As for using SetVelocity() on the physbox, it seems like the motion from the physics is overiding anything from the script. It stays in one spot while I'm not hitting it, but when I jump on it, it throws me in the direction that the velocity is set.