Confusing loop error.

TheHerb

Member
Reaction score
3
When an item is placed in a region, it will cause another item of the same type to be generated and placed in the same region every 5 seconds, so long as there is still an item there, and the number of that item hasn't exceeded a predefined integer.

Trigger:
  • Item - Pick every item in Farm1 <gen> and do (Actions)
    • Loop - Actions
      • -------- -------- --------
      • -------- Crop 1 --------
      • -------- -------- --------
      • Set CropsCount[1] = 0
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Item-type of (Picked item)) Equal to Crops[1]
        • Then - Actions
          • Set CropsCount[1] = (CropsCount[1] + 1)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • CropsCount[1] Less than MaxCrops
              • CropsCount[1] Greater than 0
            • Then - Actions
              • Item - Create Crops[1] at (Random point in Farm1 <gen>)
            • Else - Actions
        • Else - Actions


However, this trigger, instead of creating 1 new item, creates double the amount of that item in the region, that is i place one, 5 seconds later there's two, 5 seconds later four, five seconds later eight... and so on.

What could be causing this?
 

Zwiebelchen

You can change this now in User CP.
Reaction score
60
That is because you have basicly two loops in this trigger.

The first loop is "Pick every item in ...".

So if you have 2 items in the region, it will run the inside twice.

The second loop creates your amount of new items like intended.

So basicly, you need to remove that Pick line.
 

Nherwyziant

Be better than you were yesterday :D
Reaction score
96
So you want to create another one same item on the region, not doubling it until all items are removed?

replace this 2 condition
Trigger:
  • * CropsCount[1] Less than MaxCrops
    • * CropsCount[1] Greater than 0


with

Trigger:
  • CropsCount[1] equal to 1


then
put this
Trigger:
  • Set CropsCount[1] = 0


above

Trigger:
  • Item - Pick every item in Farm1 <gen> and do (Actions)
 

TheHerb

Member
Reaction score
3
EDIT: just read your post Nherqyziant, i'll try that

EDIT 2: I tried what you said and it stops the doubling, however it doesn't stop adding an extra item after it reaches the limit. It just keeps going, what could cause this do you think?

Updated trigger:
Trigger:
  • Growing 1
    • Events
    • Conditions
    • Actions
      • Set CropsCount[1] = 0
      • Item - Pick every item in Farm1 <gen> and do (Actions)
        • Loop - Actions
          • -------- -------- --------
          • -------- Crop 1 --------
          • -------- -------- --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item-type of (Picked item)) Equal to Crops[1]
            • Then - Actions
              • Set CropsCount[1] = (CropsCount[1] + 1)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • CropsCount[1] Less than MaxCrops
                  • CropsCount[1] Equal to 1
                • Then - Actions
                  • Item - Create Crops[1] at (Random point in Farm1 <gen>)
                • Else - Actions
            • Else - Actions
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
Assuming that you run this trigger every so often, your variable gets set back to 0 every time it is run. This:

Trigger:
  • Set CropsCount[1] = 0
 

TheHerb

Member
Reaction score
3
Assuming that you run this trigger every so often, your variable gets set back to 0 every time it is run. This:

Trigger:
  • Set CropsCount[1] = 0

I just thought of that, so i disabled that function and what happens is, i place the item, 5 seconds later another one is spawned, but then it doesn't spawn any more.
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
Trigger:
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • CropsCount[1] Less than MaxCrops
      • CropsCount[1] Equal to 1
    • Then - Actions
      • Item - Create Crops[1] at (Random point in Farm1 <gen>)
    • Else - Actions


Both of those conditions must be true, yet the variable is only equal to 1 once since it then gets increased to 2.
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
It sounds like what you do not want is the loop. You just want to copy an item that is there. Try adding a custom script line right after the line where you create the new item. Like so:

Trigger:
  • Custom script: exitwhen true


It's a bit sloppy, but it should work.
 

TheHerb

Member
Reaction score
3
It sounds like what you do not want is the loop. You just want to copy an item that is there. Try adding a custom script line right after the line where you create the new item. Like so:

Trigger:
  • Custom script: exitwhen true


It's a bit sloppy, but it should work.

"Unexpected 'exitwhen'"
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
Ah, drat. Pick every item doesn't have a loop in it. Ok, on to Plan B (tm).

Create a new integer variable. I'll call it Temp for now. Then here is the scheme:

Trigger:
  • Growing 1
    • Events
    • Conditions
    • Actions
      • Set Temp = 0
      • Item - Pick every item in Farm1 <gen> and do (Actions)
        • Loop - Actions
          • -------- -------- --------
          • -------- Crop 1 --------
          • -------- -------- --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item-type of (Picked item)) Equal to Crops[1]
            • Then - Actions
              • Set CropsCount[1] = (CropsCount[1] + 1)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • CropsCount[1] Less than MaxCrops
                  • CropsCount[1] Equal to 1
                  • Temp Equal to 0
                • Then - Actions
                  • Item - Create Crops[1] at (Random point in Farm1 <gen>)
                  • Set Temp = Temp + 1
                • Else - Actions
            • Else - Actions
 

TheHerb

Member
Reaction score
3
Ok, I tried that, and it looked as if it was working, but as soon as it gets to 5, it stops spawning, even if i remove all of them, and then place another.

Trigger:
  • Growing 1
    • Events
    • Conditions
    • Actions
      • Set tempInteger = 0
      • Item - Pick every item in Farm1 <gen> and do (Actions)
        • Loop - Actions
          • -------- -------- --------
          • -------- Crop 1 --------
          • -------- -------- --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item-type of (Picked item)) Equal to Crops[1]
            • Then - Actions
              • Set CropsCount[1] = (CropsCount[1] + 1)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • CropsCount[1] Less than 10
                  • CropsCount[1] Greater than 0
                  • tempInteger Equal to 0
                • Then - Actions
                  • Item - Create Crops[1] at (Random point in Farm1 <gen>)
                  • Set tempInteger = (tempInteger + 1)
                • Else - Actions
            • Else - Actions
 

Accname

2D-Graphics enthusiast
Reaction score
1,464
Trigger:
  • Item - Pick every item in Farm1 <gen> and do (Actions)
    • Loop - Actions
      • -------- -------- --------
      • -------- Crop 1 --------
      • -------- -------- --------
      • Set CropsCount[1] = 0
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Item-type of (Picked item)) Equal to Crops[1]
        • Then - Actions
          • Set CropsCount[1] = (CropsCount[1] + 1)
        • Else - Actions
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • CropsCount[1] Less than MaxCrops
        • CropsCount[1] Greater than 0
      • Then - Actions
        • Item - Create Crops[1] at (Random point in Farm1 <gen>)
      • Else - Actions


actually i would say all you have to do is this, move the item generation outside the loop. the loop will run through counting all items and saving the number in CropsCount[1] and after it looped through all items you check the conditions and create a new item if needed.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top