UnrealEngine 4 AI (二)C++ 实现简单AI
一、用Unreal Engine 4实现简单的游戏AI:
1. 建立游戏工程
启动UE4编辑器,首先选择New Project,然后选择C++页签,这里创建一个Top Down工程,输入工程名称然后选择创建工程。
2.当创建完成后我们有了一个新的工程,可以试着用播放键运行一下游戏。
3.接下来在Content Browser页签下,展开C++ Classes节点,选中下面的子目录,这里面已经有了三个对象,现在需要新建一个对象。.在这三个对象的空白处点选鼠标右键。
4. 接下来选择New C++Class
5. 先选择右上角Show All Classes,选择了Show All Classes后输入在输入框输入AIController作为父类。
6. 输入类名MonsterAIController,选择Create Class
7 .经过一段时间后在编辑器及Visual Studio工程中我们同时有了MonsterAIController这个类。
8.先修改一下CPPAI.Build.cs这个文件,用下面的代码替换,这个目的是增加一些附加库,避免后面的编译错误
public classRPGGame : ModuleRules
{
public RPGGame(TargetInfo Target)
{
PublicDependencyModuleNames.AddRange(newstring[] {"Core","CoreUObject","Engine","InputCore","AIModule", "GameplayTasks","UMG" });
PrivateDependencyModuleNames.AddRange(newstring[] {"Slate","SlateCore"});
}
}
9.如果没有"AIModule", "GameplayTasks",则会出现下面的编译错误
D:\Dev_Work\ue4\RPGGame\RPGGame\Intermediate\Build\Win64\UE4Editor\Development\UE4Editor-RPGGame-5797.lib D:\Dev_Work\ue4\RPGGame\RPGGame\Intermediate\Build\Win64\UE4Editor\Development\UE4Editor-RPGGame-5797.exp
BTSelectPatrolPoint.cpp.obj : error LNK2001:"public: virtual void __cdeclIGameplayTaskOwnerInterface::OnGameplayTaskActivated(class UGameplayTask &)"(?OnGameplayTaskActivated@IGameplayTaskOwnerInterface@@UEAAXAEAVUGameplayTask@@@Z)
RPGGame.generated.cpp.obj : error LNK2001:"public: virtual void __cdeclIGameplayTaskOwnerInterface::OnGameplayTaskActivated(class UGameplayTask&)" (?OnGameplayTaskActivated@IGameplayTaskOwnerInterface@@UEAAXAEAVUGameplayTask@@@Z)
D:\Dev_Work\ue4\RPGGame\RPGGame\Binaries\Win64\UE4Editor-RPGGame-5797.dll: fatal error LNK1120:
ERROR : UBT error : Failed to produce item:D:\Dev_Work\ue4\RPGGame\RPGGame\Binaries\Win64\UE4Editor-RPGGame-5797.dll
10.接着我们再创建一个类,父类选择Character,然后输入名字MonsterAI
11.在创建一个父类是Target Point的C++类,MonsterTarget,注意要勾选Select All Classes
12.现在我们有了三个新加C++的类,分别是MonsterAI,MonsterPoint,MonsterAIController
13在Content Browser的Content节点下建立名为MonsterAI的目录
14.选择C++ Classes下的CPPAI,选择MonsterAI,按鼠标右键选择Create Blueprint class based on MonsterAI,出现窗口后输入名字BP_MonsterAI并选择存放目录MonsterAI,然后按Create Blueprint Class,创建完成后会跳出BP_MonsterAI编辑窗口,选择存盘并关闭这个子窗口,后面再编辑。