Replacing multiple items in a list simultaneously

I have a sprite named Player, which I am hoping to make a parent of several other Player objects (eg little soccer players) so I don’t need to redo the code in each.

I want each one to save its starting location, and then its ending location after it has been moved.

I figured I’d use lists, where index 1 and 2 would be the old position x and y, and 2 and 4 would be the new x and y. And I figured using copy list might make each list unique to that particular sprite.

However, when I try to add an x and y position simultaneously, the list only accepts one number.

What’s the sensible way of doing this?

( @CodeAlpaca since you asked me to tag you, this is me doing it :slight_smile: )

2 Likes

Try using a Repeater, Counter (Number) and a Router

The repeater will output to a Number in a fast order and the router will separate that order so you can do one at a time but still be done in 1 frame.

2 Likes

you dont really need to redo the same code in each object. there is a trick called copy and pasting code. Sorry if i understood something wrong and u meant something else. Edit: or just do what JR01 said.

Actually, this isn’t the problem.
Both numbers are counted in the list, it’s just the output of the modify block is outputting its own version of the list. And that’s what you’re seeing.

What you need to do instead is use a Once for the “all” input,
then use a List Count block after the second modify block to input back into “all”.
Which then should output the correct list.

1 Like

Ah, I see what it’s doing now. So as you said, Number List Modify spits out its own list.
However, I’m not clear on how counting the items in the list helps merge the lists, so I’m not sure where to put List Count or which “all” to connect it to?

(For those following along, you can see at the end that each Modify block creates its own list)

Copy pasting code does work if necessary, but I was hoping to not have to create custom variables for each of 22 sprites, so I’m trying to make one parent sprite with code that I can modify in one place if there is an issue and have it populate all the other child sprites automatically :slight_smile:

The List Count is just to change output type, not actually counting the list.
2nd List modify → List Count → “all” in the first list (the same one you connect Once to).

Also for this, set the List modify to just replace. remove the Copy checkbox.

2 Likes

Great, that works, thank you very much!
Now curious to see how that affects child sprites, do they get their own lists with their own data (eg their own locations) or do they all end up with the parent sprite’s location.

1 Like

They should have their own lists since you’re using an extractor.

1 Like

A final “clean code” question, in case you still have the patience for it…
This code works. It outputs a list with Old X, Old Y, New X, New Y.
However, it seems like a pretty ugly solution. How would one do this the “right” way, or is this it?
(oops, just noticed I used insert instead of replace in the last two, but even so, wondering about the above) Ideally I just want one list, right?

Ah crap, this code doesn’t work because when the top code is triggered once (after I removed the Always I was using for testing), it only adds x to the list, and it has to be triggered a second time to add y to it as well :confused:

All I want to do is have the user press a button, and have that store a sprite’s location in a list.
And then have the user move the sprite, press the button again, have it store the new location, and then move the sprite back to the original location.

To make a cleaner code for what you need, could you explain in a bit more detail on how it’s suppose to work? Also if you could DM the game link, it would help a lot too.

From a user perspective, the ultimate goal, where “players” in this case refers to the sprites (soccer players), is the following:

Click begin, move your players to where you want them to go once the game begins

Once that’s set, click the button again. Your players will go back to their original positions.

(Other team goes through the same process.)

Once both moves have been recorded and all players are in their original positions, press the button again and have all players execute their “recorded” moves simultaneously, eg all go to their new positions at the same time.

From a code perspective, I want to store OldPositionX, OldPositionY, NewPositionX, and NewPositionY somewhere, probably in a single list that is unique to each player sprite. And then replace those values as the game progresses.

I’ve been working on this in the behaviors of the top left red sprite, intending to make it a parent of other sprites. You can ignore the other sprites, they contain other experiments towards the same ends.

Ofc your pathfinding code (where you click multiple spots to record a more complex movement) would be ideal for this especially if I could make it leave a visual trail, but I found dissecting/modifying it for that to be too complex for my current skills (would have to apply to each sprite one at a time)

Hope that clarifies :slight_smile:

This code, which does not use lists, is cleaner and works. But I don’t know if each child will copy those NewX Old X variable values from the parent, and then all end up in the parent’s location instead of their own:

1 Like

Unless you use a global or save, the child objects should output their own values.

1 Like

So now I have solved how I can make my game work, but I still don’t know how to add multiple items to a list simultaneously, or whether that’s even possible without making copies of a list. As in:

Add “12” to index 1 and “34” to index 2

I made my own edit to what you could do in the players for simplicity, but I’ll try to go into more in detail about lists right quick.

Now more about lists; lists are cool and allows more complex stuff, but can be even more complex to set up correctly. To “Add 12 to index 1”, you would need to grab the value at index 1, add 12, and then replace index 1 by using a modified list block.

To do what I did in the first image but with lists, it would actually be a lot more complex because a. replacing every index in a list is a lot of work, so you would probably just reset the list instead. And b. it’s just much simpler to do with numbers instead of lists because of how small the list is.

1 Like

Ahhh ok this helps a lot, thanks!

Interesting, your simplified code example seems to imply that the extractor stores its value even when another extractor stores a new value. Didn’t know that.

The ease behavior stores the input, but only activates when both inputs are used. The extractor just outputs the current value.

1 Like