Code examples
I am looking at the UltimaSDK for a couple ideas and having never seriously examined it before I was particularly interested in seeing some code examples. The following are the posts I found in this forum which offer code snippets for which no one protested. I make no claims for accuracy or suitability or versioning, I only gathered these posts together. (Primarily for my own benefit.)
If any veteran users have working code examples for simple or not-so-simple tasks to offer new users, please post them here.
I am looking at the UltimaSDK for a couple ideas and having never seriously examined it before I was particularly interested in seeing some code examples. The following are the posts I found in this forum which offer code snippets for which no one protested. I make no claims for accuracy or suitability or versioning, I only gathered these posts together. (Primarily for my own benefit.)
If any veteran users have working code examples for simple or not-so-simple tasks to offer new users, please post them here.
rsmiller21;621050 said:I have been working on a program dealing with the new Ultima.dll here and wanted to post a few code snippets.
Playing the sound:
Code:public static void PlaySound(MemoryStream ms) { System.Media.SoundPlayer sp = new System.Media.SoundPlayer(); sp.Stream = ms; sp.Play(); }
Get sound file name with the sound id:
Code:public static string GetSoundName(int soundID) { if (soundID < 0) { return null; } m_Index.BaseStream.Seek((long)(soundID * 12), SeekOrigin.Begin); int offset = m_Index.ReadInt32(); int length = m_Index.ReadInt32(); int extra = m_Index.ReadInt32(); if ((offset < 0) || (length <= 0)) { if (!m_Translations.TryGetValue(soundID, out soundID)) { return null; } m_Index.BaseStream.Seek((long)(soundID * 12), SeekOrigin.Begin); offset = m_Index.ReadInt32(); length = m_Index.ReadInt32(); extra = m_Index.ReadInt32(); } if ((offset < 0) || (length <= 0)) { return null; } byte[] buffer = newbyte[40]; m_Stream.Seek((long)(offset), SeekOrigin.Begin); m_Stream.Read(buffer, 0, 40); return System.Text.Encoding.UTF8.GetString(buffer); }
Sorry, that was basicly a rip off from GetSound lol. I needed a quick fix and that should do it.
Joeku;755738 said:That would be very nice.
Here's an example of how to use the new StringList methods:
Fully compiled and functional with RunUO 2.0 RC2.Code:using System; using Server; using Ultima; namespace Joeku.Test { public class Test_Main { public static StringList StringList = new StringList( "ENU" ); // Initialize an instance of the StringList. public static void Initialize() { // If you want to simply convert a cliloc into a string... Console.WriteLine( StringList.Table[1076267] ); // Boiling Cauldron // Boiling Cauldron // If you want to format a cliloc with args... Console.WriteLine( StringList.Format( 1070749, "John", "Amy" ) ); // ~1_val~ has joined ~2_val~. // John has joined Amy. // If you want to format a cliloc with a single arg string that is separated by tabs... string args = "Jim\tKelly"; // "\t" is the Tab character Console.WriteLine( StringList.SplitFormat( 1070749, args ) ); // ~1_val~ has joined ~2_val~. // Jim has joined Kelly. } } }
Sixkillers;789588 said:Here is little example how to obtain string from cliloc with Ultima SDK.
Code:using Ultima; private static readonly StringList list = new StringList(); public string GetString(int number) { return list.Table[number] as string; }
If the Cliloc entry doesn't exist method will return null. I hope it is what you want.
Irian;789713 said:You should know that a hue isn't just a color. It's a list of colors (otherwise, hueing an item would like pretty bad).
You can get a System.Drawing.Colors from a Hue - for example the first one from the list - by...
Code:Hue hue = Ultima.Hues.List[<HueID>]; Color color = hue.getColor(0);
JesuZ;772066 said:Sorry for my stupidity, but I don't actually get how that Client.FindLocation() works...?
What variabes should I give for it, or does it just return something...?
Yeah, just begining with this kind of stuff...
Edit:
Okey... After little googleing...
Got how ref variables works, and here's finally my code for character position:
Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Ultima; namespace UORadar { public partial class Form1 : Form { public Form1() { InitializeComponent(); Ultima.Client.Calibrate(); int CharX; int CharY; int CharZ; int CharF; CharX = 0; CharY = 0; CharZ = 0; CharF = 0; Ultima.Client.FindLocation(ref CharX, ref CharY, ref CharZ, ref CharF); Bitmap radarmap = Ultima.Map.Felucca.GetImage(CharX / 8, CharY / 8, 100, 100); pictureBox1.Image = radarmap; xpos.Text = Convert.ToString(CharX); ypos.Text = Convert.ToString(CharY); } private void button1_Click(object sender, EventArgs e) { string viesti = ": " + msg.Text; Client.SendText(viesti); } } }
Something like that