青铜和钢铁比黄金和白银要坚硬
看视频固然快,但视频中其实没有书上涉及的点多
1、本节借助Packt Publishing - Intermediate Coding Concepts with Unreal Engine 4巩固Event和Delegate
01)、Event有二,在于重写 NotifyBeginOverlap和NotifyEndOvelap
02)、Delegate有三
1、在GameMode中声明Delegate,原因是GameMode的生命周期长
在WorldSetting中选择GameMode使之生效
DECLARE_DELEGATE(FStandardDelegateSignature)
FStandardDelegateSignature CharacterVisualEffectsDelegateStart;
FStandardDelegateSignature CharacterVisualEffectsDelegateStop;
2、获取GameMode并进行绑定,其中UGameplayStatics这个词记不住
AUnrealScriptingBasicGameMode *MyGameMode = Cast<AUnrealScriptingBasicGameMode>(UGameplayStatics::GetGameMode(TheWorld));
if (MyGameMode != nullptr) {
MyGameMode->CharacterVisualEffectsDelegateStart.BindUObject(this,
&AUnrealScriptingBasicCharacter::makeEFXVisible);
MyGameMode->CharacterVisualEffectsDelegateStop.BindUObject(this,
&AUnrealScriptingBasicCharacter::makeEFXInVisible);
}
3、在EndPlay中解绑定
myGameMode->CharacterVisualEffectsDelegateStart.Unbind();
myGameMode->CharacterVisualEffectsDelegateStop.Unbind();
03)、自定义Event
1、声明
DECLARE_EVENT(APillSpawner,FPlayerEntered)
FPlayerEntered OnPlayerEntered;
2、广播
OnPlayerEntered.Broadcast();//没有智能提示,但编译通过
3、设置TriggerSource
class APillSpawner* TriggerEventSource;
4、获取TriggerEventSource
auto Itr = TActorIterator<APillSpawner>(GetWorld());
TriggerEventSource = *Itr;
5、绑定TriggerEvent
TriggerEventSource->OnPlayerEntered.AddUObject(
this, &AMagicPill::OnTriggerEvent);
6、自定义OnTriggerEventn内容
void AMagicPill::OnTriggerEvent()
{
PillEffect = FMath::RandRange(-150.f, 150.f);
GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Green,
FString::Printf(TEXT("%f is my newValue"), PillEffect));
}
2、本节借助Packt Publishing - Intermediate Coding Concepts with Unreal Engine 4学习Input
01)、Action和Axis Mapping
1、外设交互的手段,在project setting->engine->input中设置
Action Mapping有pressed和released状态
Axis Mapping则允许持续的输入
在代码中绑定相应的动作函数
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAxis("MoveRight", this, &AUnrealScriptingBasicCharacter::MoveRight);
02)、关于Collsion有几点疑问还没有解决,下一节给出理解。
如果是一个场景中的物体,一般具备什么?什么决定了交互,什么决定了它的物理属性?如果要加collision componnet的话那又是什么决定了overlap属性呢等等。
3、再看书本
Chapter06:collision的理解强化
生词:
intersection:横断,横切; 交叉,相交; 交叉点,交叉线; [数] 交集;
There are three classes of intersection for collisions
notifcation:通知; 通知单; 布告; 公布;
Collisions that pass through each other without any notifcation
interpenetration:渗透,互相贯通
Collisions that prevent all interpenetration
1、三种碰撞Class:
Ignore:使得物体互相穿透
overlap:trigger the OnBeginOverlap and OnEndOverlap events
Block:使得物体不能够穿过
2、Actor是可以防止在场景中的,它可以有多个component,有一些component自身具备可以具备碰撞,而碰撞单独也可以作为组件加在Actor身上,这些component的collision通过设置,可以协调工作,例如对于一把剑,剑本身可以是block,all,外部再加一个长方体collision设置overlap,用于拾取操作。相对的,可以分析,吃鸡里面的捡枪,见子弹,地上的枪和子弹都是ovelap的,发生碰撞时可以触发拾取操作。另外西游记中的人参果对地面是ovelap或是ignore的。 这里也说明了一点,对于碰撞,它有自己的形状。另外可以自定义物体对物体之间的碰撞属性。我可以看到你,或者无视你。
理解之后方便再看视频中的collision:
Character.h文件
添加Overlap函数重写和PostInitializeComponents用于绑定
UFUNCTION(BlueprintNativeEvent,Category=Collision)
void OnOverlapBegin(UPrimitiveComponent* comp,
AActor* otherActor,
UPrimitiveComponent* otherComp,
int32 otherBodyIndex,
bool bFromSweep,
const FHitResult& SWeepResult);
virtual void PostInitializeComponents()override;
.c文件:
void AUnrealScriptingBasicCharacter::PostInitializeComponents() {
Super::PostInitializeComponents();
if (RootComponent) {
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AUnrealScriptingBasicCharacter::OnOverlapBegin);
}
}
void AUnrealScriptingBasicCharacter::OnOverlapBegin_Implementation(UPrimitiveComponent* comp, AActor* otherActor, UPrimitiveComponent* otherComp,
int32 otherBodyIndex, bool bFromSweep, const FHitResult& SWeepResult){
AMagicPill* isPill = Cast<AMagicPill>(otherActor);
if (isPill != nullptr) {
if (PickupMode) {
GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Blue,
FString::Printf(TEXT("Colliding with %s"), *otherActor->GetName()));
HP += isPill->getEffect();
isPill->Destroy();
GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Blue,
FString::Printf(TEXT("My Hp is %f"), HP));
HPText->SetText(FText::FromString(FString::Printf(TEXT("HP:%.1f"), HP)));
}}}