Problem with loop function

So.. I have a problem with the loop "for" function.

So this loop function that I tried to make, adds lines to a certain "manifest" based on the number of population.
If there are two people then it adds two lines, so far so good.

But the problem arises when there is suddenly four people, the loop decides to fire when it is not supposed to.
Resulting in FIVE lines for four people.
I have put log scripts near the other scripts so I could pinpoint the exact moment when it happens and it is all confusing.

I am not sure if this will help

26/01/2017 19:32:12 Removed 1 pop to stabilize
26/01/2017 19:32:12 Added 1 pop to stabilize<----- NOT SUPPOSED to be here!
26/01/2017 19:32:12 Removed 1 pop to stabilize
26/01/2017 19:32:12 Added 1 pop to stabilize
26/01/2017 19:32:12 Added 1 pop to stabilize

And um... I have no idea how to exactly describe the nature of my coding so I unfortunately have to paste the whole code.

http://pastebin.com/zbQq0D21

So the problem lies in page "X03_Manifest" (Press CTRL+F and type that, so you don´t have to search trough the code)

The problem lies in Manifestscriptcall() which is combination of ManifestNameRNG() and ManifestAgeRNG()
So it is kinda complicated...

  <function name="Manifestscriptcall" type="string">
    player.manifestRNG = player.manifestRNG + 1
    player.population = player.population - 1
    Log ("Removed 1 pop to stabilize")
    for (, player.manifestRNG, player.population, 1) {
      ManifestLoop
    }
    player.population = player.population + 1
    Log ("Added 1 pop to stabilize")
    return (ManifestNameRNG() + ManifestAgeRNG())
  </function>

So if anyone could help me point out what am I doing wrong, it would help me because I am stuck at this and it makes no sense as why is it happening.


