![how do you make legacy addons for gmod 13 how do you make legacy addons for gmod 13](https://assets2.rockpapershotgun.com/gar3main.jpg)
This function is called when the entity is first spawned, it acts as a giant loop that will run as long as the NPC exists while ( true ) do - Lets use the above mentioned functions to see if we have/can find a enemy if ( self: HaveEnemy() ) then - Now that we have a enemy, the code in this block will run self. ENT:RunBehaviour() - This is where the meat of our AI is - function ENT: RunBehaviour() Not that bad right? Have a look at the code, I've flooded it with comments so you should know what everything does If there are not any enemies, then walk to a random spot.If there is an enemy then play some animations and run at the player.Check if we have an enemy, if not it will look for one using the above HaveEnemy() function.The "brain" of our botĪs scary as this code may look to some, it is actually pretty simple: Pretty straight forward right? Walk to a random spot and sit idle for 2 seconds before walking to another random spot.Īs nice as this is its not what we want, up next is a much better AI. The function is done here, but will start back at the top of the loop and make the bot walk somewhere else end end Rand( -1, 1 ), 0 ) * 400 ) - Walk to a random place within about 400 units (yielding) self: StartActivity( ACT_IDLE ) - Idle animation coroutine.
![how do you make legacy addons for gmod 13 how do you make legacy addons for gmod 13](https://www.gamemaps.com/imgmgr/limit/img/addons/l4d2/ss/only_mario_survivor__request__24048_0.jpg)
loco:SetDesiredSpeed( 200 ) - Walk speed self: MoveToPos( self: GetPos() + Vector( math. While ( true ) do - Here is the loop, it will run forever self: StartActivity( ACT_WALK ) - Walk animation self. So after your ai has finished running everything it can do, it will go back and do it again. The function is a coroutine, or pretty much a giant looping section of code, except you can pause it for a period of time using coroutine.wait( time ).Ĭoroutines allow you to do things in a timed order, letting you pause the function so we can make the bot face the player or play an animation.Īnd since its all inside a while true loop, it will run for as long as the bot exists. This next part is where most of our AI will be set up.
![how do you make legacy addons for gmod 13 how do you make legacy addons for gmod 13](https://media.moddb.com/images/downloads/1/192/191920/12321313.jpg)
Now that the bot can find enemies we need to get it to actually do something other then just having an enemy. Return true end end - We found nothing so we will set our enemy as nil (nothing) and return false self: SetEnemy( nil) Here we loop through every entity the above search finds and see if it's the one we want for k,v in ipairs( _ents ) do if ( v: IsPlayer() ) then - We found one so lets set it as our enemy and return true self: SetEnemy(v) FindInSphere( self: GetPos(), self.SearchRadius )
![how do you make legacy addons for gmod 13 how do you make legacy addons for gmod 13](https://api.mcpedl.com/storage/submissions/100764/images/jackie-chan-talismans-addon_2.png)
ents.FindInCone() to replicate eyesight local _ents = ents. Search around us for entities - This can be done any way you want eg. If the enemy is dead( we have to check if its a player before we use Alive() ) elseif ( self: GetEnemy():IsPlayer() and !self: GetEnemy():Alive() ) then return self: FindEnemy() - Return false if the search finds nothing end - The enemy is neither too far nor too dead so we can return true return true else - The enemy isn't valid so lets look for a new one return self: FindEnemy()Įnd end - ENT:FindEnemy() - Returns true and sets our enemy if we find one - function ENT: FindEnemy() If our current enemy is valid if ( self: GetEnemy() and IsValid( self: GetEnemy()) ) then - If the enemy is too far if ( self: GetRangeTo( self: GetEnemy():GetPos()) > self.LoseTargetDist ) then - If the enemy is lost then call FindEnemy() to look for a new one - FindEnemy() will return true if an enemy is found, making this function return true return self: FindEnemy() ENT:Get/SetEnemy() - Simple functions used in keeping our enemy saved - function ENT: SetEnemy(ent)Įnd - ENT:HaveEnemy() - Returns true if we have an enemy - function ENT: HaveEnemy()