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

#1 2012-03-28 23:23:55

whf4
New Scratcher
Registered: 2012-03-28
Posts: 4

How to use calculated variable names?

Hi, All.

Apologies if this si well known, but I googled and searched, maybe ineffectively.

In a sentence-parsing script, I would like to be able to calculate the name of a variable to be set, but I cannot place an expression in the 'set' block, like I can in the costume block. Kind of like pointers and casts.

for example


change to [(join "costume" counter)]

lets the script calculate which costume to change to based on user input or elapsed time or whatever.

however

set (join "varname" index) to c

doesn't seem to exist.

Any ideas?

thanks!

Bill

Offline

 

#2 2012-03-28 23:32:43

lukanater
Scratcher
Registered: 2007-09-26
Posts: 100+

Re: How to use calculated variable names?

I don't think you can create a variable automatically, which is what I think you wish to do.  You need to create a new one from the variables tab.


http://i1049.photobucket.com/albums/s385/xenokstudios/trippinstudios.png

Offline

 

#3 2012-03-28 23:36:54

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Re: How to use calculated variable names?

You can use a list.


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#4 2012-03-28 23:43:35

whf4
New Scratcher
Registered: 2012-03-28
Posts: 4

Re: How to use calculated variable names?

@lukanater: Thank you! I want to dynamically choose which of several pre-exisiting variables I am setting

@ProgramCAT:  thank you, was wondering if there was a way to pass a variable name to set rather than using a list, but could use list instead.

Offline

 

#5 2012-03-28 23:49:13

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Re: How to use calculated variable names?

You can't insert expressions as variable names.


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#6 2012-03-29 07:33:58

whf4
New Scratcher
Registered: 2012-03-28
Posts: 4

Re: How to use calculated variable names?

That might be a great thing to put on a wishlist. Thanks for your help!

Offline

 

#7 2012-03-29 09:18:48

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: How to use calculated variable names?

First of all, it is generally frowned upon to calculate and use variable names in any language. In JavaScript, it is literally a sin to use "eval('var thing'+index+' = 0;')".

You should use a list. Create a list called "myVars", then use the following script:

when gf clicked
repeat (number of variables you need)
add [null] to [myVars v]
end
Then you use these blocks to edit them:
say (item (index) of [myVars v])
replace item (index) of [myVars v] with (join [Hey, this ] [works! Thanks, Hardmath123!])
Hope this helped!  smile

Last edited by Hardmath123 (2012-03-29 09:20:23)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#8 2012-03-29 19:06:30

whf4
New Scratcher
Registered: 2012-03-28
Posts: 4

Re: How to use calculated variable names?

yes, thank you, I knew you could do this with lists.  Not sure I concur with your global horror of calculating variable names, but I agree an indexed array is cleaner.

Offline

 

#9 2012-03-29 19:42:07

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: How to use calculated variable names?

whf4 wrote:

yes, thank you, I knew you could do this with lists.  Not sure I concur with your global horror of calculating variable names, but I agree an indexed array is cleaner.

In a language like JavaScript, a malicious user could hack the program quite easily indeed.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#10 2012-03-30 07:10:12

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: How to use calculated variable names?

whf4 wrote:

yes, thank you, I knew you could do this with lists.  Not sure I concur with your global horror of calculating variable names, but I agree an indexed array is cleaner.

it's not THAT bad, but it's a really bad thing to do if there's an alternative like lists.

Offline

 

Board footer