I moved to a new URL! Check it out!

posts tagged with: csharp

Otter Updates

Otter Updates
I finally merged in all of my pending changes into the main branch of Otter! If you've been using the dev branch for the past few months not much has changed, but things finally stabilized enough to the point where I felt comfortable updating the main branch.

Afterwards I've done some minor adjustments and fixes:

* Using Game.EnableAltF4 now checks for either Alt key on the keyboard.

* The RandomElement() function in NaturalLanguage now uses Otter's Rand class to ensure consistency if you're using a preset random seed.

* Added Snake.AddAllVertebraeToScene()

* Axis and Button keys, joysticks, mousebuttons, etc, are now public accessible.

* Renamed Button.AddButton to AddJoyButton.

* Axis and Buttons can now add keys, buttons, etc, from other Axes and Buttons. Useful if you want to copy a button or axis.

* GridCounter now has MoveUp(), MoveDown(), MoveLeft(), and MoveRight()

Slowly and surely moving towards a 1.0 version!

Dev Log: Otter Updates!

Dev Log: Otter Updates!
In case you didn't know I have a free to use open source 2d game making framework named Otter. It's built on top of SFML.NET and uses C#. Here are some recent changes available in the dev branch!

* Color.AddCustom() and Color.Custom() added. This is a handy way to define custom colors by Enum and later retrieve them. I didn't like making my own static colors class for each project to keep track of my custom colors, so I added some functionality to the Color class.

* BitmapFonts have been almost finalized. They should be usable now for the most part and you should be able to import fonts from a handful of different editors. You can check out what editors are supported in the class itself with the Enum BitmapDataType.

* RichText has been updated and fixed once again. I guess I've been doing kerning wrong all this time and I think I finally have it corrected. RichTextCharacters also have some new values to play with like their X and Y offset positions, scale and rotation and more.

* Added Enum.HasFlag() as an extension method. This is a handy way to check if an Enum with a [Flags] attribute contains a specific flag. I guess you can use this if you're not comfortable looking at the bitwise operators or whatever they're called (I sure am not!)

* Shader.AddParameter() is now available to register your shader parameters as an Enum. Now you can just define it once and use the Enum everywhere which should reduce pesky typos when working with shaders.

* StateMachine has been scrapped and StateMachine<> has been upgraded a little bit. It's now possible to use a stack of States in a StateMachine<> and it will run the top most state. Also now if you call ChangeState (or if you push and/or pop states) during a StateMachine's update function the StateMachine will wait until the update has finished before applying the state change!

* Fixed a typo in the Shader class whoops (SetParamter?)

* Surfaces now have their Display() function exposed. This is crucial in using a Surface that doesn't render at all, like if you're using a Surface as input to a Shader. Without calling Display() the texture will be upside down.

* Updated Collider and Entity and Graphics with more stuff like SetPosition(), SetOrigin(), and other handy stuff.

* Surface.SaveToFile() will now automatically save with a timestamp file name as a png if you don't specify an output file path. Handy for taking a bunch of screenshots during a session.

* NineSlice has some new utility functions like SetBorderPadding. I found it easier to define the areas of a NineSlice object in this way instead of trying to figure out what the bounds of the rectangle inside the texture was.

* RichText now will always round its origin since setting the origin to a 0.5f value results in blurry text!

* Fixed a bad bug in the PolygonCollider which resulted in the collider not using the transformed polygon for collision checks.

* Added 3D Audio support thanks to Fruckert.

* Lots of little bug fixes and tuning changes here and there.

Once again this is all available on the dev branch. I'll be pushing a whole bunch of changes to main branch as soon as I can sit down and write some documentation up for the new and changed stuff.

Asset Class Generator Update

Asset Class Generator Update
Just a quick post to say I've updated my Visual Studio Asset Class Generator with some bug fixes here and there. Download the new version right here.

There were some issues with duplicate file names, and also files with more than one period in the name would destroy it so I've now set it to ignore those files. I've also cleaned up the code that the generator writes.

The source code for the updated version is also now available here so you can feel free to modify it to your hearts content!

Google Spreadsheet Sync

Google Spreadsheet Sync
Have you ever wanted to take the contents of a Google Spreadsheet and drop it right into your game project? So have I! That's why I made a tool that will take the contents of a Google Spreadsheet and turn it into a C# code file full of data!

