This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-09-21 14:32:50

gimatt
Scratcher
Registered: 2007-09-04
Posts: 2

Store text to a variable?

Hi,
Are there any plans to allow users to store a text string to a variable?  For example: {Set text.variable to "Hello there"}

This would be very helpful, especially if you could concatenate strings of free text with the values of variables to make longer strings of text, something like this:

{Set text.variable to "Your score is "_score.variable_". Great job!"}
{Say text.variable}
or
{Set text.variable to "Hello"_player.name_", would you like to buy something?"}
{Say text.variable}

(In my example the quotes designate literal strings, and underscores signify concatenations.)

The only way I've come up with to store a text string is to convert it to a numeric string, where I give each letter a value from 1-26 and use this formula to form the string:

{Set text.variable to (text.variable*100)+new.value}

So, for example, the word "ball" would become:

2011111, or 2-01-11-11 (b-a-l-l)

I then extract the values later by setting a variable called string.factor and using this formula:

{[text.variable - (text.variable mod string.factor)] / string.factor} mod 100

So, if I want to extract the "a" in ball, I would set the string factor to 10,000, for this result:

{[2,011,111 - (2,011,111 mod 10,000)] / 10,000} mod 100 =
{[2,011,111 - (1,111)] / 10,000} mod 100 =
{2,010,000 / 10,000} mod 100 =
{201} mod 100 = 1 = "a"

I then create one sprite for each character in the word I want to display, with 26 different costumes for every letter in the alphabet. That makes for a lot of sprites and a lot of costumes (albeit, exporting and importing the sprites makes that easier).

The major drawback to this is that the longer the word gets, the larger the number gets (@ 2 digits/letter), and I'm not sure how large a number I can use before I generate calculation problems or response time issues.

(That's another question - how large of a calculation can Scratch handle? At what point should I say, "Whoa, this variable's value is WAY too big, and I'm going to crash this program"?)

It seems that there ought to be an easier way to record and use text entries than what I've come up with. Has anyone come up with any other ideas of how to do this?

Thanks a bunch!
gimatt

Offline

 

#2 2007-09-21 15:21:25

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Store text to a variable?

String variables have been requested a number of times and will probably be included in the next version of scratch.

Also I don't think there is any really efficient way of making text strings in the current version of scratch.


and for every thing else that has been requested go to http://scratch.mit.edu/forums/viewtopic.php?id=1415

Last edited by archmage (2007-09-21 15:23:28)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

Board footer