Discuss Scratch

carteradams123
Scratcher
49 posts

Making RTS game- need a way to keep track of the location of multiple cloned sprites outside of frame.

Hello! I'm trying to make an RTS game in scratch, and would like some help with the following problem I ran into.

I want to clone a lake sprite and have them all stay in place on the map.

I want the map to be large, meaning I have to implement a scrolling mechanic.

I can keep sprites in place by having them constantly move to a specific X and Y position written in a Variable or List, moving these positions in the opposite direction the user is scrolling in to give the illusion that the frame is moving. I can also hide the sprite when it reaches the edge, or make it appear again when returning to the frame.

The problem comes with a limitation of my scrolling mechanic- If I clone a sprite, something required for units/building, and would make my life far easier if used for lakes, how do I keep track of it's intended X and Y position?

to summarize the above because I worry it's unclear: sprite “A” makes 10 clones. How do I keep track of where each clone is when they are off-frame?

I tried to think of multiple solutions.
Could I use variables? How do I make each clone move to their specific variable? what happens if I run out of variables to assign?
Could I use lists? How do I make each clone move to their specific list? how do I keep track of it all when new list entries are being made/deleted?

Do I have to make a unique sprite for every single possible feature/unit, and implement a unit cap so you don't build more units then there are sprites?

Any tips, suggestions, or walkthroughs would be appreciated. If you need more clarification, feel free to ask in the comments!

I am either working hard or hardly working.
WeirdBoi_test
Scratcher
10 posts

Making RTS game- need a way to keep track of the location of multiple cloned sprites outside of frame.

Better yet, just don't use clones! Use some render block that renders all of the lakes for you!
carteradams123
Scratcher
49 posts

Making RTS game- need a way to keep track of the location of multiple cloned sprites outside of frame.

WeirdBoi_test wrote:

Better yet, just don't use clones! Use some render block that renders all of the lakes for you!

How do I do that? I assume you use the pen system, which I don't have much experience in. This also doesn't account for the fact that I still need to make Units/Buildings.

I am either working hard or hardly working.
MasterofTheBrick
Scratcher
1000+ posts

Making RTS game- need a way to keep track of the location of multiple cloned sprites outside of frame.

Firstly, you would need a way to differentiate between all the clones which are being created. The solution to this is simple: All you have to do is to assign a value to each clone using local variables (a variable which can only be accessed by 1 sprite), otherwise known as a “clone ID”. In case you didn't know about this method already, here's an example of how it is generally used.

define create clone type (clone id)
set [ID v] to (clone id) //local var
create clone of [myself v]

when I start as a clone
if <(ID) = [1]> then
scripts for clone 1 ::grey
else
if <(ID) = [2]> then
scripts for clone 2 ::grey
else
and so on ::grey
end
end

Next, we would require the use of global lists (a list which is available to all sprites) to store and keep track of multiple pieces of data at once, or in this case, the positions of each clone. To do this, we will have to make a little adjustment to the custom block above:

when green flag clicked //add this in
set [ID v] to [-1]

define create new lake clone
change [ID v] by (2) //still a local var
repeat (2)
add (ID) to [list v] //you don't necessarily have to add the ID, just make sure that a list item is added
end
create clone of [myself v]

Since list items have values assigned to them (list item 1, 2, 3), we can make use of these values and match them to the clones with their corresponding IDs. Clone with ID #1 would edit the first and second item in the list, clone #3 would edit the third and fourth, and so on. Under normal circumstances the ID variable would be increased by 1 every time a clone was created, but in this case since each clone has to keep track of 2 list items (x and y positions), the ID is increased by intervals of 2.

when I start as a clone
forever
movement scripts ::grey
replace item (ID) of [list v] with (x position)
replace item ((ID) + (1)) of [list v] with (y position)
end

The list now contains constantly updating values of the respective clone's x and y positions. Alternatively, you can control the movement of all the clones using an external script that edits the list itself.

define move clone # (clone id) to x (x pos) y (y pos)
replace item (clone id) of [list v] with (x pos)
replace item ((clone id) + (1)) of [list v] with (y pos)

when I start as a clone
forever
go to x: (item (ID) of [list v] :: list) y: (item ((ID) + (1)) of [list v] :: list)
end

Last edited by MasterofTheBrick (Aug. 29, 2021 03:27:57)

Powered by DjangoBB