no i cant get that to work either, any class i try
Error 1 error C2065: ‘AnyClassITry’ : undeclared identifier
Error 2 error C2923: ‘TObjectIterator’ : ‘AnyClassITry’ is not a valid template type argument for parameter ‘T’
could you show an example how to get erm… lets say Pawns for instance
note this is in an actor based class
thanks
That should work, but may throw some errors based on pointers, probably. See if this compiles and let me know about any errors you get - I’m not great at knowing which objects require references yet, heh.
gosh blimey
it works for pawn but not my own pawn class
Error 1 error C2065: 'C_CarPawn' : undeclared identifier C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 2 error C2923: 'TObjectIterator' : 'C_CarPawn' is not a valid template type argument for parameter 'T' C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 3 error C2133: 'Itr' : unknown size C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 4 error C2512: 'TObjectIterator' : no appropriate default constructor available C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 5 error C2451: conditional expression of type 'TObjectIterator' is illegal C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 6 error C2678: binary '++' : no operator found which takes a left-hand operand of type 'TObjectIterator' (or there is no acceptable conversion) C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 11 1 C_Car
Error 7 error C2678: binary '->' : no operator found which takes a left-hand operand of type 'TObjectIterator' (or there is no acceptable conversion) C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 13 1 C_Car
Error 8 error C2039: 'IsA' : is not a member of 'TObjectIterator' C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 13 1 C_Car
Error 9 error C2653: 'C_CarPawn' : is not a class or namespace name C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 13 1 C_Car
any ideas?
edit;
never mind, forgot to add the ‘A’ before the class
argh this unreal stuff is a mess
right so now i can get the pawns in the world but how do i call something on it?
sorry it seems like i need help with every single line of code atm lol
if (Itr->IsA(AC_CarPawn::StaticClass()))
{
//got one now do something with it
Itr->SomeFunction(); //doesnt work
ive had a look at casting but i cant get that to work either, probably doing it wrong
AC_CarPawn* CarP = Cast<AC_CarPawn>(Itr);
CarP->dosomething();
-> does work, not for what im trying to do though. maybe i should start a new thread
im trying to add force
Itr->GetMesh()->Addforce(Direction);
errors
Error 1 error C2027: use of undefined type 'USkeletalMeshComponent' C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 24 1 C_Car
Error 2 error C2227: left of '->Addforce' must point to class/struct/union/generic type C:\UE4Projects\C_Car 4.6\Source\C_Car\Private\Gravity_Actor.cpp 24 1 C_Car
an alternative would be to use APawn::GetGravityDirection()
but it doesnt seem to allow me to override it
thanks for all the help so far, really appreciate it
I haven’t used physics extensively yet in C++ but the errors make it sound like you’re missing an include for something, but I’m not 100% sure what since SkeletalMeshComponent doesn’t have its own header.
thanks , thats where i started, and it still doesnt work
heres the whole code, incase ive done something silly
.h
#pragma once
#include "GameFramework/Actor.h"
#include "Gravity_Actor.generated.h"
/**
*
*/
UCLASS()
class C_CAR_API AGravity_Actor : public AActor
{
GENERATED_BODY()
UPROPERTY(Category = Gravity, EditDefaultsOnly)
float Strength;
virtual void Tick(float Delta) override;
};
.cpp
#include "C_Car.h"
#include "C_CarPawn.h"
#include "Components/SkeletalMeshComponent.h"
#include "PhysicsEngine/BodyInstance.h"
#include "Engine.h"
#include "EngineUtils.h"
#include "Gravity_Actor.h"
void AGravity_Actor::Tick(float Delta)
{
//itterate through all physics actors and addforce -980 * actor mass towards
/*for (TObjectIterator<AC_CarPawn> Itr; Itr; ++Itr)
{
if (Itr->IsA(AC_CarPawn::StaticClass()))
{
// Determine direction vector using Dir = B - A
FVector CarLocation = Itr->GetActorLocation();
FVector MyLocation = GetActorLocation();
FVector Direction = MyLocation - CarLocation;
// Normalize the direction to a unit vector.
Direction.Normalize();
Direction *= Strength * Itr->GetMesh()->GetBodyInstance()->GetBodyMass();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, Itr->GetName());//shows nothing
}
Itr->GetMesh()->SetSimulatePhysics(true);
Itr->GetMesh()->AddForce(Direction);//GetBodyInstance()->//no effect
}
}*/
for (TActorIterator<AC_CarPawn> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, ActorItr->GetName());//shows nothing
}
// Determine direction vector using Dir = B - A
FVector CarLocation = ActorItr->GetActorLocation();
FVector MyLocation = GetActorLocation();
FVector Direction = MyLocation - CarLocation;
// Normalize the direction to a unit vector.
Direction.Normalize();
Direction *= Strength * ActorItr->GetMesh()->GetBodyInstance()->GetBodyMass();
ActorItr->GetMesh()->AddForce(Direction);////no effect
//ActorItr->GetMesh()->GetBodyInstance()->AddForce(Direction);//no effect
}
}