Demo


Let's say we have a spreadsheet full of data for our video game written in C#, and it looks something like this.

Image


The resulting output of that spreadsheet will be this.
class Sheets {

public static Dictionary<string, CharactersRow> Characters = new Dictionary<string, CharactersRow>() {
{"Adventurer", new CharactersRow() {
Name = "Adventurer",
Health = 100,
Magic = 100,
Attack = 100,
Defense = 100,
Speed = 100,
VictoryPhrase = "Nice try, \"chump!\"",
FailurePhrase = "Ah... well done..."
}},
{"Thief", new CharactersRow() {
Name = "Thief",
Health = 75,
Magic = 100,
Attack = 100,
Defense = 50,
Speed = 200,
VictoryPhrase = "I take what I want!",
FailurePhrase = "Not fair!"
}},
{"Assassin", new CharactersRow() {
Name = "Assassin",
Health = 100,
Magic = 50,
Attack = 200,
Defense = 50,
Speed = 100,
VictoryPhrase = "You're lucky to be alive.",
FailurePhrase = "How could you defeat me?!"
}}
};

public class CharactersRow {
public string Name;
public int Health;
public int Magic;
public int Attack;
public int Defense;
public int Speed;
public string VictoryPhrase;
public string FailurePhrase;
}

public static Dictionary<string, WeaponsRow> Weapons = new Dictionary<string, WeaponsRow>() {
{"Bronze Sword", new WeaponsRow() {
Name = "Bronze Sword",
Attack = 45,
Defense = 0,
Type = WeaponType.Sword,
Value = 100,
CanUpgrade = true
}},
{"Silver Bow", new WeaponsRow() {
Name = "Silver Bow",
Attack = 20,
Defense = 0,
Type = WeaponType.Bow,
Value = 250,
CanUpgrade = true
}},
{"Fire Wand", new WeaponsRow() {
Name = "Fire Wand",
Attack = 35,
Defense = 15,
Type = WeaponType.Staff,
Value = 400,
CanUpgrade = false
}},
{"Lasso", new WeaponsRow() {
Name = "Lasso",
Attack = 30,
Defense = 0,
Type = WeaponType.Whip,
Value = 150,
CanUpgrade = true
}}
};

public class WeaponsRow {
public string Name;
public int Attack;
public int Defense;
public WeaponType Type;
public int Value;
public bool CanUpgrade;
}
}

With a class like that in my project now it becomes very simple to get data from the spreadsheet with the key of the row (the first column.)
var charName = "Adventurer";
var health = Sheets.Characters[charName].Health;
var victoryPhrase = Sheets.Characters[charName].VictoryPhrase;
Console.WriteLine(victoryPhrase);

Download


Download GoogleSpreadsheetSync.zip (0.02MB)

Usage


I use Visual Studio, so I can only really tell you about that.

It's easiest to set this up as an EXTERNAL TOOL in Visual Studio.
* In Visual Studio go to the Tools menu at the top, select "External Tools..."

Image

* Click "Add"
* Name it something like "Google Spreadsheet Sync"
* For Command, press the "..." button to the right and find the exe.
* For arguments you can use "$(TargetName)" "$(ProjectDir)GoogleSheet.key"
* Include the quotes in those arguments!
* For Initial Directory put $(ProjectDir)
* No quotes in that one.
* Below that check the "Use Output Window" option.

Image


Before you run it though you're going to need to create a GoogleSheet.key file. That file should only contain google spreadsheet key.

For this example here's a URL to a published google spreadsheet: https://docs.google.com/spreadsheets/d/11Ia3wr-Pon1180M0_KRR1hywEITLn_P1BoUedbNQeqw/pubhtml

The key is that big long section: 11Ia3wr-Pon1180M0_KRR1hywEITLn_P1BoUedbNQeqw So in this example the contents of your GoogleSheet.key file should only be:

11Ia3wr-Pon1180M0_KRR1hywEITLn_P1BoUedbNQeqw

Save the file as GoogleSheet.key inside your root project folder. (So probably the same folder as your Program.cs)

Image


You should be all set to now run it from Visual Studio! It should appear in your Tools menu. You can even set a keyboard shortcut for it later if you want.

