Topic:   Are there 2d arrays in SC?   (Read 145 times)


0 Members and 1 Guest are viewing this topic.

Johna100


  • GMG Newbie

  • *


  • Posts: 70

  • Gan is so awesome.
    • View Profile
    • make a simple Rougelike
Are there 2d arrays in SC?
« on: June 05, 2020, 02:48:18 AM »
Are there 2d arrays in SC? Or a way to do something similar? Like having a variable be an array then that variable in a different array?

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2362

  • It's a secret to everyone...
    • View Profile
Re: Are there 2d arrays in SC?
« Reply #1 on: June 05, 2020, 09:08:04 PM »
There are not 2D arrays exactly, and this was a topic of discussion a while back. One solution was to use strings, since each string is basically an array and you can make an array of strings.

The other solution, which I didn't think about at the time, is that you can make a 1-dimensional array and assign each space on a 2D grid to a position in that array. You have to simply divide it into rows, and write a function that takes values X,Y and returns a place in your array.

If I want a 5x5 board, I can make an array of size 25. I'll label it starting from 0 for the example.
Code: [Select]
  0 1 2 3 4  
0 # # # # #
1 # # # # #
2 # # # # #
3 # # # # #
4 # # # # #
so to access tile (2,3) I would go to row 3 which is 5*3, and add 2.
(5*3)+2 = 17
so tile (2,3) is stored in array position 17.
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Johna100


  • GMG Newbie

  • *


  • Posts: 70

  • Gan is so awesome.
    • View Profile
    • make a simple Rougelike
Re: Are there 2d arrays in SC?
« Reply #2 on: June 05, 2020, 09:53:29 PM »
thank you ;D

GMG Mike


  • Administrator

  • GMG-er

  • *****

  • no avatar

  • Posts: 536
    • View Profile
    • mikerichardson.name
Re: Are there 2d arrays in SC?
« Reply #3 on: June 27, 2020, 01:33:21 AM »
Honestly if I were to implement 2D arrays, they would most likely work internally the same way as Connor's example. But having SilverCreator implement it might make them work a little bit faster - but really, Macs are fast enough now that it doesn't really matter.

Johna100


  • GMG Newbie

  • *


  • Posts: 70

  • Gan is so awesome.
    • View Profile
    • make a simple Rougelike
Re: Are there 2d arrays in SC?
« Reply #4 on: July 02, 2020, 06:42:41 AM »
ok, thanks