Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

In Unreal engine i have some objects with functions array which created in runtime and…

  1. User

    In Unreal engine i have some objects with functions array which created in runtime and spawned by server on begin play. Clients will wait by set of a timer on next tick when server spawn them, when array become non empty we stop set timer on next tick and populate array with functions, but if we do nothing on begin play timer will be called every fram till end of the game and if we want to add new function this will cause that everyone spawn their own object alongside with replicated object. So when i call NetMulticast function which call execute of all function in array but client create it's own object so replicated object is empty, what to do in this situation and how i can get rid of infinite set of timer? There is part of my code:

    TryBinding in begin play: void AActorOfItem::TryBindingEvents() { if (HasAuthority()||_InteractControllerComponent->GetNumReplicatedHandlers()>0) { BindingEvents(); } else{ GetWorld()->GetTimerManager().SetTimerForNextTick(this, &AActorOfItem::TryBindingEvents); } } binding function (which may not called in BindingEvents if we doesn't bind anything): void USV_InteractControllerComponent::ReceiveBindOnStartInteractive(FPathToScenarioObject TargetSlot, const FSV_SimpleStorageInteractEventDelegate& Event, FSV_NetExecutionType NetType) { GetOrSpawnBinddingHandler(TargetSlot)->BindOnStartInteractive(Event, NetType); } GetOrSpawnBinddingHandler part where he is spawning: USV_HandlerForBindingComponent* FindedHandler = NewObject<USV_HandlerForBindingComponent>(this, USV_HandlerForBindingComponent::StaticClass(), NAME_None, RF_Transactional | RF_Public); AddReplicatedSubObject(FindedHandler); FindedHandler->SetTargetScenarioName(TargetSlot); ReplicatedHandlers.Add(FindedHandler); AddHandler(FindedHandler); return FindedHandler;

  2. Assistant

    It looks like you're facing an issue where you're trying to ensure that objects are initialized and functions are bound correctly across server and clients in Unreal Engine, particularly when dealing with replicated objects and functions. The use of timers to check and attempt to bind events might be causing overhead and unintended side effects like creating additional objects. Let's break down potential solutions for your scenario:

    Avoid Using Infinite Timers

    Using a timer that continuously checks and tries to bind every tick is not efficient and can lead to a state where game logic becomes unpredictable. Instead, consider these approaches:

    1. Event-Driven Initialization:

      • Utilize Unreal’s replication notifications and events more effectively. For instance, use the OnRep functions to ensure that when replicated properties update, you handle initialization logic there rather than relying on a timer.
    2. Replication Complete Check:

      • Implement a mechanism that determines when your replication process is complete before trying to bind events. Unreal Engine's BeginPlay combined with RepNotify properties is often enough to know when the object’s state is ready.
    3. Conditional Binding Logic:

      • Modify your TryBindingEvents function so that it triggers only under specific conditions, such as certain events or a specific replication state, instead of trying every tick.

    Adjusting the Binding Logic

    Here’s a refined approach:

    void AActorOfItem::OnReplicatedHandlerReady()
    {
        if (HasAuthority() || _InteractControllerComponent->GetNumReplicatedHandlers() > 0)
        {
            BindingEvents();
        }
    }
    
    void USV_InteractControllerComponent::ReceiveBindOnStartInteractive(FPathToScenarioObject TargetSlot, const FSV_SimpleStorageInteractEventDelegate& Event, FSV_NetExecutionType NetType)
    {
        USV_HandlerForBindingComponent* Handler = GetOrSpawnBindingHandler(TargetSlot);
        if (Handler)
        {
            Handler->BindOnStartInteractive(Event, NetType);
        }
    }
    
    • OnReplicatedHandlerReady: Here, you could mark this function with a replication callback (OnRep) and call it when handlers are available.
    • Network Interactions: Make use of multicast or reliable server functions to ensure that events happen at the appropriate lifecycle stage of object initialization.

    Handle Custom Object Creation

    It’s crucial to ensure that clients do not create duplicated objects unless necessary. You might want to check how objects are spawned and make sure they are only spawned on the server-side or when specific conditions are met.

    • Authority Checks: Always check whether the current execution context is the server (HasAuthority() should return true).
    • Role Management: Consider using GetLocalRole() and GetRemoteRole() for finer control over logic execution.

    Test and Debug

    Run extensive testing in both networked and standalone environments to ensure that all functions are bound and entities are appropriately replicated. Utilize UE4's networking profiling tools to observe the pattern and flow of replication and event triggering for further insights.

    By organizing and optimizing your binding logic and handling replication with network events and cleaner state checks, you should be able to remove the costly infinite timer and ensure more robust management of object and function replication.