playMaker

Author Topic: [SOLVED] Array loops starts at 0 first time, 1 second time?  (Read 1610 times)

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
[SOLVED] Array loops starts at 0 first time, 1 second time?
« on: December 14, 2017, 07:25:25 AM »
Hey Guys,

I gotten a lot of help in this forum, and once again I'm back with a new challenge :)

I created a leaderboard for our game KnightOut. When the game is over it add's a score to our leaderboard based on how good the score is. It will place it at spot 0 if its a new highscore. And it works completely when I run it the first time. The second i run it, if the player wants to restart, time the "Array Get Next" action starts at index 1 instead of 0? The range is set to 0-9 and I change nothing when I run it. It doesn't make sense.

This means even if I get a new highscore it will place it under the number 0, because it starts checking from 1, instead of 0, which screws up the whole system.

Any ideas?
« Last Edit: December 15, 2017, 08:00:42 AM by DennisJensen »
Game Designer, CEO, Artist
http://2ndStudio.com

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Array loops starts at 0 first time, 1 second time?
« Reply #1 on: December 14, 2017, 11:19:17 AM »
I don't know about that one, but arraylistgetnext has the reset toggle. Check it to start iterating from the first item.

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #2 on: December 15, 2017, 02:08:11 AM »
@krmko

Okay, there is also a Array Get Next on the ecosystem with a restart bool, so I might try that first so I don't have to change my array variables :) But that might do the trick.
Game Designer, CEO, Artist
http://2ndStudio.com

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Array loops starts at 0 first time, 1 second time?
« Reply #3 on: December 15, 2017, 02:15:48 AM »
As far as i know, you don't need to change variables and the reset will do the trick.

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #4 on: December 15, 2017, 02:28:58 AM »
As far as i know, you don't need to change variables and the reset will do the trick.

Okay, I just tried using the get array 2 from ecosystem, it has a reset flag which I set to true, but it gives me the same error.

I attached the issue. I really can't figure it out :/ Same how it starts at 1 the second time I run through it. I could try with array lists instead but I dont see how that should solve it, and its a bit of work to change all my variables.
Game Designer, CEO, Artist
http://2ndStudio.com

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Array loops starts at 0 first time, 1 second time?
« Reply #5 on: December 15, 2017, 02:59:23 AM »
I see the index 0 at the second picture?

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #6 on: December 15, 2017, 03:11:21 AM »
I see the index 0 at the second picture?

Yes, so I run from the event save score, and I set the index int to 0 before running the array get next. So it should be set to 0, however it doesn't when I run save score for the second time. It's so weird.
Game Designer, CEO, Artist
http://2ndStudio.com

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Array loops starts at 0 first time, 1 second time?
« Reply #7 on: December 15, 2017, 03:14:50 AM »
There must be some logic fallacy, can you provide a small project perhaps?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14819
  • Official Playmaker Support
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #8 on: December 15, 2017, 06:39:25 AM »
Hi,

 Yes, a small repro would help. Also, when using the reset flag, you must also reset the index variable if you are using a variable to store the current index in the action, else it will start from that index)

 Bye,

 Jean

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #9 on: December 15, 2017, 07:25:21 AM »
So I just created a new example, where do the following:

1. Set a index int to 0
2. Run through a loop X amount of times based on array, debug by outputting the index int number (Using "Array Get Next")
3. Pressing a key to reset

And that works.

So I redid my scoreboard loop.

1. I set index to 0
2. I run through my scoreboard array using "Array get next"
3. I compare the new score to the score in my array, if it's higher then proceed.
4. If the score is higher then save the index as the placement for our new highscore. Using "Set int" PlayerSpot_I = PlayerIndex_I (PlayerIndex_I is the index variable i use in the array get next.

I don't use my PlayerIndex_I anywhere else than these three states but somehow it outputs 0 the first time you run it. and then 1 the second time even though both are new highscores.

Can it be something with the array not updating the new score that I input?

This is what happen, my score is 0, 0, 0 for top 3, I set a score at 100 and the score becomes 100, 0, 0. Then I replay the game and sets a score at 120 which then gives me 100, 120, 0 but only if I replay it from the same game scene, if I reload the game scenes it works. It's so weird.
Game Designer, CEO, Artist
http://2ndStudio.com

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4648
    • View Profile
    • jinxtergames
Re: Array loops starts at 0 first time, 1 second time?
« Reply #10 on: December 15, 2017, 07:36:50 AM »
Hi,
from what i can see on the picture you have checked the reset manually
Ones the action is called, it will set this to false.
So you will need to use a variable and set the variable to true every time you want to restart the loop. (so on a fsm before the 'array get next 2')

Maybe my tutorials can help?


or


DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: Array loops starts at 0 first time, 1 second time?
« Reply #11 on: December 15, 2017, 08:00:25 AM »
I actually did follow your tutorial, thank you for it, it's really good.

So I found the problem, but I don't understand why. First I was using "Array get next" then I used "Array get next 2", both gave the same problem. When I then changed the reset value to a bool and reset it after I ran my loop then it apparently works, I have no idea why  :-X
Game Designer, CEO, Artist
http://2ndStudio.com

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4648
    • View Profile
    • jinxtergames
Re: [SOLVED] Array loops starts at 0 first time, 1 second time?
« Reply #12 on: December 15, 2017, 08:08:23 AM »
Hi.
What happens is :
When the action is played it will set the bool on the variable to false.
Then it will do a loop and check again if false or true.
If false it will continue the loop, if it was set back to true it will reset the loop and it will set the variable to false again.

So the action is getting the bool to check if true and then if it is true it will set the variable to false and reset the loop. :)

DennisJensen

  • Full Member
  • ***
  • Posts: 103
    • View Profile
Re: [SOLVED] Array loops starts at 0 first time, 1 second time?
« Reply #13 on: December 16, 2017, 03:10:41 AM »
What happens is :
When the action is played it will set the bool on the variable to false.
Then it will do a loop and check again if false or true.
If false it will continue the loop, if it was set back to true it will reset the loop and it will set the variable to false again.

So the action is getting the bool to check if true and then if it is true it will set the variable to false and reset the loop. :)

Thank you for explaining, but I would guess the normal array get next would restart each time you run the state, however it didn't really work, it doesn't make sense to me that it runs from 1 instead of 0 the second time I run the action :O I could understand it if it didnt do anything. But who cares it works now and I kinda get what I did wrong.
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14819
  • Official Playmaker Support
    • View Profile
Re: [SOLVED] Array loops starts at 0 first time, 1 second time?
« Reply #14 on: December 18, 2017, 12:00:19 AM »
Hi,

This is a necessary evil because if the action would restart every time it enter the state then it would never be able to go through all indexes.

Bye,

 Jean