Author Topic: Implementing Game Score (Via timer)  (Read 3131 times)

chanpod

  • Visitor
  • *
  • Posts: 6
    • View Profile
    • Email
Implementing Game Score (Via timer)
« on: 2013-03-13, 11:07:23 AM »
Ok, so I can create the timer in C# but I'm not so sure how to implement it in sgdk. I've just started using this so I still have no idea how everything fits together. I've done the basic tutorial, but that's it. So,

1. How do I implement C# code into sgdk (I know how to import it, but how do I USE it?)

2. Once the timer is running, I don't want to display the timer, I want to display the score based on time elapsed.

3. How do I display text based counters?

Thanks, if this information is out there, I apologize. I searched around but couldn't find the thing I was looking for.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Implementing Game Score (Via timer)
« Reply #1 on: 2013-03-13, 03:48:49 PM »
Hi! Welcome to the forums. I'll answer these one by one, slightly out of order.

Quote
1. How do I implement C# code into sgdk (I know how to import it, but how do I USE it?)


1. You need to arrange your code in this format.

   [Description("Your method's description")] <- Make sure you have this right before any method declaration.
   public void YourMethod()
   {
         //Your code here..
   }

Add these imports to your .cs file.

using System.ComponentModel;
using System.Collections;




Quote
3. How do I display text based counters?
3. If you want to display a text based counter you need a modified version of DrawCounterWithLabel, which I'll make for you now:




Copy and Paste this into PlanBase.cs

 
Code: [Select]
 [Description("Display a string in the current plan's rectangle")]
   public virtual void DrawString(string Label, System.Drawing.KnownColor color)
   {
  
      System.Diagnostics.Debug.Assert(!PlanRectangle.IsEmpty, "DrawCounterAsTile was called on a plan that does not have a rectangle defined");
      if (PlanRectangle.IsEmpty)
         return;
      
      Display disp = ParentLayer.ParentMap.Display;
      disp.ScissorOff();
      disp.SetColor(Color.FromKnownColor(color));
      disp.DrawText(Label.ToString(), PlanRectangle.Left, PlanRectangle.Top, -540);
      disp.Flush();
  
   }

This plan rule will draw a string in the plan's rectangle. Just enter it in as "your text here". As far as text based counters go, SGDK2 uses integers. You would have to use your own class or object to represent text based counters as strings.

2.
Quote
2. Once the timer is running, I don't want to display the timer, I want to display the score based on time elapsed.

Just use that above method, and put whatever you want to display as ("" + whatevervalue + "")

I think Timer may already have a method that  converts it to a string. (Or not?)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2744
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Implementing Game Score (Via timer)
« Reply #2 on: 2013-03-13, 05:08:01 PM »
I think v6v mostly answered your questions, but I'll comment too in case my comments can add further benefit.

I would encourage you to use DrawCounterWithLabel to get the score displayed, and then you would only need to figure out how to get the value of one counter to represent the score based on the value in another counter (if you even need two separate counters). Is there any reason you couldn't simply update the score directly with the ChangeCounter function? If you need to update it more slowly, you can use ChangeCounter to change an invisible counter, and have it loop and use it with "If" to change another counter only once every time it loops. That way you could make the score change more slowly in relation to a counter that increments on every frame. I suspect whatever you want the score to do could be done without writing any code, but I'd need to know more about what you're trying to do to the score.

You may have to be more specific about what you mean by "text based counters". A counter implies a number, so I don't understand what you're asking for.

There are a couple ways to use C# code once you have it in the project. One way is to do as v6v suggests (if you are adding code to SpriteBase.cs, PlanBase.cs or GeneralRules.cs). Such functions, when added with a Description, will then show up in the Function dropdown list (if you don't have any errors) for the Sprite editor, Plan editor, or both respectively. The other way is to edit one of the other .cs files in the SourceCode folder of your project (in the IDE) and just call the function from other code that is already running at the time you want your code to be called. For example, if you write a function in MapBase.cs that writes some debug info to a file, you could call it from the DrawAllViews function in MapBase.cs, and then your function would run once on every frame. Not sure if that fully answers your questions, but hopefully gives you some idea.

