Scrolling Game Development Kit Forum

SGDK Version 2 => Help, Errors, FAQ => Topic started by: bluemonkmn on 2011-12-25, 08:34:53 AM

Title: Making your own SpriteBase class
Post by: bluemonkmn on 2011-12-25, 08:34:53 AM
This message is in reply to a question Pizzaman had, and I wanted to post the answer more publicly so everyone could benefit.  You can add parameters to all sprites, or all sprites of a particular type by creating a class that inherits from SpriteBase, then configuring your sprites to inherit from that.  First create the base class like this (create a new source code object and delete the lines that put it into the CustomObjects namespace):

Code: [Select]
public abstract class UserSprite : SpriteBase
{
   public string Username;
   public int userId;

   public UserSprite(LayerBase layer, double x, double y, double dx, double dy, int state, int frame, bool active, Solidity solidity, int color)
   : base(layer, x, y, dx, dy, state, frame, active, solidity, color)
   {
   }
}

This class adds a "Username" and a "userId".

Make sure your project builds.  Then go to the sprite definition screen and you should see a new option in the Base Class dropdown list as shown in the attached screenshot.  If you choose "UserSprite", the extra values you added in this class will be available in the rule editor.

HOWEVER, the rule editor only shows integer type parameters, so although you will see "userId" in the dropdown lists in the rule editor, you will not see Username.  But that doesn't mean you can't use Username.  It just means you'll have to type it manually.  Most of the built-in functions try to use integers and stay away from strings because it limits complexity and room for error.  Notice how the SaveGame functions, for example, use slot numbers instead of saved game file names.  Then you don't have to always remember to be quoting your parameters to make sure they're actually strings.
Title: Re: Making your own SpriteBase class
Post by: v6v on 2011-12-25, 10:51:40 AM
Ah, that was the issue, I had added public strings directly to the SpriteBase class and wondered why the username string wouldn't appear within the dropdown.

This also means that for the "Real Physics' (The real name escapes me) Code Object, I can now create an abstract class that only uses the Realistic Physics, while the default class uses the default, right?

>SpriteBase
>UserSprite
>PhysicsBase

Cool beans!
Title: Re: Making your own SpriteBase class
Post by: bluemonkmn on 2011-12-27, 08:33:05 AM
I don't remember how well the real physics sprites play together with normal sprites.  Hopefully you are correct.  I vaguely remember that I might have had to change some code that wasn't in the SpriteBase class to make it work, but I might be imagining things, or it might have been a change that wouldn't affect the other sprites.  It's certainly worth trying.
Title: Re: Making your own SpriteBase class
Post by: v6v on 2011-12-28, 04:16:35 PM
Oop, scratch that, there seems to be some differences within the Initialize Method of both versions of SpriteBase. Not much of a problem, as long as Strings are accessible within my Custom SpriteBase class. Aesthetics aren't as peremptory. :)
Title: Re: Making your own SpriteBase class
Post by: v6v on 2012-01-01, 01:46:02 PM
Oh hey, does it Happen to show Methods made in the new class?

It compiles without errors, but I can't seem to find my new methods.

Edit: Whoa, I didn't successfully build it, but after I had it appeared fine.
Title: Re: Making your own SpriteBase class
Post by: v6v on 2012-01-30, 08:05:37 PM
Ok, my new issue here is this.

I need to define some methods in the constructor, but If I paste them within the UserSprite constructor I get errors.

I'm still learning C#, will override allow me to insert new methods into the UserSprite constructor?
Code: [Select]
int bleep;
int blop;
int etc..;

   public UserSprite(LayerBase layer, double x, double y, double dx, double dy, int state, int frame, bool active, Solidity solidity, int color)
   : base(layer, x, y, dx, dy, state, frame, active, solidity, color)
   {

up= bleep;
            down= blop;    
 left= bloopedyblop;
            right= beepbeepbeep;
   }

  
    
}

How exactly would I create methods unique to the class? Especially inside of the constructor?
(I'm getting an Endfile expected, by the way, although I've double checked my brackets)
Title: Re: Making your own SpriteBase class
Post by: bluemonkmn on 2012-01-31, 06:08:13 AM
You have more closing braces than opening braces in your file.  That's what that error indicates, and I can see it in the sample code you posted.  Removing the extra closing brace at the end should fix that.  You said you double-checked that -- I guess you needed to triple check? :)