Hello Guest

Author Topic: 2DToolKit with JavaScript - More Questions  (Read 5890 times)

imnickb

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
2DToolKit with JavaScript - More Questions
« on: March 25, 2013, 04:16:11 am »
First off, I've read the top thread in the FAQ about "How do I access 2D Toolkit from JavaScript" I followed the instructions for the latest version and clicked "Set Up For JavaScript" and I can honestly say I have no idea what it did...

My situation is that I have a game I've been working on that's completely in JavaScript. I'd like to use the 2DToolKit for text. I've read through all of the tutorials and they were extremely helpful, but since I'm pretty much a beginner at this, I still have many questions...

1-Is it possible to do 2DToolKit scripting in JavaScript, or do I have to make C# scripts and then try to access those scripts from my other JavaScripts?
2-Whichever way it is that I have to work, how do I do it? Are there any examples anywhere of this stuff?

For example, I have a HeroController JavaScript and it increments a coinCount variable in it when the hero collides with coins. I want to display the coinCount variable using 2dToolKit. For the life of me, I can't get this to work at all. I can display the coinCount using OnGui in another JavaScript by making it a static variable and accessing it using HeroController.coinCount. I've also gone through the TextMesh scripting tutorial and I can get the score to update when I press the Q button. However, I have NO IDEA how to go about getting my existing Hero script to work with the 2DToolKit.

I'm sorry if this is simple to most of you, but I'm fairly inexperienced with a lot of this and I finally decided to break down and ask. I can post my HeroController JavaScript and the TextMesh scripting tutorial script if that helps.

Thanks so much!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2DToolKit with JavaScript - More Questions
« Reply #1 on: March 25, 2013, 03:14:13 pm »
Setup for JS moves files about so they can be accessed from JS.
Unity requires scripts to be in a specific folder to be allow c# scripts to be accessed from JS.

Once you've done that you can easily grab components and use them. The c# code is almost 100% transferable to JS. I've attached a port of this script here:
http://unikronsoftware.com/2dtoolkit/doc/tutorial/scripting_a_text_mesh.html

Code: [Select]
#pragma strict

private var textMesh : tk2dTextMesh;
textMesh = GetComponent(tk2dTextMesh);

private var score = 0;

function Update () {
if (Input.GetKey(KeyCode.Q)) {
score++;
textMesh.text = "SCORE: " + score.ToString();
textMesh.Commit();
}
}

imnickb

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: 2DToolKit with JavaScript - More Questions
« Reply #2 on: March 25, 2013, 03:20:34 pm »
This is a huge help, thank you so much. This is exactly what I was looking for! This is the part I was stuck on:

Code: [Select]
private var textMesh : tk2dTextMesh;
textMesh = GetComponent(tk2dTextMesh);

Thanks again! Do you know what parts may not be transferable to JS? I'm just curious so I can look out for that if I run into a problem. Although, I doubt I'll ever run into that issue. Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2DToolKit with JavaScript - More Questions
« Reply #3 on: March 25, 2013, 03:21:31 pm »
Everything should work in JS. You may have to brave some funky syntax changes, but apart from that everything should work. If it doesn't report here and I'll get it sorted out.