CodeSpells Guide

Spellcrafting Academy Introductory Course: Chapter 2 - From Flares to Fireworks for CodeSpells

Spellcrafting Academy Introductory Course: Chapter 2 – From Flares to Fireworks

Overview

In Chapter 2, we will expand on the basic Flare to build a Fireworks spell which will explode into multiple Flares after a short delay.

Introduction

In CodeSpells, all spells are based around “orbs.” (Flying balls of conjured magic) When a spell is created, a certain amount of mana is assigned to it. This mana is consumed by the spell as it performs tasks. If you create a new spell and don’t change anything, that spell will conjure an orb that simply flies forward in the direction you are looking. Once the assigned mana is used up, the orb fizzles out of existence and the spell ends. However, because no mana is used unless you activate a power, it will fly forever until we tell it to destroy itself.

Flare – The simplest spell


This spell is super simple and was the topic of Chapter 1. By default, when a spell is cast, the orb flies in the direction the player is looking and will continue in that direction until the assigned mana runs out (indicated by the green arrow). In this case, I have assigned 2 Earth Mana to this spell which means that it will last about 2 seconds until the orb fizzles out and disappears. It does nothing else.

Try creating and testing this spell now.

Fireworks – Flies fast, then explodes into numerous Flares

Spell orbs also have the ability to cast other spells. When building the logic, you can use the “Cast Spell” block to fire off other spell orbs with their own custom spell behavior and a specified mana quantity, which is taken from the base spell’s remaining mana. This causes the main spell to fizzle out sooner as mana is consumed more quickly.

Here are the contents of the Fireworks spell:


There are a few phases that a spell goes through called events.

  • Create – when the spell is initially casted by the player (or another spell)
  • Activate – when the player pushes the activate button, all spells that use an activate event are notified and this section fires. Fireworks doesn’t use this, but it can be fun for setting up chain reactions that are executed with the push of a button
  • Hit – when the spell orb collides with an object or the ground, this event is is executed. Fireworks doesn’t use this but it can be used to do things that interact with other objects, such as to get a critter to be pulled along by the orb as it moves.

As you can see in the spell image, only On Create is used. It has two blocks in it:

  • First, it sets the speed of the orb to “fast.”
  • Next, it sets up a call to a function called “explode” after the orb has been flying for 0.7 seconds. The number in this block is set to be a number that doesn’t change, but you can explore the “Math” category to try changing that little number block it to other blocks for various effect. A function is basically a self-contained chunk of logic that can be called and reused as desired. Breaking a spell up into functions can make it much easier to manage and understand.

Apart, from the On Create block is a function block, which I have named “explode.” This is the main effect of the Fireworks spell, executed by On Create‘s “Call Function” block exactly 0.7 seconds after the spell is casted. The explode function has a couple of actions:

  • The green block is called a “loop”, which causes the contained blocks to execute a specific number of times. In this case, the following two steps happen 20 times in a row:
    • The main orb for this spell is rotated to face a random direction.
    • Then a new orb is cast in the direction the Fireworks orb is now facing. It is assigned the spell named “Flare” (as above) and given a mana of 2 (so that each flare lasts about 2 seconds in the sky) from the amount assigned to the Fireworks spell.
  • After the loop is complete and all the flares are fired, the orb for the fireworks spell is destroyed so that it doesn’t linger in the sky.

The reason for setting a random rotation in the loop is that when a spell is casted, it always goes in the direction the caster is facing (whether the caster is a player or a spell orb from another spell). So rotating the orb in the loop caused the flares to go in random directions from each other rather than all in the same direction. Try moving the rotation block to before loop and observe what happens.

I chose 41 as the mana quantity for the fireworks spell to ensure it has sufficient quantity to linger in the sky and portion it out to each flares that is fired. 20 flares given 2 mana each = 40 mana plus one mana for the time the orb travels through the sky (0.7 seconds) before exploding. If you modify the number of flares sent out, remember to adjust the Fireworks spell mana accordingly.

That’s about it for this first simple spell. I hope my explanation of what is happening is clear and concise!

If anyone has any comments, questions, or feedback, please let me know!

SteamSolo.com