x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Checking if overlapping Actors are specific amount and type of Actors

I hope you guys can help me, atm I am trying to build a blueprint prototype for a gameidea contest of ours. Because of a possible game that might spawn out of this, it's a c++ project, but I do the prototyping in Blueprints, so this is a blueprints question. I have tried quite a lot but I fail to understand the logic needed, on paper it's straight forward and should work well, it's just that as my first project I don't know all blueprint notes and I have run into a stone wall with this. - also its been a long workday of staring into the same screen, might have something today with that. I am trying to archive this:

On open craft menu (press “c” key – set up and working) check which actors “collection box” (collision box component part of the character) is overlapping with. Then: if its 2 Twigs set bool lighter to true, if its 2 branches and 30 twigs set bool shelter true, if its 4 branches and 10 twigs set bool fire true.

Then (this part is already set up and working) the craft menu which has already been opened has three buttons for lighter, fire and shelter and if the bool or the object is true it can be placed otherwise the game prints debug “you don't have the resources”.

So basically I am stuck at the check of the overlapping actors, comparing them to the amounts and stetting the booleans accordingly.
I have attached the current setup (yes I am aware atm they all get set to true – thats what I am trying to change ;) )

alt text (After uploading I realized that a) this is the CharacterBP and b) The Hud Ref is connected because I copied this to take the screenshot, it works so no need to point that out )

Additional Info: I know that the overlapping actors part works, I have - in c++ - a setup which I created where the player picks up power ups that change attributes. So I know it works. My 2 Blueprints, BP_Twig and BP_Branch, which I am placing in the level to be used for crafting are both children of a c++ crafting pickups class. That class at the moment only adds a mesh, the children blueprints are untouched (other then the static being added.).

I would highly appreciate the help and if possible posted pictures of the solution, as I do hope to finish this in time. Thank you in advance!

Product Version: UE 4.12
Tags:
more ▼

asked Aug 16 '16 at 01:28 AM in Blueprint Scripting

avatar image

MultiFlagStudios
10 2 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

If you can confirm that the overlapping actors array is properly being populated. I recommend building a map with the results, the key being the actor class and the value being the count.

Since I am completely oblivious to the blueprint nodes available to you (I solely use C++) and you seem savvy enough with blueprints and probably have a basic understanding of pseudo-code, so I'll go ahead and give you a rundown.

 TMap<AActor, int32> ActorCountMap;
 
 foreach AActor in OverlappingActors
     ActorCountMap.FindOrAdd(Actor.GetClass) + 1 // FindOrAdd returns a value reference associated with the key. Add one to it.

At this point you can manipulate the buckets however you want. Do you have 30 sticks?

if (ActorCountMap.Find(StickClass) >= 30) Do Something;

more ▼

answered Aug 16 '16 at 03:34 AM

avatar image

Shohei
1.2k 31 8 40

avatar image MultiFlagStudios Aug 17 '16 at 02:52 PM

Thanks for the reply, yes I do know the basics of code. Can you maybe help me with the actual syntax, I can't find any documentation about the for each loop and also don't quite understand how you get the OverlappingActors in. Following Problem:

How do I actually populate the TMap with the overlapping actors, the TArray that check overlapping actors is in a different functon and not connected so I assume I have to first have a TArray in this function?

     TArray<AActor*> CraftingActors;
     CollectionBox->GetOverlappingActors(CraftingActors);

I know this works - its the same setup as for my pickups. However how do I populate the TMap with the result? I can't find any documentation regarding it.

As for the Blueprints, that will all work now thanks to your help. I will have this function be called via blueprints when i need it and can use the booleans set in it in the Hud Button logic. So all thats missing is the actual population of the TMap and the logic which set the bool true. Thanks again.

avatar image Shohei Aug 17 '16 at 07:54 PM

I was under the impression that you were doing this in blueprints.

You are on the right track. (As always, any provided code is not guaranteed to work - get back to me if it doesn't)

 TArray<AActor*> CraftingActors;
 CollectionBox->GetOverlappingActors(CraftingActors);

 // Depending on how you use this it may be stored as a variable in your header instead of as a local variable.
 TMap<UClass*, int32> CraftingActorBuckets;

 CraftingActorBuckets.Empty();

 for (AActor* Actor : CraftingActors)
 {
     ++(CraftingActorBuckets.FindOrAdd(Actor->GetClass()));
 }

To find out whether you have enough sticks:

 if (CraftingActorBuckets.FindOrAdd(AStickActor::StaticClass()) > NumberOfRequiredSticks)
 {
     DoSomething();
 }

The code above implies that your (for example) stick actor blueprint inherits from a native class AStickActor. If you are using C++ and you need to deal with blueprint classes it is generally a good idea to make your blueprint actor extend from a custom AActor instead of directly from AActor. That way you can do operations like the one above.

You don't have to use the method above. The point I am trying to get at (if you are not keeping track of your item count somewhere) is to create buckets, fill them, them get the bucket you want and count how many are in there to get your final result.

avatar image MultiFlagStudios Aug 18 '16 at 12:59 AM

Yeah sorry for the confusion, it was a Blueprint question but as it turns out populating a TMap in Blueprints is not possible – or so I think.Seeing how I want to learn c++ and this is already a c++ project it all works out well! Thanks to your help I got it working! However (and this is more for future reference for other people) I did have to change the code a bit for it to work. I don't know why but it wouldn't add anything to the Bucket with your code example. I got around it like this:

 // set all crafting bool to default
     SetCraftShelter(false);
     SetCraftLighter(false);
     SetCraftFire(false);
 
     //Get all overlapping Actor and build a Map
     TArray<AActor*> CraftingActors;
     CollectionBox->GetOverlappingActors(CraftingActors);

     TMap<UClass*, int32> CraftingActorBuckets;
     CraftingActorBuckets.Empty();
 
     for (AActor* Actor : CraftingActors)
     {
         // Check if Actor is a Branch
         if (Actor->IsA(ACraftIngredientBranch::StaticClass()))
         {
             // Adds a Branch
 ++(CraftingActorBuckets.FindOrAdd(ACraftIngredientBranch::StaticClass()));
         }
         // Check if Actor is Twig
         if (Actor->IsA(ACraftIngredientTwig::StaticClass()))
         {
             // Adds a Twig
     ++(CraftingActorBuckets.FindOrAdd(ACraftIngredientTwig::StaticClass()));
         }
     }
      // set if lighter can be crafted with overlapping actors
     if (CraftingActorBuckets.FindOrAdd(ACraftIngredientTwig::StaticClass()) >= 2)
     {
         SetCraftLighter(true);
     }


If I have any other items for crafting I will just add another if condition for those later on. Thanks again!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question