• Making a bot move in a random direction
    8 replies, posted
[CODE] local function ControlBots(ply, cmd) --Ignore real players if not ply:IsBot() then return end --Get the current keys pressed (probably none since they are a bot) local KeysPressed = cmd:GetButtons() --Add your keys into the mix KeysPressed = bit.bor( KeysPressed, IN_FORWARD) --Apply the keys to the movecommand cmd:SetButtons(KeysPressed) end hook.Add("StartCommand","MyBotController",ControlBots)[/CODE] Someone showed me this code for making bots move, but I have some questions. 1. How would I make the bot move in a random direction via table.Random? 2. How would I make this affect a bot individually, not every bot on the server? (Note: I'm talking about the bots created via the "bot" concommand). 3. How would I call the function upon the bot being hurt?
1> IN_FORWARD is one of the movement enums. A list of these enums is located here: [url]http://wiki.garrysmod.com/page/Enums/IN[/url] You could have a table with these enums (Be sure to use the enums, not the values of the enums as those may change during Gmod updates) and choose a random enum with the table.Random. 2> Taking a look at this resource ([url]http://wiki.garrysmod.com/page/GM/StartCommand[/url]) we can see that what we do is change the input of the player (Bot is derived from player) before it's processed by the server. First we check if the player is a bot (if not ply:IsBot() then return end). You can make a table of bots and their actions (Or make your own class with its own metatable, whatever you are into) and control them individually using the enums I linked you to in 1. 3> As I said in the second answer, bots are derived from players. They are treated as players on the server. You can check for this hook: [url]http://wiki.garrysmod.com/page/GM/EntityTakeDamage[/url] and see if the targeted entity is a bot. I'd love to help you out with your problems further, just add me on Steam! Username: techdaan
Well, number 2 is saying "This code affects every player that is a bot when it is called. What I want it to do is affect the single bot that is shot, not every bot on the server upon a bot being shot, which is when I'll call the hook.
[QUOTE=A Fghtr Pilot;47658535]Well, number 2 is saying "This code affects every player that is a bot when it is called. What I want it to do is affect the single bot that is shot, not every bot on the server upon a bot being shot, which is when I'll call the hook.[/QUOTE] Could you add my Steam so we can talk more easily? Kind of hard to explain to me [editline]4th May 2015[/editline] [QUOTE=A Fghtr Pilot;47658535]Well, number 2 is saying "This code affects every player that is a bot when it is called. What I want it to do is affect the single bot that is shot, not every bot on the server upon a bot being shot, which is when I'll call the hook.[/QUOTE] To extend my answer: The code there runs for every player. As of now it has no checks on what it targets. It targets every bot out there. Bots each have their own name (Bot01, Bot02, Bot03). You can select what bot you want to control by a name check for example.
Hmm. Well, bot names go on forever(as in if someone added enough bots their could be a bot9999 or something like that). Is there a way to signify 01 - infinity in Lua? [editline]Oh, wait, I could check to see if the player has "Bot" in their name, right? I've never done something like that, though.[/editline] Also, I will add you, but first I gotta get home(I'm typing this on a mobile device).
[QUOTE=A Fghtr Pilot;47658953]Hmm. Well, bot names go on forever(as in if someone added enough bots their could be a bot9999 or something like that). Is there a way to signify 01 - infinity in Lua? [editline]Oh, wait, I could check to see if the player has "Bot" in their name, right? I've never done something like that, though.[/editline] Also, I will add you, but first I gotta get home(I'm typing this on a mobile device).[/QUOTE] I would personally make a database with every bot listed, listen to the player join and player leave events, then register and unregister. From that database (Basically a table) you could pull out the bots and store actions in their table. You can then call the bot from the GM:StartCommand input and handle the actions per-bot =)
I sent you a friend invite. I'm Von Kaiser(AKA A Fighter Pilot).
[QUOTE=A Fghtr Pilot;47657762]1. How would I make the bot move in a random direction via table.Random?[/QUOTE] 1. I think you would make the bot move to a random vectors instead of make the bot wander on random direction (it will look weird anyways, you will notice later this problem when you want to make advanced bots). If this is the case, then, you could set the bot's eye angles to the vectors's angles (and sure, have IN_FORWARD active). The cons by using this method is you will get the bot looking at the direction weirdly (depending of vectors's angles) and have spam in the console of incorrect eye angles positions (I think robotboy655 fixed recently this problem or in dev version only ?) and you will have to use a pathfinding system for the bot (or make one) or expect some unexpected problems in some cases. (props/wall/players/cars/moving things/buildings/...).
[url]http://wiki.garrysmod.com/page/Global/VectorRand[/url]
Sorry, you need to Log In to post a reply to this thread.