Skip to content

Commit

Permalink
Fix default object crash
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Jul 2, 2024
1 parent 61425de commit 5cd116c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion SurrealEngine/Editor/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ MemoryStreamWriter Exporter::ExportClass(UClass* cls)

text << cls->ScriptText->Text;
text << "\r\ndefaultproperties\r\n{\r\n";
text << ExportObject(cls->GetDefaultObject(), 1, false);
text << ExportObject(cls->GetDefaultObject<UObject>(), 1, false);
return text;
}

Expand Down
2 changes: 1 addition & 1 deletion SurrealEngine/Render/RenderSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void RenderSubsystem::DrawSprite(FSceneNode* frame, UActor* actor)

if (actor->DrawType() == DT_SpriteAnimOnce)
{
float t = (1.0f - actor->LifeSpan() / UObject::Cast<UActor>(actor->Class->GetDefaultObject())->LifeSpan());
float t = (1.0f - actor->LifeSpan() / actor->Class->GetDefaultObject<UActor>()->LifeSpan());
int count = texture->GetAnimTextureCount();
int index = (int)std::floor(clamp(t, 0.0f, 1.0f) * count);
for (int i = 0; i < index; i++)
Expand Down
10 changes: 5 additions & 5 deletions SurrealEngine/UObject/UActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ UActor* UActor::Spawn(UClass* SpawnClass, UActor* SpawnOwner, NameString SpawnTa
vec3 location = SpawnLocation ? *SpawnLocation : Location();
Rotator rotation = SpawnRotation ? *SpawnRotation : Rotation();

float radius = SpawnClass->GetDefaultObject()->GetFloat("CollisionRadius");
float height = SpawnClass->GetDefaultObject()->GetFloat("CollisionHeight");
bool bCollideWorld = SpawnClass->GetDefaultObject()->GetBool("bCollideWorld");
bool bCollideWhenPlacing = SpawnClass->GetDefaultObject()->GetBool("bCollideWhenPlacing");
float radius = SpawnClass->GetDefaultObject<UActor>()->CollisionRadius();
float height = SpawnClass->GetDefaultObject<UActor>()->CollisionHeight();
bool bCollideWorld = SpawnClass->GetDefaultObject<UActor>()->bCollideWorld();
bool bCollideWhenPlacing = SpawnClass->GetDefaultObject<UActor>()->bCollideWhenPlacing();
if (bCollideWorld || bCollideWhenPlacing)
{
auto result = CheckLocation(location, radius, height, bCollideWorld || bCollideWhenPlacing);
Expand Down Expand Up @@ -897,7 +897,7 @@ void UActor::TickInterpolating(float elapsed)
{
pawn->DesiredFlashScale() = mix(target->ScreenFlashScale(), next->ScreenFlashScale(), physAlpha);
pawn->DesiredFlashFog() = mix(target->ScreenFlashFog(), next->ScreenFlashFog(), physAlpha);
pawn->FovAngle() = mix(target->FovModifier(), next->FovModifier(), physAlpha) * Class->GetDefaultObject()->GetFloat("FovAngle");
pawn->FovAngle() = mix(target->FovModifier(), next->FovModifier(), physAlpha) * Class->GetDefaultObject<UPlayerPawn>()->FovAngle();
pawn->FlashScale() = vec3(pawn->DesiredFlashScale());
pawn->FlashFog() = pawn->DesiredFlashFog();
}
Expand Down
8 changes: 7 additions & 1 deletion SurrealEngine/UObject/UClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ class UClass : public UState
void Load(ObjectStream* stream) override;

UProperty* GetProperty(const NameString& name);
UObject* GetDefaultObject() { return this; }

template<typename T>
T* GetDefaultObject()
{
// Note: this is a horrible hack. The object is not actually the type returned, but our property getters are members on that type.
return static_cast<T*>(static_cast<UObject*>(this));
}

void SaveToConfig(PackageManager& packageManager);

Expand Down
4 changes: 2 additions & 2 deletions SurrealEngine/VM/ExpressionEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void ExpressionEvaluator::Expr(DefaultVariableExpression* expr)
if (UObject::TryCast<UClass>(Context))
Result.Value = ExpressionValue::Variable(Context->PropertyData.Data, expr->Variable);
else
Result.Value = ExpressionValue::Variable(Context->Class->GetDefaultObject()->PropertyData.Data, expr->Variable);
Result.Value = ExpressionValue::Variable(Context->Class->GetDefaultObject<UObject>()->PropertyData.Data, expr->Variable);
}

void ExpressionEvaluator::Expr(ReturnExpression* expr)
Expand Down Expand Up @@ -167,7 +167,7 @@ void ExpressionEvaluator::Expr(ClassContextExpression* expr)
UClass* cls = UObject::TryCast<UClass>(object.ToObject());
if (cls)
{
Result = Eval(expr->ContextExpr, Self, cls->GetDefaultObject(), LocalVariables);
Result = Eval(expr->ContextExpr, Self, cls->GetDefaultObject<UObject>(), LocalVariables);
}
else
{
Expand Down

0 comments on commit 5cd116c

Please sign in to comment.