Search code examples
javascriptloopstwinesugarcube

Sugarcube game editing using for loops


I'm very new to Sugarcube, I learned that you can edit values by inspecting on the browser. My question is how do you use a loop to change the value of a bunch of things at once? For instance if a game looks like this

ShoppingCart

Milk cost=10

Meat cost=20

Bread cost=30

How do I make a loop to say

for i in ShoppingCart:

i.cost = 10;

end

or something to that effect? I have some coding knowledge of other languages but not this. Until now I've been doing

SugarCube.State.variables.ShoppingCart.Milk=10

SugarCube.State.variables.ShoppingCart.Meat=10

SugarCube.State.variables.ShoppingCart.Bread=10

but its getting tedious, please help


Solution

  • For in loops through key

    const cart = SugarCube.State.variables.ShoppingCart
    for(let i in cart) {
       if(cart[i].cost != undefined ) continue; 
       cart[i].cost = 10 
    }