Author Topic: Making your own SpriteBase class  (Read 3470 times)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Making your own SpriteBase class
« 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.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Making your own SpriteBase class
« Reply #1 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!

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Making your own SpriteBase class
« Reply #2 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.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Making your own SpriteBase class
« Reply #3 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. :)

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Making your own SpriteBase class
« Reply #4 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.
« Last Edit: 2012-01-01, 01:50:11 PM by Pizzaman »

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Making your own SpriteBase class
« Reply #5 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)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Making your own SpriteBase class
« Reply #6 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? :)