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"

Break out Part of Character into a Separate Class and Instantiate it as a Blueprint

I have what may be a pretty specific question. I have a character called MyCharacter. I want to create a UObject class called MyCharacterStats. MyCharacterStats will have the characters stats like health, strength and stamina and getters and setters for each stat.

So all the above is fine, now it gets a little tricky. I want to add a

 UPROPERTY(EditDefaultsOnly, Category = Character) 
 class UMyCharacterStats* CharacterStats ;

What I want to do is create a Blueprint instantiation of MyCharacterStats. Then in the editor I can easily assign the MyCharacterStats blueprint to MyCharacter. This will make it easy to create multiple stat load out that we can swap in and out and easily change

Now the problem I am having. The blueprint of MyCharacterStats is a different type than the:

     UPROPERTY(EditDefaultsOnly, Category = Character) 
     class UMyCharacterStats* CharacterStats ;

Which means I can't assign the blueprint to that property in the editor. So I did the following:

 class UMyCharacterStats* CharacterStats 
 
 UPROPERTY(EditDefaultsOnly, Category = Character)
 TSubclassOf<class UMyCharacterStats> CharacterStatsTemplate;

This allowed me to assign the MyCharacterStats blueprint to the UPROPERTY of MyCharacter, in the editor. Now lastly I want to do this:

 CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate);

But when I do this CharacterStats is all NULL. It seems like things aren't being initialized. if anyone has any experience with this or any ideas I would really appreciate the help!

Cheers!

Product Version: UE 4.7
Tags:
more ▼

asked Jun 14 '15 at 11:14 PM in C++ Programming

avatar image

Janyx
497 67 65 212

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

1 answer: sort voted first

I just figured it out. Instead of:

 CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate);

Do this instead:

 CharacterStats = Cast<UArenaCharacterStats>(CharacterStatsTemplate.GetDefaultObject());
more ▼

answered Jun 14 '15 at 11:36 PM

avatar image

Janyx
497 67 65 212

avatar image Janyx Jun 15 '15 at 01:11 AM

This actually isn't correct. This gets you the default object which means when ever you modify a value, you are actually changing the defaults. This won't work.

avatar image Jamie Dale STAFF Jun 15 '15 at 10:32 AM

If you want to have a mutable instance per-character, then you should be able to create one using NewObject since what you have in your CharacterStatsTemplate is essentially a UClass*.

 CharacterStats = NewObject<UArenaCharacterStats>(this, CharacterStatsTemplate);

If you're inside your constructor, you may need to use ObjectInitializer.CreateDefaultSuobject rather than NewObject.

avatar image Janyx Jun 15 '15 at 02:09 PM

I could kiss you! This is exactly what I was looking for, thanks.

avatar image Jamie Dale STAFF Jun 15 '15 at 02:49 PM

Also make sure that your CharacterStats variable is marked as a UPROPERTY. This will prevent the GC system from thinking your newly allocated object is unreachable and deleting it.

(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