Your Speel blueprint derives from “SomSpell”?
I wrote two C+±Classes named “SomSpell” and “SomSpellbook” deriving from AActor. Now I want the Spellbook-Blueprint to have a reference to the corresponding Spell-Blueprint.
In the Spellbook-Class I already established a variable for this:
//The Spell that is being learned with the help of this book
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Defaults)
ASomSpell* ResultingSpell;
But in the Blueprint of the Spellbook I can’t choose my Spell-Blueprint. Even drag and drop doesn’t work.
What am I doing wrong?
Thats because there no object related to ASomSpell does not exist, you need to spawn actor first then you will be able to use it and thats only possible in runtime. So insted of using object as a varable use UClass*. UClass is class identifier and it’s generated for each C++ and Blueprint class, you use it then class is needed for argumante, for example in SpawnActor. So make UClass* or:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Defaults)
TSubclassOf<ASomSpell> ResultingSpell
When you use UClass* you will have all classes in engine listed, when you use TSubclassOf which works same as UClass* (actully it’s mask) editor will limit the class selection to specific class relation.
Now from that variable you can spawn specific spell selected in that varable with ->SpawnActor(ResultingSpell) and returning value store in some spell array or whatever in spell book (??? don’t know what system you exacly making :p)
Thanks ! The Subclass-thing works great. I am storing the spawned spell in an array inside the character-class.
Is there a possibility to check if the spell has been learned before I even start learn the book? In this case I have to check the
TArray<ASomSpell*> LearnedSpells
of the already spawned spells against the
TSubclassOf<ASomSpell> ResultingSpell
inside the Spellbook class. The following code doesn’t work:
for (int32 iSpellList = 0; iSpellList < LearnedSpells.Num(); iSpellList++)
{
if (LearnedSpells[iSpellList] == SpellbookItem->ResultingSpell)
{
PrintNotifyMessage(NSLOCTEXT("StruggleOfMages", "SpellAlreadyLearned", "I already know that spell"));
return false;
}
}
Thanks again!
UClass is not the same as object spawned from that class, do you can’t compare those 2. There function in UObject for that called IsA
So do:
if(LearnedSpells[iSpellList].IsA(SpellbookItem->ResultingSpell))
You need to remeber that this function also checks the base classes, so if you input ASomSpelit will always return true in your loop. If you use some spells as a base for other spells you should not use this as it will give you erroneous results. To check if object is really of specific class not any other, you use GetClass() and compire it with some UClass*, like this:
if(LearnedSpells[iSpellList].GetClass() == SpellbookItem->ResultingSpell)
Once again, saves the day. That works like a charm, thanks!