I want to code a SENT (Scripted Entity) - How2startpl0x
6 replies, posted
New to Lua and coding as a whole, wanting to learn as I go.
[B]Most importantly, [/B]I would appreciate if any of you could explain the basics of creating a SENT. How does everything work?[B]I understand it's sort of like player classes - do you have to define a class for every entity? [/B]How do I set one up? I understand a SENT Lua file needs to be at gamemodes\gamemodename\entities, is this true or can I put it anywhere I want?
Going into a bit more detail, I'm looking to create an entity that has the following functionality:
- Despawns (explodes) after receiving X damage
- When used (pressed E on):
- Rotates into a given angle
- Emits DynamicLight
- Draws glow sprites
- Emits a sound originating from self
[B] - Receives all entities that touch (physically collide) it and:
- Setvars those entities
[/B]Will appreciate all the help I can get. This is only the second script (third overall code) I ever wrote (first script was a simple prop protection system), so please excuse me if I fall behind on your explanations.
Thanks!
[editline]21st January 2015[/editline]
Update: Got the entity to show up on the Q menu. Here's a start.
Use these functions.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage]ENTITY/OnTakeDamage[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ENTITY/Use]ENTITY/Use[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/Create]ents.Create[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ENTITY/PhysicsCollide]ENTITY/PhysicsCollide[/url]
Should be overwritten in the entity (except for ents.Create) for example
[lua]
function ENT:Use()
--do creating of stuff
end
[/lua]
Learn from these files. If you find some code you don't know about, look it up in the wiki.
[url]https://github.com/garrynewman/garrysmod/tree/master/garrysmod/gamemodes/sandbox/entities/entities[/url]
You aren't required to use player class system; but with entities each entity needs a lua file, or folder.
WHERE self = _ent or you reference an entity object that has spawned.
WHERE ENT = actual entity functions you define in the entity file
For rotating look at self:SetAngles
Dynamic Light ( look at ENT:Draw - create one using local _dl = DynamicLight( self:EntIndex( ) ) and modify the settings every x seconds; set up delay so you're not making one every frame although if you include EntIndex it'll just recreate itself )
Sprites, also look at ENT:Draw, or look into effects or Particle Emitters ( pcf )
Sound: use self:EmitSound
Look at ENT:Touch, ENT:StartTouch, etc...
Awesome, thanks.
Right now trying to solve the entity's rotation. Set angles is too fast and sharp, and the physics don't update right when setting them. Any way of rotating the entity more slowly?
Use local _yaw = ( math.sin( CurTime( ) * _speed ) * _height ) % _height; -- where _speed is how fast you want ( 0.001? - 100000... ), and _height is 360
If you have more than one prop, I'd recommend using an offset var added inside of math.sin by using self.RotationalOffset = math.random( 0, _height ); so that other entities using this don't all rotate in unison.
[QUOTE=Acecool;46990948]Use local _yaw = ( math.sin( CurTime( ) * _speed ) * _height ) % _height; -- where _speed is how fast you want ( 0.001? - 100000... ), and _height is 360
If you have more than one prop, I'd recommend using an offset var added inside of math.sin by using self.RotationalOffset = math.random( 0, _height ); so that other entities using this don't all rotate in unison.[/QUOTE]
I ran a quick test per our conversion on Steam and: I'm wrong on the math, sorry. math.sin is perfect for up/down motion because it slows down before the extremes ( -1 and 1 ) and that slowing down which gives the up/down motion a nice smooth transition would look horrible for rotating an object because it wouldn't be consistently smooth in one direction ( although it'd be good if you want something to spin one direction akin to winding up a spring-driven toy because towards the end it will gain more tension and slow down but it'd also need to be inverted for the release )...
For constant rotation you'd actually want constant addition or subtraction while maintaining -180 to 180 range for a valid yaw.
If you're going to use Think hook to update, here's an example using a revised math.rotate available in my dev-base:
[code]//
// Handles repetitive smooth rotational calculations into 1 line without the confusion and normalizes the angle ( -180 )
//
function math.rotate( _speed, _offset )
return ( ( RealTime( ) * ( _speed || 180 ) + ( _offset || 0 ) ) % 360 ) - 180;
end
//
//
//
function ENT:Think( )
if ( CLIENT ) then return; end
self.RotationalOffset = ( !self.RotationalOffset ) && math.random( 0, 360 ) || self.RotationalOffset;
local _yaw = math.rotate( 45, self.RotationalOffset );
self:LocalToWorldAngles( Angle( 0, _yaw, 0 ) );
end[/code]
So, self.RotationalOffset will be nil so the ternary function will grab a value from 0 to 360 at random so that if more than 1 entity is rotating, they won't be in unison.
_yaw calls the function with your speed ( Which I set to 45 degrees per second of rotation ) and the random offset. We then apply an angle to the current angle of the entity ( instead of using local _ang = self:GetAngles( ); _ang.y = _yaw; self:SetAngles( _ang );
the LocalToWorldAngles shortens it for us however it also uses the current yaw of the entity so if it moves the rotational yaw will appear as though it is speeding up when the entity rotates in the direction it is, and slowing down when rotating the other direction; to combat that you would need to use the other method so it rotates without any influence, or you'd need to grab the current value and add or subtract it from the _yaw that is applied.
Sorry, you need to Log In to post a reply to this thread.