Discuss Scratch

ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

Well, you read the title. In my game, I have a list with different items being the different sales for different car models. How would I make a variable be the measure of all the list items combined?

Thanks!

Call me Toad, my dude.
StrangeMagic32
Scratcher
1000+ posts

How to Create Variable that is the Sum of All Items on a Changing List

Do you want it to add up the total prices or…?


“Remember the worth of souls is great in the sight of God;”
- Doctrine and Covenants 18:10


I have since moved to @JollyWinter
ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

StrangeMagic32 wrote:

Do you want it to add up the total prices or…?
I want to make it so there is an ever changing total of the number of cars sold. By “sales” I mean number of times the car was bought. The list has multiple rows with multiple increasing values for each item.

Basically, I just want to know if there is a way to get the sum of all list items. Or if not, alternatives.

Call me Toad, my dude.
matthewparkerstas
Scratcher
65 posts

How to Create Variable that is the Sum of All Items on a Changing List

when green flag clicked
show variable [money gained v]
forever
set [money gained v] to ((item (1 v) of [list v] :: list) + (item (2 v) of [list v] :: list))
end

Will this work?

It's free real estate

50th post on February 5th, 2018
deck26
Scratcher
1000+ posts

How to Create Variable that is the Sum of All Items on a Changing List

Why can't you just loop through the list and add the value? If the list is big you could keep a running total so every time you add to the list you add the new value to your sum or if you're deleting an item you subtract it first. If replacing an item subtract the old value and add the new one.
ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

matthewparkerstas wrote:

when green flag clicked
show variable [money gained v]
forever
set [money gained v] to ((item (1 v) of [list v] :: list) + (item (2 v) of [list v] :: list))
end

Will this work?
No, because the number of items would be changing.

Call me Toad, my dude.
ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

deck26 wrote:

Why can't you just loop through the list and add the value? If the list is big you could keep a running total so every time you add to the list you add the new value to your sum or if you're deleting an item you subtract it first. If replacing an item subtract the old value and add the new one.

The list is constantly getting longer, and each item on it is getting bigger.

Call me Toad, my dude.
AveryBrodyA
Scratcher
41 posts

How to Create Variable that is the Sum of All Items on a Changing List

Do something like this:

when green flag clicked
forever
set [oldLength v] to (length of [list v] :: list)
wait until <not <(oldLength) = (length of [list v] :: list)>>
set [sum v] to [0]
set [timeAround v] [0]
repeat (length of [list v] :: list)
change [sum v] by (item (timeAround) of [list v] :: list)
change [timeAround v] by (1)
end
end

this basically waits until the list's length changes. When it does, it resets the sum and count and adds everything to it by counting up to the total list's length.
ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

AveryBrodyA wrote:

Do something like this:

when green flag clicked
forever
set [oldLength v] to (length of [list v] :: list)
wait until <not <(oldLength) = (length of [list v] :: list)>>
set [sum v] to [0]
set [timeAround v] [0]
repeat (length of [list v] :: list)
change [sum v] by (item (timeAround) of [list v] :: list)
change [timeAround v] by (1)
end
end

this basically waits until the list's length changes. When it does, it resets the sum and count and adds everything to it by counting up to the total list's length.
Awesome! Thanks for the help!

Call me Toad, my dude.
deck26
Scratcher
1000+ posts

How to Create Variable that is the Sum of All Items on a Changing List

ToadTV wrote:

deck26 wrote:

Why can't you just loop through the list and add the value? If the list is big you could keep a running total so every time you add to the list you add the new value to your sum or if you're deleting an item you subtract it first. If replacing an item subtract the old value and add the new one.

The list is constantly getting longer, and each item on it is getting bigger.
Doesn't change anything I said.
ToadTV
Scratcher
77 posts

How to Create Variable that is the Sum of All Items on a Changing List

deck26 wrote:

ToadTV wrote:

deck26 wrote:

Why can't you just loop through the list and add the value? If the list is big you could keep a running total so every time you add to the list you add the new value to your sum or if you're deleting an item you subtract it first. If replacing an item subtract the old value and add the new one.

The list is constantly getting longer, and each item on it is getting bigger.
Doesn't change anything I said.

Oh, I understand now. Thanks!

Call me Toad, my dude.
siakap_keli09
Scratcher
2 posts

How to Create Variable that is the Sum of All Items on a Changing List

matthewparkerstas wrote:

when green flag clicked
show variable [money gained v]
forever
set [money gained v] to ((item (1 v) of [list v] :: list) + (item (2 v) of [list v] :: list))
end

Will this work?

Powered by DjangoBB