mirror thread: http://www.playuo.org/emu/index.php?threads/help-with-using-list.1329/
-----
I'm working on this code below as part learning and part planned update to my spellbar script.
What I cannot figure out is how to Remove a button from the list.
I've tried a few ways to do it
Any help would be appreciated.
-----
I'm working on this code below as part learning and part planned update to my spellbar script.
What I cannot figure out is how to Remove a button from the list.
I've tried a few ways to do it
and using a foreach statement to go through the list and remove a button, but that caused a crash.list.Remove( new SpellBarButton ( 1, "Spell 1", 1 ));
Any help would be appreciated.
C#:
using System;
using Server;
using Server.Network;
using Server.Gumps;
using Server.Items;
using Server.Mobiles;
using System.Collections.Generic;
using Server.Spells;
using Server.Spells.First;
using Server.Commands;
namespace Server.Gumps
{
public class SpellBarButton
{
public int SpellID{ set; get; }
public string SpellName { set; get; }
public int ButtonID{ set; get; }
public SpellBarButton(int spellID, string spellName, int buttonID)
{
this.SpellID = spellID;
this.SpellName = spellName;
this.ButtonID = buttonID;
}
}
public class SpellBarTest : Gump
{
public static void Initialize()
{
CommandSystem.Register( "spellbartest", AccessLevel.Player, new CommandEventHandler( spellbartest_OnCommand ) );
}
[Usage("spellbartest")]
[Description("test")]
public static void spellbartest_OnCommand( CommandEventArgs e )
{
if (e.Mobile.HasGump(typeof(SpellBarTest)) ); {
e.Mobile.CloseGump(typeof(SpellBarTest));
}
e.Mobile.SendGump( new SpellBarTest(e.Mobile) );
}
public List<SpellBarButton> list = new List<SpellBarButton>();
public static bool HasSpell( Mobile from, int spellID )
{
Spellbook book = Spellbook.Find( from, spellID );
return ( book != null && book.HasSpell( spellID ) );
}
public SpellBarTest( Mobile from ) : base( 0, 0 )
{
Closable=true; Disposable=true; Dragable=true; Resizable=false;
AddPage(0);
AddBackground(0, 0, 400, 100, 9200);
list.Add( new SpellBarButton ( 1, "Spell 1", 1 ));
list.Add( new SpellBarButton ( 2, "Spell 2", 2 ));
list.Add( new SpellBarButton ( 3, "Spell 3", 3 ));
int x = 0;
foreach( SpellBarButton button in list)
{
x+=25;
AddButton( x, 10, 2084, 2084, button.ButtonID, GumpButtonType.Reply, 0);
}
}
public override void OnResponse( NetState sender, RelayInfo info )
{
Mobile from = sender.Mobile;
foreach( SpellBarButton button in list)
{
switch ( button.ButtonID )
{
case 1:
{
if ( HasSpell( from, 0 ) )
new ClumsySpell( from, null ).Cast();
break;
}
case 2:
{
if ( HasSpell( from, 1 ) )
new CreateFoodSpell( from, null ).Cast();
break;
}
case 3:
{
if ( HasSpell( from, 2 ) )
new FeeblemindSpell( from, null ).Cast();
break;
}
}
}
}
}
}