I haven't looked at all of your code (I'd need to see what your 'ManifestLoop',' ManifestNameRNG', and 'ManifestAgeRNG' does), but let's just look at the process of your 'Manifestscriptcall' Function:

  1. player.manifestRNG = player.manifestRNG + 1
  2. player.population = player.population - 1
  3. Log ("Removed 1 pop to stabilize")
  4. for (, player.manifestRNG, player.population, 1) {

for #4, it will repeat/iterate/run, running the 'ManifestLoop' Function, a number of times based upon the difference of 'player.population - player.manifestRNG + 1'

(also, you might have a problem with increasing your 'player.ManifestRNG' as you also are decreasing your 'player.population', for example: 0~5: 6 runs of 'ManifestLoop', 1~4: 4 runs of 'ManifestLoop', 2~3: 2 runs of 'ManifestLoop', 3~2: ERROR or 0 runs of 'ManifestLoop')

  1. AFTER #4 is done with however many iterations/repeats/run of 'ManifestLoop', (aka: #5, will WAIT/PAUSE UNTIL #4 is fully done, before #5 is run/done), it (#5) does this:

player.population = player.population + 1

  1. Log ("Added 1 pop to stabilize") // this looks like it is your unwanted line being displayed (which may mean that you don't want #5 either, as it is actually doing the increasing of the 'player.population' Integer Attribute)
  2. return (ManifestNameRNG() + ManifestAgeRNG())

note that you are altering your Attributes' Values, and not resetting them to their default/initial/starting Values, after you're done with your 'Manifestscriptcall' --- this could cause problems/issues


okay... your 'ManifestLoop' is (calls) your 'manifestscriptcall'...

you thus got some middle recursion going on, and I'm not sure if this is what you want... I don't really understand your code design, but it seems to be too complex (not your fault if not an expert coder of course, lol), it could probably be made more concise/efficient.

here's what's going on due to the middle recursion:

(I'll make a new post actually)


your initial/default/starting Attributes' Values form your code:

SetCounter ("TimeDay", 1)
        SetCounter ("TimeMonth", 1)
        SetCounter ("TimeYear", 2361)
        SetCounter ("Food", 10)
        player.water = 10
        player.colonistids = 0
        player.manifestnamenum = 1
        player.manifestname = 1
        player.manifestnameRNG = 0
        player.manifestageRNG = 0
        player.manifestRNG = 0
        player.population = 1
        player.idlepopulation = 1
        player.watergatherer = 0
        player.explorer = 0
        player.explorationpoints = 0
        SetCounter ("Credits", 1000)
        SetFlagOn ("DEBUG")

  <function name="Manifestscriptcall" type="string">
    player.manifestRNG = player.manifestRNG + 1
    player.population = player.population - 1
    Log ("Removed 1 pop to stabilize")
    for (, player.manifestRNG, player.population, 1) {
      ManifestLoop
    }
    player.population = player.population + 1
    Log ("Added 1 pop to stabilize")
    return (ManifestNameRNG() + ManifestAgeRNG())
  </function>

here's what's going on with your middle recursion:

(assuming that you're using your default/starting/initial Attributes' Values)

  1. player.manifestRNG = player.manifestRNG + 1 // 0 + 1 = 1

  2. player.population = player.population - 1 // 1 - 1 = 0

  3. Log ("Removed 1 pop to stabilize")

  4. for (, player.manifestRNG, player.population, 1) { // from 1 to 0 iterations: this should be an ERROR or 0 recursion calls, or maybe it's going in reverse order... which my brain can't even begin to follow (I have trouble enough with simple recursion), laughs.

  5. pretending that there's no issues with #4... let's say it's going to just do a single recursion (a single iteration, and thus a single call of 'ManfestLoop', which calls your 'manifestscriptcall', which is a single recursion call), for example: 'player.ManifestRNG=1' and 'player.population = 1':

  6. player.manifestRNG = player.manifestRNG + 1 // 1 + 1 = 2

  7. player.population = player.population - 1 // 1 - 1 = 0

  8. let's say it's thus unable to do any (2nd level) recursion calls, so it's going back


ERR... this is too ahrd for me... simple recursion is hard enough for me to explain/follow... and the middle recursion and details of your code, make this just too confusing for me... I'm not even understanding how it's even working at all, as it seems like it shouldn't be, to me, anyways.


So first, thanks for pasting the whole code, that helps. However, as you say, it is complicated. I wondered if it was because you had not set a loop variable, or you were changing the values of the start and end of the loop inside the loop, but apparently not. I note that ManifestLoop just calls Manifestscriptcall recursively. Also ManifestNameRNG and ManifestAgeRNG just give strings, so the code boils down to this:

  <function name="Manifestscriptcall">
    player.manifestRNG = player.manifestRNG + 1
    player.population = player.population - 1
    Log ("Removed 1 pop to stabilize")
    for (i, player.manifestRNG, player.population) {
      Manifestscriptcall
    }
    player.population = player.population + 1
    Log ("Added 1 pop to stabilize")
  </function>

I would guess it is due to the loops within loops. What were player.manifestRNG and player.population when you got that off result?


if the recursion works, he/she should be getting multiple 'Log ("Removed 1 pop to stabilize")' outputs next to each other (though based upon the recursion and further nested recursions, which could add multiple 'Added 1 pop to stabilize' outputs too, breaking up the multiple 'removed' outputs from being next to each other), I think, for examples:

say a single recursion occurs:

(function's scripts begin)
Removed 1 pop to stabilize
(1st level's 1st recursion begins)
Removed 1 pop to stabilize
(2nd level's 1st recursion fails: back to finishing up the 1st level's 1st recursion's scripts)
Added 1 pop to stabilize
(1st level's 1st recursion ends: back to finshing up the rest of the function's scripts)
Added 1 pop to stabilize
(function's scripts end)

R
R
A
A

say a single 1st level single recursion recurs and a single 2nd-level single recursion occurs:

(function's scripts begin)
Removed 1 pop to stabilize
(1st level's 1st recursion begins)
Removed 1 pop to stabilize
(2nd level's 1st recursion begins)
Removed 1 pop to stabilize
(3rd level's 1st recursion fails: back to finishing up the rest of the 2nd level's 1st recursion's scripts)
Added 1 pop to stabilize
(2nd level's 1st recursion ends: back to finshing up the rest of the 1st level's 1st recursion's scripts)
Added 1 pop to stabilize
(1st level's 1st recursion ends: back to finshing up the rest of the function's scripts)
Added 1 pop to stabilize
(function's scripts end)

R
R
R
A
A
A

VS

his/her code output:

R
A
R
A
A

(I think some iterations, of the initial function's scripting, and/or, of whatever level/s of recursions, are occuring somehow, or it's doing the recursion in reverse order... my brain can't handle/follow this level of recursion and his code data, sighs)


Wow, sorry I really didn´t mean to waste so much of your time.

Well Pixie, the values of player.manifestRNG were 0 because I´ve set it to that way every time the Manifestscriptcall function finishes. This way, the manifest displays the same thing every time it is looked into.

Also, the "added 1 pop to stabilize" and "removed 1 pop..." are there because the loop function fires TWICE even when there is only one person. I should have made that more clear :/
But this weak "patch" stops working at 4 population so it is useless anyway.

But this whole doesn´t make sense to me because I´ve noticed that the function does not have any logic when it fires and when not. If there is 5 population then it fires 7 times if there is 6 then it fires some another number and I have no idea why.

That´s why I think I am going to give up on this method because it is going to waste more time for very little benefit.

Could someone just show me a better way to have a list, that is:

-Randomly generated.
-Remembers the randomly generated stuff and flags it for every different name?
-Expands when there is more people...

Maybe the NewStringList() function could work for this but I had a trouble trying to copy code from TA into GB and it didn´t work so I don´t know...

I guess I just need to learn, way way more.

BTW: This was how the whole thing is supposed to work. https://i.gyazo.com/968b92270829eca3ea44007b4e391c14.png


If it is supposed to be making a list of people, it falls well short of that, I am afraid. It only ever generates one person.


Well, this is what happens when I add THREE people.
That is why is the ManifestLoop() in there, to keep it firing until it manifestRNG equals population.

https://i.gyazo.com/c51b4eed1449d0a852f9d30b76e9b87f.png

(In future I hope it will assing everyone an unique name AND remember it, but right now I cant even get it to display.)


How about storing the names in a list; add a new name when someone is added to the population. Use the size of the list to track the population size perhaps. Then it is simply a matter of outputing a list.

If you want to store more than just the name, you could put it all in one long list, separated by semi-colons, then when outputing (or whatever), break it up with the Split function.


Are you sure Split function works in GB?

I can´t find it in the library elements and when I try to use it it throws the "Could find not function 'Split()"
I might be doing something wrong maybe.

EDIT: Nvm, Split works in GB, I am just dumb.
EDIT EDIT:
Well it seems like I didn´t do anything wrong and it really doesn´t work in GB.
I´ve created a new list using NewStringList(). I am sure I´ve done it correctly.
I´ve added value to it and still this is what happens. https://i.gyazo.com/76edfe0f94e88f7bd4495bce7d840795.png

:/
I think it´s time to move to TA I guess...


I think it´s time to move to TA I guess...

This.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums