- Home /
Best approach for spell book and spell bar (using composition)
Hi,
I’m relying on composition to built spell prefabs that are Canvas buttons with scripts attached that define behaviours, stats, requirements and so on.
Now I want to collect all spells in a spell book and allow player to drag some of the spells form the book onto a spell bar to be able to use them them during gameplay.
In the spell book:
spells should not be clickable and not launch the actual spell
spells display the spell icon and a description
all available spells are listed
spells can be filtered and searched
In the spell bar:
spells are clickable to actually cast the spell
spells can be dragged out to be deleted and can be replaced by other spells
spells don't display the description only the icon
Equipping a spell:
spells can be dragged from the spell book to the spell bar
dragging a spell doesn’t not remove it from the spell book, players drag a copy of the spell
Questions
Should I use the same prefab in both the spell book and spell bar, and disable/enable functionalities depending on if they are in the spell book or spell bar?
Or should I have two different prefabs: one for the spell book and one for the spell bar? They should be linked somehow?
Also should I instantiate the spells in the spell bar or just activate/deactive them?
Or should I use a different approach?
Thanks p.
Since you seem to have completely different UI interactions for the two I don't see why you can't use the same prefab/class. In the spell book you only use EventTrigger.OnDrag() and in the spell bar you only use EventTrigger.OnPointerClick().
Thanks. So it would be: if the parent is the spell book canvas, then allow the drag, if the parent is the spell bar canvas, then allow the click? Also I'd make a new instance of the prefab to copy it form the spell book into the spell bar and destroy it when dragging out the spell bar?
I think like this: You have a List of spells that your character knows (it might even be the entire database of spells depending on what you want to do). In the spellbook-gameobject: Iterate over the list --> Instantiate spellbook_spellPrefab. Set the public Spell mySpell of this prefab to your specific spell. The prefab should change it's layout, e.g. text and sprite to whats specified by the spell-class. This prefab has a EventTrigger.OnDrag(). OnDragStop()--> if mouse is over a spellbar slot, add the spell-class to the spellbar by instantiating a spellBar_spellPrefab. Set the public Spell mySpell of this new prefab to the spell. spellBar_spellPrefab has an EventTrigger.OnClick() that will call mySpell.Cast().