Jump to content

Convert an int to a String?

RelentlessAF

Hey guys, I'm making a blackjack game for the sake of practice (not in college yet) and I wanted to know if there was a way to call data from an int and output it as a visible String.

 

Currently I have a var that keeps track of your current total so you don't have to add the cards together yourself and it displays at the bottom.

 

Next to that since it displays as "Current Total:" I want the currentTotal var which is an int to be converted to String and change as your total increases (for example "Current Total: 20"). However the engine (I'm using Unity) says I can't just say the String is equal to the current total.

 

It's in Javascript, I can post the script itself if you instruct me on how to do so if that will help.

Link to comment
Share on other sites

Link to post
Share on other sites

someNumber.toString()

I placed it like this

 

if(GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 + 350, 300, 100), cardTotal))

       currentTotal.ToString("cardTotal");
 
But I get an error stating "void cannot be used in a boolean context" How would I word it outside of an if statement?
Link to comment
Share on other sites

Link to post
Share on other sites

Indeed, .toString() should fix that for you.

It doesn't appear to work :\ the String still remains empty.

 

The var is "var cardTotal : String = String.Empty;"

 

and I worded it like this: 

 

function Start ()

{

 

currentTotal.ToString("cardTotal");

 

}

Link to comment
Share on other sites

Link to post
Share on other sites

 

I placed it like this

 

if(GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 + 350, 300, 100), cardTotal))

       currentTotal.ToString("cardTotal");
 
But I get an error stating "void cannot be used in a boolean context" How would I word it outside of an if statement?

 

thats not how you use toString(). Its just a function that converts any object (that supports the function) to a string. So if you convert it to string you would do

cardTotal.toString();

Then append

 

EDIT: I would also like to state it doesn't necessarily convert the object to a string. It gets the string representation of that object which is done in the interface. So if you end up making your own object, you can create a toString() function and return whatever you would like the object to be printed out when printed as a String.

Link to comment
Share on other sites

Link to post
Share on other sites

thats not how you use toString(). Its just a function that converts any object (that supports the function) to a string. So if you convert it to string you would do

cardTotal.toString();

Then append

I changed it to that and it won't let me display it on the GUI saying that I cannot use an int in place of a String :\

Link to comment
Share on other sites

Link to post
Share on other sites

I changed it to that and it won't let me display it on the GUI saying that I cannot use an int in place of a String :\

 

I'm pretty sure capitals matter. I see you have ToString() and would like you to try toString()

Link to comment
Share on other sites

Link to post
Share on other sites

I'm pretty sure capitals matter. I see you have ToString() and would like you to try toString()

It states an error saying "toString is not a member of 'String'. Did you mean 'ToString'?"

Link to comment
Share on other sites

Link to post
Share on other sites

I recently made a card game for my A2 cursework and i would suggest making a "card" structure/object that way you can assign a card a picture, value and a suit. I have no idea about the syntax of javascript but it worked in VB.net.

That could be the problem, I was planning to add the physical cards later and get the basic structure done first.

Link to comment
Share on other sites

Link to post
Share on other sites

That could be the problem, I was planning to add the physical cards later and get the basic structure done first.

I definitely suggest you make it into an object, I managed to re-write my code for the game 3 times before I was happy with the way it worked. Also, I would recommend making the deck of cards as a FILO data structure e.g. a queue. It made things really simple when it came to dealing and shuffling the cards.

Link to comment
Share on other sites

Link to post
Share on other sites

I definitely suggest you make it into an object, I managed to re-write my code for the game 3 times before I was happy with the way it worked. Also, I would recommend making the deck of cards as a FILO data structure e.g. a queue. It made things really simple when it came to dealing and shuffling the cards.

Alright thank you for the help and dragosudeki as well. I will work this out and come back and let you know how it went :D

Link to comment
Share on other sites

Link to post
Share on other sites

It states an error saying "toString is not a member of 'String'. Did you mean 'ToString'?"

 

Thats odd then. Perhaps Unity has it different then? Was checking to make sure because I'm pretty sure I remember correctly

 

 

Convert a number to a string:

var num = 15;

var n = num.toString();

The result of n will be:

15

 

 

 

Source

Link to comment
Share on other sites

Link to post
Share on other sites

Thats odd then. Perhaps Unity has it different then? Was checking to make sure because I'm pretty sure I remember correctly

 

 

 

Source

After trying specifically the same example with var num = 15 var n = num.ToString();(toString still didn't work) and then calling for the string in a GUI.Label the number still won't appear on the screen or in the String in the Inspector.

Link to comment
Share on other sites

Link to post
Share on other sites

toString is not necessary

JS automatically convert numbers to strings when it's necessary

 

for example

if( 1 == "1" )         alert("that's the same yo");

will make the alert appear: the condition is true

 

and

var n = 1;alert(n);

works

Link to comment
Share on other sites

Link to post
Share on other sites

toString is not necessary

JS automatically convert numbers to strings when it's necessary

 

for example

if( 1 == "1" )         alert("that's the same yo");

Wow, that hurts on the inside.

Also, I would recommend making the deck of cards as a FILO data structure e.g. a queue.

A queue is FIFO, a stack is FILO.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Wow, that hurts on the inside.

A queue is FIFO, a stack is FILO.

what do you mean?

anyway, filo does not exist

it's lifo

jus' grammarnaziing

Link to comment
Share on other sites

Link to post
Share on other sites

toString is not necessary

JS automatically convert numbers to strings when it's necessary

 

for example

if( 1 == "1" )         alert("that's the same yo");

will make the alert appear: the condition is true

 

and

var n = 1;alert(n);

works

Neither of those works in Unity, what I am trying to do is get the total of the cards you have to display on the GUI.

 

I created an entirely new project just to test it to see if it works.

 

All that exists right now is this:

 

EDIT: Adding n = num.ToString(); under the if statement in the Update function fixed it.

 

var num = 15;

var n = num.ToString();

 

function Update ()

{

    if(Input.GetKey(KeyCode.E))

       num += 1;

}

 

function OnGUI ()

{

   GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 300, 150) n);

}

 

In the game view 15 appears in the center of the screen but pressing "E" does not change it to 16 like it should.

Link to comment
Share on other sites

Link to post
Share on other sites

Neither of those works in Unity

i just posted that javascript code, i didn't know that unity does not use js

anyway, this works

	var n = 1;	Debug.Log("number one: " + n);

you just had to substitute the "alert" when the compiler gave you the error

 

the comparison example didn't work tho, looks like unityscript has a stronger feeling about types

 

so, this is how you convert "int to a string"

 

and in your code, you need to update n when the function is called

function Update (){       if(Input.GetKey(KeyCode.E)){              num += 1;              n = num.toString();              // n = num + ""; equivalent       }}
Link to comment
Share on other sites

Link to post
Share on other sites

Well your problem is that the place where you put 

var num = 15;

var n = num.ToString();

 

is performned only once on start. ...

 

So you want to create var n;

 

and do n = num.ToString() in

 

function Update(){

n = num.ToString();

};

 

So the value of variable is constantly incremented if the num value is changed. :)

 

 

EDIT : Yup just tested in my project , works like charm http://imgur.com/5FeDYwJ

 

You can add it into onGui itself,  but the function onGUI is called few times per frame, so it is just unnecessary.

 

For better understanding check unity's lifecycles, so you know how many times are each built-in functions called :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×