Staff that casts a random spell.

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Staff that casts a random spell.

Post by Emera Nova »

So I'm working on making a staff that when you activate its specialattack it will cast a spell out of it, this spell is undetermined until its time to cast. Below is the code I'm using for this, I'd like to know if you think this would work or not. I feel that where i start the spell = ( is the problem, and will need to be changed to something else for this to work.

Code: Select all

{
			class = "CastSpell",
			name = "cast",
			uiName = "ShockBurst",
			spell = (
			local r = math.random()
				if r < 0.25 then
					spell = "rs_frostburst_2"
				elseif r < 0.5 then
					spell = "rs_poison_cloud_small_2"
				elseif r < 0.75 then
					spell = "rs_shockburst_2"
				else 
					spell = "rs_fireburst"
				end)
			energyCost = 50,
			cooldown = 2.2,
			requirements = { "air_magic", 1, "earth_magic", 1, "water_magic", 1, "fire_magic", 1 },
		},
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Re: Staff that casts a random spell.

Post by Emera Nova »

I was most correct on that being formatted wrong xD grimrock had no idea what the hell I was saying. Any suggestions on how to fix this up so that it randomly fires off 1 of those 4 spells when the power attack is used.
User avatar
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

Re: Staff that casts a random spell.

Post by AndakRainor »

I think a simple way is to define a new spell that casts randomly one of those effects.

Or maybe you can use the function ItemActionComponent.onAttack(self, champion, slot, chainIndex) as CastSpellComponent extends ItemActionComponent
Emera Nova
Posts: 146
Joined: Wed Jun 11, 2014 1:31 am

Re: Staff that casts a random spell.

Post by Emera Nova »

I attempted to make a spell using this

Code: Select all

defineObject{
  name = "catalytic_1",
  uiName = "Catalytic Blast",
  onCast = function(self)
    local r = math.random()
				if r < 0.25 then
					self.go.champion:shootProjectile("rs_frostburst_2")
				elseif r < 0.5 then
					self.go.champion:shootProjectile("rs_poison_cloud_small_2")
				elseif r < 0.75 then
					self.go.monster:shootProjectile("rs_shockburst_2")
				else
					self.go.champion:shootProjectile("rs_fireburst")
				end
			end
}
And the weapon having this

Code: Select all

defineObject{
	name = "rs_staff_catalytic",
	baseObject = "base_item",
	components = {
		{
			class = "Model",
			model = "assets/models/items/acolyte_staff.fbx",
		},
		{
			class = "Item",
			uiName = "Catalytic Staff",
			description = "This staff makes destructive spells more powerful. (Tier 1)",
			gfxIndex = 340,
			gfxIndexPowerAttack = 450,
			impactSound = "impact_blunt",
			weight = 3.5,
			secondaryAction = "cast",
		},
		{
			class = "EquipmentItem",
			slot = "Weapon",
			willpower = 5,
		},
		{
			class = "CastSpell",
			name = "cast",
			uiName = "Catalytic Blast",
			spell = "catalytic_1",
			energyCost = 50,
			cooldown = 2.2,
			requirements = { "air_magic", 1, "earth_magic", 1, "water_magic", 1, "fire_magic", 1 },
		},
	},
} -- tier 5
It's popping up telling me unknown spell or unknown wand spell.
User avatar
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

Re: Staff that casts a random spell.

Post by AndakRainor »

try this (defineSpell is not defineObject) :

Code: Select all

defineSpell{
  name = "catalytic_1",
  uiName = "Catalytic Blast",
  gesture = 0,
  hidden = true,
  onCast = function(champion, x, y, direction, elevation, skillLevel)
    local r = math.random()
    if r < 0.25 then
       champion:castSpell(number) -- replace number with rs_frostburst_2 gesture
    elseif r < 0.5 then
       champion:castSpell(number) -- replace number with rs_poison_cloud_small_2 gesture
    elseif r < 0.75 then
       champion:castSpell(number) -- replace number with rs_shockburst_2 gesture
    else
       champion:castSpell(number) -- replace number with rs_fireburst gesture
    end
  end,
}
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Staff that casts a random spell.

Post by Isaac »

Alternatively... This works, and can be customized to suit.

Code: Select all

      defineObject{
       name = "random_staff",
       baseObject = "shaman_staff",
       components = {
          {
             class = "Item",
             uiName = "Elemental Staff",
             description = "A rune covered staff with a pulsating green gem attached to its tip. You can sense chaotic power in it.",
             gfxIndex = 1,
             gfxIndexPowerAttack = 440,
             impactSound = "impact_blunt",
             weight = 3.3,
             secondaryAction = "random_spell",
          },
          {
                class = "CastSpell",
                name= "random_spell",
                uiName = "Elemental Spell",
                spell = nil,
		charges = 9,
                cooldown = 5,
                energyCost = 25,
                power = 3,
                requirements = { "concentration", 2 },
                onAttack = function(self)
                         local randomSpell = {"ice_shards","fireburst","shock","poison_cloud"}
                         self.go.random_spell:setSpell(randomSpell[math.random(#randomSpell)])
                      end,
          },
       },
    }

Post Reply