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

#1 2012-09-28 23:17:34

radicalace
Scratcher
Registered: 2008-06-18
Posts: 85

How can Scratch tell the difference between characters and a variable?

when gf clicked
set (fun times) to 10
set (herderp) to 0
set(herp) to herp
set (derp) to derp
repeat until <join<( herp ) (derp) > = (fun times)>
say [I am performing an action.] for 1 seconds
change (herpderp) by 1
end
Clearly I am not very good at in-post scratch blocks, but I think you can understand what I am getting at here (Please don't tell me about how I'm bad at them or tell me how to do the correct formatting for them; that's not why I made this thread).  This program here does what I want it to.  The program says it's phrase until herpderp gets to 10, then it stops.  No big deal, right?  The problem arises when you try to use characters instead of numbers.

*EDIT*
Actually ignore that whole paragraph.  It doesn't work.  I'm just even more confused.

when gf clicked
set (fun times) to lulz
set (herderp) to NO
set(herp) to herp
set (derp) to derp
repeat until <join<( herp ) (derp) > = (fun times)>
say [I am performing an action.] for 1 seconds
end

when gf clicked
wait 10 seconds
set herpderp to lulz

Now, you would think that the program would wait be saying the phrase for 10 seconds until herpderp got set to lulz, and then the program would end.  But no.  It just keeps on going, because I assume it thinks that a variable with characters (a string, in other words) is just 0 when used in a number block.  And 0 of course never equals lulz.

Also, when you use string variables in other operators, for example:

when gf clicked
set (herp) to herp
set (herp) to derp
set (herpderp) to 5
set lulz to 10 - <join (herp) (derp)> 
It should be 5, right?  Nope.  It's 10, because a string equals 0.

Does anyone know how to help me?  The reason I am asking is because I'm working on this
(Note: some content may be inappropriate for younger viewers (there's some red stuff, who knows what it is though?) so view at your own risk)

As you can see I just made an enemy and I plan on making a lot more of them.  However, I don't want to spend all my time replacing variables- I would rather "optimize" so to speak, my scripts so that I can simply have something like:


when gf clicked
set (Enemy 1 weapon) to fists // public string in sprite 1

when gf clicked 
set (my name) to Enemy 1  //private string in sprite 2
set (weapon) to weapon // private string sprite 2

if < <join  (my name) (weapon) > = fists >
 perform x action
end
What I would like to happen is that my sprite "Enemy 1" will see that "Enemy 1 + weapon" (or, Enemy 1 weapon, a public string I declared in a different sprite) equals "fists" so that it can know to perform the intended action.  But it doesn't.  So again, if anyone knows how to help me that would be great.  And by the way, I already read this and I didn't find it particularly helpful.




*Edit* I think I figured it all out.  You can't use "join" to call variables, you can only use it to indicate things like "go to <join (mouse-) (pointer)>" but I might be wrong, if anyone can confirm/deny then thanks.

Last edited by radicalace (2012-09-28 23:44:25)

Offline

 

#2 2012-09-29 00:21:57

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: How can Scratch tell the difference between characters and a variable?

There is a way to call variables based on sprites, but it's not exactly what you're looking for because it uses local variables which can only be set inside the sprite that hosts that variable.

You could, however, use lists.  Create a list called "weapon" and any other lists you need.  Then the sprite you are working with depends on a single item in that list specified by the item number (i.e. Enemy 1 looks at item 1).  You can then set weapons fairly easily.  You can also put variables into the section where the item number is usually entered meaning you can decrease the amount of code needed.

Basically, you can do this:

replace item (Enemy #) of [weapon v] with (weapon)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#3 2012-09-29 00:29:43

radicalace
Scratcher
Registered: 2008-06-18
Posts: 85

Re: How can Scratch tell the difference between characters and a variable?

AtomicBawm3 wrote:

There is a way to call variables based on sprites, but it's not exactly what you're looking for because it uses local variables which can only be set inside the sprite that hosts that variable.

You could, however, use lists.  Create a list called "weapon" and any other lists you need.  Then the sprite you are working with depends on a single item in that list specified by the item number (i.e. Enemy 1 looks at item 1).  You can then set weapons fairly easily.  You can also put variables into the section where the item number is usually entered meaning you can decrease the amount of code needed.

Basically, you can do this:

replace item (Enemy #) of [weapon v] with (weapon)

Well, what I wanted to do was make it so that when I copied a enemy sprite to make another one, all I would have to do is change the declaration of "My Name" and a few other minor things, and then all the work would be done for me.  However, I think I can still use that method to accomplish my goal, so I will try it.

Offline

 

Board footer