Variables set to change in a loop aren't changing

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • So my original program was a simulation of the ideal gas law; users could set the number of molecules in the container, the size of the container and the temperature to see how it affected pressure. The updated program that I am currently working on is designed to automatically test a set of values for volume, temperature, and number of molecules and record those along with the pressure in a list object. I'm using a for loop to change the variables each time, then record the pressure and reset the container for the next iteration of the loop.

    The problem I'm having though is that upon the start of the program the loop is automatically executed, so while the values for molecules, temperature and volume are correct, pressure is displayed as zero for each one. If I set the loop to wait between each iteration, then the pressure is tested and recorded correctly, but temperature, molecules and volume all stay at one each time.

    tl;dr - A loop starting with a wait command doesn't set variables to desired values on each iteration, how can I make it so the values update?

    Here is the link to my google drive folder with the capx files for both the original and revised programs

    drive.google.com/drive/folders/1tZc73p894fMpW88PwF2FtH4P7iqpuWZq?usp=sharing

  • Yes, if you use "wait" in a loop, you can't access any temporary variables (local variables, loopindex expression, function parameters etc.) after the "wait" action.

    You need to use either global variables, instance variables or static local variables.

    For example, this will not work:

    For i=1 to 10 -> Wait 0.5*loopindex ; Create Sprite at (loopindex*50, 200)

    You have to change it to something like this:

    Local Static variable x=0

    For i=1 to 10

    ... Wait 0.5*loopindex

    ... Add 1 to x

    ... Create Sprite at (x*50, 200)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yes, if you use "wait" in a loop, you can't access any temporary variables (local variables, loopindex expression, function parameters etc.) after the "wait" action.

    You need to use either global variables, instance variables or static local variables.

    For example, this will not work:

    For i=1 to 10 -> Wait 0.5*loopindex ; Create Sprite at (loopindex*50, 200)

    You have to change it to something like this:

    Local Static variable x=0

    For i=1 to 10

    ... Wait 0.5*loopindex

    ... Add 1 to x

    ... Create Sprite at (x*50, 200)

    Yes, thanks a ton, I got it to work. Before posting this I tried using a separate static variable so not sure exactly what I was doing wrong, but following your syntax worked great.

Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)