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


0 Members and 1 Guest are viewing this topic.

Johna100


  • GMG-er

  • **


  • Posts: 161

  • lalala
    • View Profile
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?

EDIT:
http://gamemakersgarage.com/forum/index.php?topic=1801.msg25111#msg25111
« Last Edit: August 15, 2020, 04:07:39 PM by Johna100 »

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • 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-er

  • **


  • Posts: 161

  • lalala
    • View Profile
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-er

  • **


  • Posts: 161

  • lalala
    • View Profile
Re: Are there 2d arrays in SC?
« Reply #4 on: July 02, 2020, 06:42:41 AM »
ok, thanks

Johna100


  • GMG-er

  • **


  • Posts: 161

  • lalala
    • View Profile
Re: Are there 2d arrays in SC?
« Reply #5 on: August 15, 2020, 04:04:45 PM »
This is how I think of it:

If you have an array that is 40000
Then that can be thought of as a grid 200 by 200
Now if the player Position variable Subtracts 200 at once thats Forward one space
If the player Position moves 1 number More thats to the Right one space

Adding 200 is Backwards
And Subtracting 1 is Left

You could use this in a rouge-like with text tiles, a game with sprites or a 3d environment. I have done all 3 to some extent.


Code: [Select]
// Create a number array of 40000
DIM worldGrid(40000)

// Create a string also known as a text array of 40000
DIM worldTextGrid$(40000)

// Create a space with a Wall
LET worldGrid(800) = 1

// Set where the player starts
LET playerPosition = 1200

// Move The player up one space
LET playerPosition = playerPosition - 200

// Make the player bounce back so the wall is like its solid
IF worldGrid(playerPosition) = 1 THEN
LET playerPosition = playerPosition + 200
END IF

// Move The player right one space
LET playerPosition = playerPosition + 1




Just change the numbers to the correct 200 or 1 and operators to + or - for the other directions.

There are many ways to show this information with a little effort, ideas and testing. Like a text adventure describing you walked forward, back, right, left or hit a wall. Also you could show it with sprites or on a text-tile based map(Rouge-Like).
« Last Edit: August 15, 2020, 04:15:25 PM by Johna100 »

Johna100


  • GMG-er

  • **


  • Posts: 161

  • lalala
    • View Profile
Re: Are there 2d arrays in SC?
« Reply #6 on: August 15, 2020, 04:28:12 PM »
Here are some files.