Announcement

Collapse
No announcement yet.

Question about an official tutorial from a C++ dev who is very new to UE -- are closures possible?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Question about an official tutorial from a C++ dev who is very new to UE -- are closures possible?

    I'm going through the official "Blueprint twin stick shooter" tutorial and have a question about events.

    Quick background, since it's not super relevant: I'm a professional C++ dev, but my only gaming background are hobby projects made 100% from scratch. I want to actually get something done, so I'm diving into UE4. I'm starting with blueprints to try and learn the fundamentals of how things are supposed to fit together, and then I'll probably switch to C++. (Side question: is this a good idea, or should I just go straight to C++? so far it seems there's way more "learn UE4" information centered around blueprints, so that's where I decided to start)

    Here's the actual blueprint I'll be referencing:
    https://www.youtube.com/watch?v=RArL...youtu.be&t=620
    Here, when these objects overlap, he stores the "Hero" as a variable, and sets a timer to hit an event repeatedly until the object is no longer overlap a "HeroCharacter" object. While this timer is running, it damages the stored "Hero" variable.

    My question is more theoretical/about "clean code" rather than practical for this project (and maybe worrying about these things is why I can't ever finish a game on my own). Say there were multiple heroes. How I would *want* this to act, is that every hero in range would be damaged their own timer, based on when they came in range of the enemy. How I'm guessing this acts is that when any hero is intersected, it starts the timer. From then on, any subsequent hero entering the intersection will replace the single, member-variable "Hero", and thus will be the only one taking damage from that point on. I'm not sure what happens to the timer, I'm assuming it just gets reset when another hero object intersects (cancels and restarts). What I think *should* happen, is every hero entering should spawn its own timer, and that timer should hit an event that damages *that hero* and that timer should be canceled only when *that hero* is no longer overlapped.

    The obvious solution to me is: Have the "DamageTheHero" event take a "hero" as a parameter, rather than storing a single "hero" as a member. The timer would then schedule a closure with that hero parameter captured, which gets called every n seconds (0.5 in the tutorial). This doesn't seem to be possible, since timers seem like they only work with events that take no parameters. Is there any way to do this in blueprints, or am I trying to stretch blueprints too far? Will this be easy to do if I were to convert this all to C++? Would I be able to create a generic "Timer" blueprint that supports capturing variables to pass to parameterized events?

    For the "EndOverlap" part, ideally on the "BeginOverlap" event I'd be able to say "when this overlap ends, launch x event" where "x" is an event that captures that timer (that I have a local reference to, since I'd still be in "BeginOverlap" when doing the capture) in the "set" event, and when it is triggered it simply cancels the timer. Less ideally, I'd store a map of actor references -> timers, lookup the actor and cancel the timer if it's found (which would mean an expensive map lookup that really does not need to happen). I assume the latter will certianly be possible when I delve into UE C++, but is the former possible?

    #2
    To solve this problem in particular I think you should use C++ as they do here:

    https://answers.unrealengine.com/que...arameters.html

    UE4 timers can support functions with parameters, I don't know why you can't use them in blueprints. I haven't tried it, but I assume you could pass a pointer and that would suit your needs. You also have access to the FTimerHandle through C++ which doesn't appear to be available in blueprint either.

    In my opinion, it's more useful to learn (and implement with) Blueprint than C++. The best way to use C++ is the same way Epic does, by exposing parts of your classes to blueprint (UPROPERTY, UFUNCTION, etc, macros) so that it can be easily implemented and tested in the editor. The only way to know what you want your blueprint implementation to look like is to have experience with blueprint. If you write the entire implementation of your game through C++ you're wasting a lot of functionality of UE4 (and compile time).

    Comment

    Working...
    X