Run the tool from the Tools menu and what should happen is a Sheets.cs file is generated with all the data from your spreadsheets.

Image


Source


Of course it would be way better if you're able to take this tool and craft it into your own needs, so here's the source code!

I've started a public repository where I'll keep any useful tools I create right here.

Trouble?


If you run into any trouble feel free to post a comment and I'll try to help! Or you can reach me at hi@kpulv.com, or the contact form at the bottom of my site.

Dev Log: Event Queues Everywhere

Dev Log: Event Queues Everywhere
I've been working on a little prototype involving turn based combat. I'm not quite 100% ready to dive back into Stratoforce yet, and I had this idea gnawing at me and I just have to get it out of my system. What exactly is the idea? Well I can't say what it is yet, but if it works out then I'll talk more about it!

Since it is based around turn based combat I had to figure out how exactly that works. I'm so used to programming things that happen all in real time that suddenly going into a turn based game makes absolutely no sense to my brain. At first I had these huge state machines that were incredibly clunky, then I transformed it all into events that can be placed into a magical event queue.

Here's a simple class that can be dropped into Otter that is an event queue manager. It was whipped up very quickly so there might be some slight issues to debug. Check it out:
/// <summary>
/// A Component to manage and process queue of events
/// </summary>
class EventQueue : Component {

public List<EventQueueEvent> Events = new List<EventQueueEvent>();
public EventQueueEvent CurrentEvent { get; private set; }

bool isFreshEvent = true;

public EventQueue() {

}

public bool HasEvents {
get {
return Events.Count > 0;
}
}

public void Add(params EventQueueEvent[] evt) {
Events.AddRange(evt);
}

public void Push(params EventQueueEvent[] evt) {
Events.InsertRange(0, evt);
}


public override void Update() {
base.Update();

if (CurrentEvent == null) {
NextEvent();
}

while (CurrentEvent != null) {
if (isFreshEvent) {
isFreshEvent = false;
CurrentEvent.EventQueue = this;
CurrentEvent.Start();
CurrentEvent.Enter();
}

CurrentEvent.Update();
CurrentEvent.Timer++;

if (CurrentEvent.IsFinished) {
isFreshEvent = true;
CurrentEvent.Exit();
CurrentEvent.EventQueue = null;
Events.Remove(CurrentEvent);
NextEvent();
}
else {
break;
}
}

}

void NextEvent() {
if (Events.Count > 0) {
CurrentEvent = Events[0];
}
else {
CurrentEvent = null;
}
}
}

class EventQueueEvent {

public EventQueue EventQueue;
public int Timer = 0;
public bool IsFinished { get; private set; }

public void Start() {
IsFinished = false;
Timer = 0;
}

public virtual void Enter() {

}

public virtual void Update() {

}

public virtual void Exit() {

}

public void Finish() {
IsFinished = true;
}
}

This is sort of similar to just a very simple behavior tree (see Kodo for more advanced stuff!) Basically what happens is there's a list of events that all need to be run. The current event is run during the Update, and if it's finished then it's removed and now the next one starts up. You can both Add and Push events into the Queue. Push will insert an event at index zero so it will be the next thing to run. To make events just make classes that extend EventQueueEvent!

So I have events like "Damage" "ActorDefeated" "ChooseTarget" "TakeActions" and more. It becomes very easy to manage when things are broken up into separate classes like this! This is actually also giving me ideas on how to run cutscenes in more real time action type games as well. Maybe it will be of some use to you!

Dev Log: Otter Updates

Dev Log: Otter Updates
Over the past week or so I've been doing some various changes to Otter as I'm still trying to figure out the best way to add Entities to Scenes, and Components to Entities. Currently in the dev branch an Entity's Added function is not called until the Entity is actually added to the scene at the beginning of the next update, and now for Components their Added function is not called until it is actually added to the Entity which can occur at the start of each update.

For now I just did some minor updates and changes

* Changed how Entity.IsInScene works. Now IsInScene will return true if the Entity is in a scene, or queued up to be added to a scene.

* Added SetColor to the Color class. This is useful for changing all of the values on a color especially when using another color as the source values. Mostly used in cases where you don't want to Copy and create a new Color instance.

* Finally figured out how to get colliders to have separate constructors when using Enums as tags. Now you should be able to specify Enums as tags in the constructors of all colliders.