Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » A Better way of creating a 2 dimensional List
- manwithmanykids
- Scratcher
67 posts
A Better way of creating a 2 dimensional List
So I am starting to think grander in the way i am using Scratch, and I now have need of 2 dimensional lists. So I set out to creat my own and Came up with this.
http://scratch.mit.edu/projects/10602132/
The above project uses 1 list to keep track of pointers/indexes in 3 other lists for easy reference
I then found I needed Functions and could not get the Blocks to be global so I used Broadcast and wait for them.
So I guess I have 2 questions.
1) How do I make a global Block (Which I believe is like a function call in other languages?
2) Is there any plans to add 2 dimensional lists any time soon?
Am I on the right track here or is there a much simpler way I have missed
http://scratch.mit.edu/projects/10602132/
The above project uses 1 list to keep track of pointers/indexes in 3 other lists for easy reference
I then found I needed Functions and could not get the Blocks to be global so I used Broadcast and wait for them.
So I guess I have 2 questions.
1) How do I make a global Block (Which I believe is like a function call in other languages?
2) Is there any plans to add 2 dimensional lists any time soon?
Am I on the right track here or is there a much simpler way I have missed
Last edited by manwithmanykids (June 3, 2013 15:36:51)
- mwiedmann
- Scratcher
100+ posts
A Better way of creating a 2 dimensional List
Yes, 2D lists are needed in Scratch. I've played around with something similar to what you've got here but it always turns out ugly and I just find a different way. Looks like you've got it working though. I'd be curious to hear what problem you want to solve with the 2D list and see if we can come up with a work around.
For the public functions, you will not be able to find a good way to do it in Scratch because you ultimately end up having to pass data around using public variables and then use broadcast and wait (like you have). The performance hit is VERY noticeable, and you can't always guarantee that your function call will not be interrupted. If it has any loops in it or screen updates you can run into problems. Really though the performance hit kills it. If you have one of these function calls in the main loop on a sprite, you will see it stutter A LOT.
Usually I just end up using the “make a block” feature and then copy it around to all the sprites that need it. It duplicates code a bit but it performs better and looks cleaner in the code (and is easier to understand).
- manwithmanykids
- Scratcher
67 posts
A Better way of creating a 2 dimensional List
Thanks for the reply. I was Hoping for better news, but I Guess I can use Blocks instead of broadcast and wait. and Ya, in my example it works, but in the game I plan on designing, I can see there being all kinds of problems with variable being reset by other forever loops running in the back ground etc. I was really hoping for a global blocks solution or work around that some one came up with.
As for the lists, I am planning a traders type game (either space themed or Fantasy) in which you will earn wealth etc through trade. I envision having to track Traders, resources, pricing, supple, demand etc and think a 2d list would be handy.
ie:
Lists:
Traders
- Trader Number, Name, Start, Destination,Cargo, Alignment, type
Cargo
- Type, Amount
Cities/Space Ports
- Population, alignment, Wealth
Etc,
I know I Can do it with alot of lists, but I was hoping to normalize my data as much as Possible.
I am guessing an other way to do this might be to tranpose the list so that.
in the case of the traders list, I assume 10 attributes an therefore the list might look like this
Traders:
1
Jack
Toronto
London
5
Canadian
Aggressive
0
0
0
2
Fitz
Hamilton
London
6
American
Passive
0
0
0
Do you see any major Flaws in this Approach?
As for the lists, I am planning a traders type game (either space themed or Fantasy) in which you will earn wealth etc through trade. I envision having to track Traders, resources, pricing, supple, demand etc and think a 2d list would be handy.
ie:
Lists:
Traders
- Trader Number, Name, Start, Destination,Cargo, Alignment, type
Cargo
- Type, Amount
Cities/Space Ports
- Population, alignment, Wealth
Etc,
I know I Can do it with alot of lists, but I was hoping to normalize my data as much as Possible.
I am guessing an other way to do this might be to tranpose the list so that.
in the case of the traders list, I assume 10 attributes an therefore the list might look like this
Traders:
1
Jack
Toronto
London
5
Canadian
Aggressive
0
0
0
2
Fitz
Hamilton
London
6
American
Passive
0
0
0
Do you see any major Flaws in this Approach?
Last edited by manwithmanykids (June 3, 2013 18:16:58)
- mwiedmann
- Scratcher
100+ posts
A Better way of creating a 2 dimensional List
That approach would definitely work. One that I've used before with good results is to pack each object with its properties together into a string (with delimiter) and store in the list that way. Example with your data:
Traders:
1|Jack|Toronto|London|5|Canadian|Aggressive|0|0|0
2|Fitz|Hamilton|London|6|American|Passive|0|0|0
Pros: Keeps your lists a little more manageable/readable, easy to add new properties, the “ID” of the object is the same as it's index in the list.
Cons: You have to unpack each time you want to read a property. Also have to re-pack after setting a property.
I have the pack/unpack routines saved in 1.4 somewhere. I'll see if I can dig them up, but they aren't too hard to write. You basically just use a loop to unpack the string into a separate list called “Properties”. Then you can read/set them however you want and repack them when done (again a simple loop).
Sounds like a good game idea. Look forward to seeing it!
Traders:
1|Jack|Toronto|London|5|Canadian|Aggressive|0|0|0
2|Fitz|Hamilton|London|6|American|Passive|0|0|0
Pros: Keeps your lists a little more manageable/readable, easy to add new properties, the “ID” of the object is the same as it's index in the list.
Cons: You have to unpack each time you want to read a property. Also have to re-pack after setting a property.
I have the pack/unpack routines saved in 1.4 somewhere. I'll see if I can dig them up, but they aren't too hard to write. You basically just use a loop to unpack the string into a separate list called “Properties”. Then you can read/set them however you want and repack them when done (again a simple loop).
Sounds like a good game idea. Look forward to seeing it!
Last edited by mwiedmann (June 3, 2013 18:41:30)
- scimonster
- Scratcher
1000+ posts
A Better way of creating a 2 dimensional List
Try using the technique shown here.
- manwithmanykids
- Scratcher
67 posts
A Better way of creating a 2 dimensional List
That approach would definitely work. One that I've used before with good results is to pack each object with its properties together into a string (with delimiter) and store in the list that way. Example with your data:
Traders:
1|Jack|Toronto|London|5|Canadian|Aggressive|0|0|0
2|Fitz|Hamilton|London|6|American|Passive|0|0|0
Pros: Keeps your lists a little more manageable/readable, easy to add new properties, the “ID” of the object is the same as it's index in the list.
Cons: You have to unpack each time you want to read a property. Also have to re-pack after setting a property.
I have the pack/unpack routines saved in 1.4 somewhere. I'll see if I can dig them up, but they aren't too hard to write. You basically just use a loop to unpack the string into a separate list called “Properties”. Then you can read/set them however you want and repack them when done (again a simple loop).
Sounds like a good game idea. Look forward to seeing it!
The Pack routine is what I am doing in my example. The more I think about it, the more I am leanig toward using this method:
Traders:
1
Jack
Toronto
London
5
Canadian
Aggressive
0
0
0
2
Fitz
Hamilton
London
6
American
Passive
0
0
0
My concern now is what is hte limit to the number of items I can put in a list comfortably.
I am thinking of a map that is 100X100 which would be stored as a single list with 10,000 items. Is that pushing the limits of Scratch?
- ErnieParke
- Scratcher
1000+ posts
A Better way of creating a 2 dimensional List
No, it isn't. I've even seen a list or two that go way beyond that and that push the millions. The issue isn't that Scratch can't handle lists that size, but wether your program will be able to handle that much data in a smooth, lag-less way. That, and it takes a long while to fill super-large lists.That approach would definitely work. One that I've used before with good results is to pack each object with its properties together into a string (with delimiter) and store in the list that way. Example with your data:
Traders:
1|Jack|Toronto|London|5|Canadian|Aggressive|0|0|0
2|Fitz|Hamilton|London|6|American|Passive|0|0|0
Pros: Keeps your lists a little more manageable/readable, easy to add new properties, the “ID” of the object is the same as it's index in the list.
Cons: You have to unpack each time you want to read a property. Also have to re-pack after setting a property.
I have the pack/unpack routines saved in 1.4 somewhere. I'll see if I can dig them up, but they aren't too hard to write. You basically just use a loop to unpack the string into a separate list called “Properties”. Then you can read/set them however you want and repack them when done (again a simple loop).
Sounds like a good game idea. Look forward to seeing it!
The Pack routine is what I am doing in my example. The more I think about it, the more I am leanig toward using this method:
Traders:
1
Jack
Toronto
London
5
Canadian
Aggressive
0
0
0
2
Fitz
Hamilton
London
6
American
Passive
0
0
0
My concern now is what is hte limit to the number of items I can put in a list comfortably.
I am thinking of a map that is 100X100 which would be stored as a single list with 10,000 items. Is that pushing the limits of Scratch?
As for your pack method above, I'd just like to chip in and say that it's quite useful. I even used it City Tycoon II to store all of the tiles on the floor, and it worked out great. So, if you use it, you should be good. ;)
With regards,
ErnieParke
- manwithmanykids
- Scratcher
67 posts
A Better way of creating a 2 dimensional List
way beyond that and that push the millions. The issue isn't that Scratch can't handle lists that size, but wether your program will be able to handle that much data in a smooth, lag-less way. That, and it takes a long while to fill super-large lists.No, it isn't. I've even seen a list or two that go
As for your pack method above, I'd just like to chip in and say that it's quite useful. I even used it City Tycoon II to store all of the tiles on the floor, and it worked out great. So, if you use it, you should be good. ;)
With regards,
ErnieParke
Thanks Ernie
I have Played City tycoon, (Actually had a race with my wife to see who could get all the trophies first lol)
as for the list, My thought was that I would only fill what I need to fill when I need it filled, so I think i should be ok (no sure though) will play with it and see
- ErnieParke
- Scratcher
1000+ posts
A Better way of creating a 2 dimensional List
That's always good to hear.way beyond that and that push the millions. The issue isn't that Scratch can't handle lists that size, but wether your program will be able to handle that much data in a smooth, lag-less way. That, and it takes a long while to fill super-large lists.No, it isn't. I've even seen a list or two that go
As for your pack method above, I'd just like to chip in and say that it's quite useful. I even used it City Tycoon II to store all of the tiles on the floor, and it worked out great. So, if you use it, you should be good. ;)
With regards,
ErnieParke
Thanks Ernie
I have Played City tycoon, (Actually had a race with my wife to see who could get all the trophies first lol)
as for the list, My thought was that I would only fill what I need to fill when I need it filled, so I think i should be ok (no sure though) will play with it and see
Regarding CT II, I've never heard of two people holding a race to see if you could finish City Tycoon first. You two are quite fun. ;)
With regards,
ErnieParke
- Discussion Forums
- » Help with Scripts
- » A Better way of creating a 2 dimensional List