Discuss Scratch

Skibiji
Scratcher
10 posts

Storing Sprite Positions Via Cloud Variables

I'm trying to create a game inspired by clash royale in scratch, and I currently need to figure out how to store sprite positions of the enemy team. I already have a match ID to differentiate between games that are being played at the same time and have figured out how to store the data for a single sprite's x and y position and direction, all in one cloud variable, but I can't figure out how to store more than 1 sprite's data in a single cloud variable. Can someone help?
Dakota_The_Coder
Scratcher
100+ posts

Storing Sprite Positions Via Cloud Variables

I've tried to do something like this before, but I haven't managed to pull it off yet. My suggestion would be to send each sprite's position through the cloud variable one frame at a time. For example, in the first frame, you would send the first sprite's data through the cloud; in the second frame, the second sprite's data, etc. Just keep looping around like that forever, and that should do it.
Skibiji
Scratcher
10 posts

Storing Sprite Positions Via Cloud Variables

That idea didn't even cross my mind! I was thinking something like storing them all at once in a single cloud variable. The only downside to your idea is that cloud variables only update up to 10 times a second, so with lots of sprites on screen, it would be extremely laggy. However, I think your idea will be a good place to start!
Dakota_The_Coder
Scratcher
100+ posts

Storing Sprite Positions Via Cloud Variables

Skibiji wrote:

That idea didn't even cross my mind! I was thinking something like storing them all at once in a single cloud variable. The only downside to your idea is that cloud variables only update up to 10 times a second, so with lots of sprites on screen, it would be extremely laggy. However, I think your idea will be a good place to start!
You're welcome!
g6g6g66g6g
Scratcher
100+ posts

Storing Sprite Positions Via Cloud Variables

Skibiji wrote:

I was thinking something like storing them all at once in a single cloud variable.

This is the better solution because you can fit a lot more data in a cloud variable. The trick is to first assign a number to every character you will send, e.g.

  • a -> 01
  • b -> 02
  • c -> 03


The easiest way to convert characters to numbers is to put them in a list and use the
(item # of (char) in [chars v])
block.

From there, add a 0 to the start if it is a single digit, so that all characters convert to a 2 digit code.

After that, add the number of characters to the start of the string, then add the length of that number (the number of characters) to the start as well, so you know how far to read.

Example: Let's convert 2 values
  • user
  • -123
1. Convert to numbers
  • 21 19 5 18
  • 51 27 28 29
2. Insert 0 to single digit numbers
  • 21 19 05 18
  • 51 27 28 29
combine
  • 21190518
  • 51272829
4. add number of characters (“user” and “-123” are both 4) to start
  • 421190518
  • 451272829
5. add length of number of characters (length of “4” = 1) to start
  • 1421190518
  • 1451272829
6. Combine
14211905181451272829

DEMO

Powered by DjangoBB