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"

How to cast from a normal Blueprint to a Data Blueprint?

So this is probably a really fundamental question but I've been searching around and experimenting for a couple days and can't figure out how to cast between blueprints that are not the Player Blueprint. I'll attach a diagram below, but basically I'm trying to cast from a Weapon Pickup Blueprint to a Data Blueprint in order to set those variables inside the Pickup Blueprint. The Data Blueprint consists of several structs that categorize the weapon's variables so it's easier to manage when it comes time for them to go to many different places inside the Player Blueprint. From there if the player picks up that Pickup Blueprint, the Player Blueprint casts to the Pickup Blueprint to receive those variables. From there the player can modify those variables, stuff like changing magazine capacity or weapon accuracy. If the player drops that weapon a new weapon pick up is created with the modified variables inside. I hope that makes sense.

I've followed many tutorials on casting most of which have involved casting from the Player Blueprint, which is not my issue, I can do that correctly. So specifically I want to inherit the variables from a Data Blueprint into a Weapon Pickup Blueprint. If I can just get that figured out I should be golden. Do you guys have any solutions for this? A screenshot, or a tutorial, or documentation? Any help would be soooooo awesome. Thanks.

alt text

Product Version: UE 4.18
Tags:
more ▼

asked Jan 03 '18 at 12:24 PM in Blueprint Scripting

avatar image

mikegarn
22 4 5 9

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

1 answer: sort voted first

Hi,

First of all, thank you for providing that figure. It was quite helpful in describing your question. Second, unfortunately, the approach you have in mind is just not possible! Casting is only done on the inheritance chain. What you want is to be able to change the parent of a class during runtime which is just impossible! In the context of object-oriented modeling, once an object is initialized, you cannot change its type nor can you change its inner relation. However, this doesn't mean that you cannot accomplish what you want. All you need is just a little bit of modification in your original diagram. In fact, what you want can be even done in a very clear, neat, and somewhat generic and reusable manner and for that, you need a few key features of object-oriented modeling, specifically inheritance and polymorphism. I have answered very similar questions in the past few days (link1, link2, link3, link4). I highly recommend that you take a look at them to better understand these concepts (you can even think of these questions as examples).

So how would you go about accomplishing this? You would at least need 4 blueprints: 2 for your base classes, and 2 for the children derived from them. They will have the following relationship

BaseWeapon <- Weapon_Type_01

BaseCharacter <- Character_Type_01

The <- sign here means that the operand on the right inherits from the operand on the left (e.g. Weapon_Type_01 inherits from BaseWeapon). I'm gonna stick with these names as they are more generic, though feel free to name your blueprints whatever that makes sense to you. I'm also only gonna focus on the two Base classes as their children can be as unique as you want and that's not really of our concern here. Now, starting with the BaseWeapon, this blueprint would contain base variables and interfaces that are common to all your different types of weapons. You can simply create as many different weapons as you want from this. One interface function that this BaseWeapon probably needs to have is Fire(). Taking advantage of polymorphism, you would implement its common parts such as damaging the player in the BaseWeapon class and the unique part such as spawning a laser in its unique child. Any of your characters can simply call this Fire() method through BaseWeapon and it would automatically link to the appropriate weapon and fire it for you! Definitely refer to those link I provided to better understand how the polymorphism works. Similarly, create a BaseCharacter and base all your different types of characters on this.

Now comes the tricky part that you were doing wrong! Instead of trying to change inheritance relation of your classes, create a variable in your BaseCharacter and make it of type BaseWeapon. Let's name it PickedWeapon. Now whenever your character picks any type of weapon that is based on BaseWeapon, you store it in this variable PickedWeapon. Think about this, what does a character need to know about a gun? -Probably the number of available bullets and magazine capacity. These can be public variables and you can have getters for them so your character can read them. What functions of it does a character need? -Just a fire button to press and fire that weapon (probably a reload function too)! A character doesn't need to know how to process the bullets and fire them. It's weapon's responsibility! And that's the beauty of object-oriented design! Whenever you want to fire, from within any of your unique characters, get this PickedWeapon variable that is inherited from BaseCharacter and call its function Fire(), which has its signature in BaseWeapon and its implementation in the unique children. The polymorphism will take care of it for you and link to the Fire method of the appropriate child weapon!

If you want to drop your weapon, call the drop function you have in your character, play the animation, and set this variable PickedWeapon to null (or None in blueprints terms).

There you go! A beautiful and generic system that would work with any weapon and any character you have that are based on these base classes.

Hope this helps :)

~Vizgin

more ▼

answered Jan 04 '18 at 05:23 PM

avatar image

Vizgin
1.5k 10 10 17

avatar image mikegarn Jan 04 '18 at 06:08 PM

Ohhhh I see! What an interesting way to build this and way more modular than what I had in mind. I'll check out those links tonight and return if I have anymore questions. Thanks so much Vizgin, you're a hero!

avatar image Vizgin Jan 04 '18 at 06:12 PM

You're very welcome.

Please consider marking the question as resolved by checking that ckeckmark next to the answer if it helped resolving your issue. Thanks :-)

avatar image mikegarn Jan 05 '18 at 08:26 PM

I want to make sure we're on the same page. I'm working in Paper2d, so all I need are to set the variables from one blueprint to the Player Blueprint. Animations and weapon functions are all currently stored in the Player BP. I made another diagram to be a bit more specific what I'm trying to accomplish.

alt text

I realize I may still have to do it the way you described, but out of my own curiosity I want try it this way if possible.

avatar image mikegarn Jan 08 '18 at 06:35 PM

Ok I found a video that helped clear up my issue sending variables between Blueprints, it can most certainly be done, but out of simplicity and more control decided to change my method for my weapon system to something more like what Vizgin suggested. Here's a link to the video I watched on Blueprint Communication and a link to the video I'm referencing to build my weapon system. I'm building this sucker in Paper2d so I'm not using skeletal sockets to hold the weapons but going to try and use Child Actor Components within the Player BP, and then set the desired Weapon BP as the Child Actor Class. It seems to be working... so far ;)

Blueprint Communication Tutorial https://www.youtube.com/watch?v=EM_HYqQdToE

Blueprint based Weapon System Tutorial https://www.youtube.com/watch?v=ZcvXm8uDEmk

(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