Also, keep in mind that if you want your project to run as HTML you can't just add C# code, you would also need to add .js code.

chanpod

  • Visitor
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Implementing Game Score (Via timer)
« Reply #3 on: 2013-03-14, 12:28:26 PM »
Scratch that, I did what bluemonkmn suggested. how do I update a counter manually. Only functions I see are increment, set Maximum, set Min. I want to add some arbitrary amount.
« Last Edit: 2013-03-14, 01:01:28 PM by chanpod »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2744
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Implementing Game Score (Via timer)
« Reply #4 on: 2013-03-14, 01:10:55 PM »
Try selecting "=" as the function, select Counter.YourCounter.CurrentValue as the Value, and select Counter.YourCounter.CurrentValue as the "Output to". Then in the Value field, change Counter.YourCounter.CurrentValue to Counter.YourCounter.CurrentValue + 5 if you want to add 5, for example. Just be aware that if you try to set the value to something outside the min/max range, it will stop at the limit.

chanpod

  • Visitor
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Implementing Game Score (Via timer)
« Reply #5 on: 2013-03-14, 01:46:31 PM »
Sweet, thanks. I should have figured that out : /

Alright, last thing. I'm displaying the counter as suggested but it's saying Counter#value# even though my label is "" + Counter.Score. I would obviously like to do "Score: " + Counter.Score and show only Score: #value#

Is that what the modified drawcounterwithlabel is for?

Thanks

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2744
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Implementing Game Score (Via timer)
« Reply #6 on: 2013-03-15, 08:29:31 AM »
The sample project shows a score just like the one you're trying to. If you want to see how that works, create a new project from the Sample project template (or load the sample project template directly) and look under:
Maps
.Overlay for 1 player
..Layers
...Inventory
....Plans
.....Score
Then select the "Draw score" rule. You will see that it uses DrawCounterWithLabel, and passes "Score: " (including quotes) as the Label parameter, Counter.Player_1_Score as the counter parameter, and System.Drawing.KnownColor.White as the color parameter.

Does that answer your question?

The reason you may be getting unexpected results is because when you convert an object to a string in C#, you generally get the object name if the object doesn't define how to convert itself to a string. And Counter objects don't convert themselves to strings because they expect that you refer to their value instead of the counter object itself. If you want the value of the counter you need to refer to CurrentValue. The reason DrawCounterWithLabel works with a counter is because the second parameter *expects* a counter, so it knows how to get the value of the counter to draw just the value.

chanpod

  • Visitor
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Implementing Game Score (Via timer)
« Reply #7 on: 2013-03-15, 10:00:22 AM »
That got it, thanks. So, all of the text fields in the sgdk are parameters and as long as I know what type of parameter I'm dealing with, I can put any compilable code into that field? That's good to know. It's not limited to pre-defined rules. Since this appears to be the case, is there a reference library somewhere so I can figure out that I should have done Counter.Score.CurrentValue instead of just Counter.Score? I couldn't find the counter class anywhere in the provided code. (I assume it's a class anyways)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2744
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Implementing Game Score (Via timer)
« Reply #8 on: 2013-03-18, 05:08:53 AM »
Only the bottom 4 fields in the Edit Plan window, and the bottom 4 fields on the Rules tab of the Sprite Definition window take parameters. But yes, you can put any compatible code into those fields so long as you keep in mind that if you are writing an HTML project, it generally has to be compatible with both C# and JavaScript (which is usually easy since they are so similar in syntax).

The Counter class is one of the few classes that are 100% generated by the code generator instead of having some of the code in the project tree. But you can see what it looks like if you look at the generated file Counters.cs (or Counter.cs - I forget) after building your project.