tModLoader Index was outside the bounds of the array

RADICALAdrift

Official Terrarian
Hi there,
I keep getting this "Index was outside the bounds of the array" from my mod to do with my GODKnife Projectile, Originally i assumed there was nothing wrong as the code compiles fine but when i enter a world and use the knife as soon as the projectile hits the floor this "Index was outside the bounds of the array" occurs and im not sure why

Section of code in question

C:
public override bool OnTileCollide(Vector2 oldVelocity)
        {
            {
                Projectile.Kill();
                SoundEngine.PlaySound(2, (int)Projectile.position.X, (int)Projectile.position.Y, 10);
                SoundEngine.PlaySound(2, Style: SoundLoader.GetSoundSlot("Assets/Sounds/Wooo"));
            }
            return false;
        }
Is there something i need to change or not?
Thanks in advance,
Radical
 
Just taking a quick guess, but you are killing the projectile and then trying to play sound based on it's position, but the projectile has been removed already.

Try moving projectile.kill after the 2 playsound lines.
 
Something like that, i could try moving the projectile.kill below the 2 playsound lines though it did work like that in TML 1.3 then again this is TML 1.4 so stuff isn't necessarily going to work the same as it did in 1.3
 
Well as far as i can tell it's something about this line (line 40) in the projectile code it doesn't like yet MVS2022 doesn't tell me there is an issue so i guess it could be related to loading the sound file perhaps (full projectile code below) the sound file is in the right place "Assets/Sounds/Wooo"

C:
using Terraria;
using Terraria.ID;
using Terraria.Audio;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;

namespace OPDefense.Content.Projectiles.Ranged
{
    public class GODKnifeP : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("GOD Knife");
        }
        public override void SetDefaults()
        {
            Projectile.width = 20;
            Projectile.height = 20;
            Projectile.friendly = true;
            Projectile.aiStyle = 2;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.penetrate = 2;
            Projectile.extraUpdates = 1;
            AIType = ProjectileID.ThrowingKnife;
        }
        public override void AI()
        {           
                Projectile.ai[0] += 0.999f;
            if (Projectile.ai[0] >= 500f)
            {
                Projectile.velocity.Y = Projectile.velocity.Y + 0.15f;
                Projectile.velocity.X = Projectile.velocity.X * 0.99f;
            }
        }
        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            {
                Projectile.Kill();
                SoundEngine.PlaySound(2, (int)Projectile.position.X, (int)Projectile.position.Y, 10);
                SoundEngine.PlaySound(2, Style: SoundLoader.GetSoundSlot("Assets/Sounds/Wooo"));
            }
            return false;
        }
    }
}
 
Looking on the discord, someone is doing the same as you but with different code:
SoundEngine.PlaySound(SoundLoader.GetLegacySoundSlot("ModName/Assets/Sounds/Custom/Summon"), player.Center);

Apparently the directory is required to be ModName/Sounds/Custom as well. Also, what format is your sound file in?
 
currently .ogg, originally .wav in my mod for TML 1.3 though i wouldn't have thought that would have made a difference and it's funny you mention "(SoundLoader.GetLegacySoundSlot("ModName/Assets/Sounds/Custom/Summon")" as ive just had a look at the ExampleGun.cs in the Example mod and it uses an identical line for playing a mod sound (SoundLoader.GetLegacySoundSlot(Mod, "Assets/Sounds/PlasmaFire") which i assume would also work? in my case

Yeah turns out thats all i had to do was change "SoundEngine.PlaySound(2, Style: SoundLoader.GetSoundSlot("Assets/Sounds/Wooo"));" to "SoundEngine.PlaySound(SoundLoader.GetLegacySoundSlot(Mod, "Assets/Sounds/Wooo"));" and it now plays the sound even with the sound located in "Assets/Sounds"
 
Last edited:
Back
Top Bottom