PickUp Optimization

Hi, i made a PickUp System, where the player can take the Pickup ONLY if the PickUp is in the CrossHair, but i’m not sure i’ve done it the right way.
Problem is i’m using a LoopFunction and a Cast every Tick, to check which PickUp im targetting.

My blueprint in the detail :

  • In PickUpActor Blueprint : Check if the player is in the PickUp Trigger, if yes, add itself in an array “PickUpInRange”

  • In Character Blueprint : Execute a function, every Tick (explained in the next step)

232122-charactertick.png

  • InPickUpArea (Function)(EveryTicks) : If a PickUp is close to the player, LineTraceByChannel, then if a PickUp is Hit, Loop PickUpInRange to get which PickUp is the TargetedPickUp and then Show the PickUpText (“Drop”) and enable player to loot the PickUp

I heared that Cast nodes can make some problems.
With this system i will have performance issue in the future, right ? How should i optimize this ?

Regards

What exactly do you mean by “target” the pick up? Once the pick up is in the crosshairs? Are you clicking on the pick up? I am not sure exactly what you are trying to do here because the code seems very complex for something as simple as “pick up” object when nearby. Not sure why you even need an array if you are adding and removing the item in that array based off whether or not the player is overlapping it. Is your player going to overlap multiple “pick ups” at once?

An Overlap trigger instead of a tick function might work better for your item pick up area for memory sake.

Thanks it’s awesome! It help me a lot to understand how to script a lot of things

I mean by “target” that the pickup is in the crossair. I will edit the post. Im not clicking, im juste enabling “CanPressEToPickUp”, if true, player can press E to pickup. Im using an array because player can have multiple pickup in is area, i didn’t found other ways to script that…

hey, I’m not sure I understood, using this method, if there are multiple pickups, I could not loot the pickup im targetting ?

Well, i think “Tick” is a bit of overkill for what you are trying to do as Caimartin said, i think creating a “sphere collision” component as an “overlap” trigger volume will create the “in range” effect (not sure if that’s what your code currently is doing by “overlap sphere” but if it is then that’s perfect) Cast once to the player character “on begin play” and save the output as a variable to be used later. Then just hook up the “begin overlap” to a branch with the condition “equal object” and use a reference to your player character (saves you from recasting) if true “Add Timeline”, create an event track and have it execute every 0.2 seconds or so a function that line traces out from your camera and the rest of your code can pretty much be the same. But this way you replace the tick with a Timeline that only executes when the player is within the collision radius, and you only cast once instead of each time the overlap is triggered.

Thanks for your answer, im not sure on how to use Event Track in Timeline, if i use a Set Timer by Function Name, i should get the same result right? But, with this method, if there are multiple PickUps around the player, the timeline (or timer) will be executed multiple times?

Actually, just add a timeline, drag off the “Update” pin and add a delay 0.2 seconds, this will execute the script afterward which should be your line trace function every 0.2 seconds. Double click on the timeline and just check the box for “loop” that is all you need to do. You don’t even have to mess around inside the timeline with event tracks and things, i just tried this out. This code whether you use a timer or a timeline will be executed anytime the player is within the “overlap” of the pick up item. So if the player overlaps multiple items the timeline will run on each object. Just be sure to set the “on end overlap” to “stop” the timeline so it doesn’t continue to execute the line trace if the player is not within the overlap area. As for having multiple pick ups, only the one that the line trace “hits” will be successful the others should return a null output as the character is only looking in one direction he will only face one pick up/line trace successfully to one pick up at a time. I have attached a sample idea of what I am talking about. Let me know if it helps.

Glad I could help!