简体   繁体   中英

Create an event that is connected to the change of a variable value?

I have issues with the following problem in AnyLogic:

I would like to create an event that is connected to a changing value of another variable. That variable can wheter be 1 or -1. At any moment it changes from -1 to 1, that event shall be triggered, but only once per occasion. Does anyone have a solution for this?

I already thought about implementing an boolean that checks that change, but I wouldn't know how.

You need to approach this the other way around.

You are trying to "monitor" the variable and wait for a change of it.

Instead, you should only allow 1 way to change that variable and add any additional model changes that come from it.

In OOP, you do it by:

  1. setting your variable myVariable to access level private
  2. adding a public function setMyVariable(int newValue) next to it
  3. in the function, you have the code myVariable = newValue; AND any other code you want to be triggered whenever newValue==1 or newValue==-1
  4. change your model such that myVariable is only ever changed via the function setMyVariable(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM