Author Topic: Can a variable have multiple values?  (Read 7551 times)

0 Members and 1 Guest are viewing this topic.

Offline Doan Sephim

Can a variable have multiple values?
« on: December 02, 2010, 02:34:42 PM »
This seems like something I should know, but I haven't programmed in a while. Can a local variable have 2 or more simultaneous values, say 4,7, and 10?
« Last Edit: December 02, 2010, 02:44:52 PM by Doan Sephim »


Artificial Intelligence Competition

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #1 on: December 02, 2010, 04:15:17 PM »
It should be able to. Just use square brackets like:

Code: [Select]
(local
       aLocalVariable = [3][5]
)

I THINK that's how you do it. I can't remember if it's different for SCI or not. But I know it's possible. If you remember asking about that scoreboard you were needing help on way back and I offered my help, I think I utilised array variables in that as well. In any case, I believe it's documented in the SCI Studio help file.
« Last Edit: December 02, 2010, 04:16:51 PM by MusicallyInspired »
Brass Lantern Prop Competition

Offline gumby

Re: Can a variable have multiple values?
« Reply #2 on: December 02, 2010, 05:51:25 PM »
Actually,  MI's example is a 2-dimensional array, which would generate a total of 24 'variables', and you could set them like this:

Code: [Select]
 var aLocalVariable[3][5]
  = aLocalVariable[0][0] "a"
  = aLocalVariable[0][1] "b"
  = aLocalVariable[0][2] "c"
  ...
  = aLocalVariable[0][5] "d"
  = aLocalVariable[1][0] "1"
  = aLocalVariable[1][1] "2"
  ...

But to answer your question, no, you cannot have a variable have 2 values at the same time.  You need multiple variables to handle this (or an array of variables)  A single dimensional array might work for you, i.e.

Code: [Select]
  var aLocalVariable[3]
   = aLocalVariable[0] 4   // first 'state' to keep track of
   = aLocalVariable[1] 7   // second 'state' of keep track of
   = aLocalVariable[2] 10 // 3rd...

But you have to reference their values with array index, just like how you assigned them.
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #3 on: December 02, 2010, 06:19:05 PM »
Right ok. The numbers in the boxes when declaring the variable are the number of variables to hold values. It's been a while.
Brass Lantern Prop Competition

Offline Doan Sephim

Re: Can a variable have multiple values?
« Reply #4 on: December 02, 2010, 10:27:29 PM »
This is all very interesting, though I am not sure I quite understand all of it. I am guessing I will just have to use 36 different variables for what I need to do... :( That just seems unnecessary to me...
« Last Edit: December 02, 2010, 10:37:53 PM by Doan Sephim »
Artificial Intelligence Competition

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #5 on: December 02, 2010, 11:04:09 PM »
No, you don't need 36 variables. How many do you need? What are you using it for?
Brass Lantern Prop Competition

Offline Doan Sephim

Re: Can a variable have multiple values?
« Reply #6 on: December 02, 2010, 11:07:54 PM »
I have a puzzle board with 36 (4 3x3 squares) squares. Some of the squares are "holes" that the character can fall into. Every move, the holes are supposed to shift down. I need to program the script to recognize which square the player is on and whether or not it is a hole. I can figure it out if I have 36 individual variables representing each square, but I was just trying to find a less cumbersome way about it.

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #7 on: December 02, 2010, 11:39:47 PM »
Yeah, you can use arrays to do it instead. They're designed specifically for things like matrices like that. Though, I discovered that SCI doesn't support multi-dimensional arrays. Though, I'm having trouble wrapping my own brain around it right now. Maybe Gumby can help you. Man I haven't scripted or coded anything in ages. You really do lose it if you don't use it...
Brass Lantern Prop Competition

Offline gumby

Re: Can a variable have multiple values?
« Reply #8 on: December 03, 2010, 08:14:17 AM »
I have a puzzle board with 36 (4 3x3 squares) squares. Some of the squares are "holes" that the character can fall into. Every move, the holes are supposed to shift down. I need to program the script to recognize which square the player is on and whether or not it is a hole. I can figure it out if I have 36 individual variables representing each square, but I was just trying to find a less cumbersome way about it.
This is great case for a 2 dimensional array - you could have an array for each square, with 9 indexes (indices?  oh, hell):
Code: [Select]
var squareOne[2][2]     // Has indexes of 0-2
 var squareTwo[2][2]
 var squareThree[2][2]
 var squareFour[2][2]

Then you use them like a matrix.  For example, squareOne would look like this, each slice of the array represents a column or row.  Visually, it looks like this:

[0,0], [0,1], [0,2]
[1,0], [1,1], [1,2]
[2,0], [2,1], [2,2]

So, if you wanted to manipulate the middle square value, just reference squareOne[1][1] in your code.  Or the bottom left square would be [2][0].

If you really want to get crazy, you might be able to do a three dimensional array, but I'm not sure if the compiler can handle it:

Code: [Select]
  var multiDimArray[3][2][2] 
...where the first dimension is the square number, the 2nd the column, and the 3rd the row.  Doesn't have to be laid out exactly this way, you can shuffle the dimensions any way you like (2-3-2, 2-2-3, you get the idea)

Actually, I don't even think I've tried a 2D array with the compiler.  Guess I'd better test that...

In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #9 on: December 03, 2010, 08:35:22 AM »
Code: [Select]
var squareOne[2][2]     // Has indexes of 0-2

Actually, it has indexes of 0-1 because you declared it with only 2 variables in each array.

Quote
Actually, I don't even think I've tried a 2D array with the compiler.  Guess I'd better test that...

I tried it and it doesn't work. At least, it doesn't work as a local variable.
Brass Lantern Prop Competition

Offline gumby

Re: Can a variable have multiple values?
« Reply #10 on: December 03, 2010, 09:27:36 AM »
Code: [Select]
var squareOne[2][2]     // Has indexes of 0-2

Actually, it has indexes of 0-1 because you declared it with only 2 variables in each array.

Quote
Actually, I don't even think I've tried a 2D array with the compiler.  Guess I'd better test that...

I tried it and it doesn't work. At least, it doesn't work as a local variable.
Ah, so the number in the array declaration is the size, not the last index.  Good to know.  No multi-dimensional array support, huh?  Bummer.  Thanks for checking that out!
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Doan Sephim

Re: Can a variable have multiple values?
« Reply #11 on: December 03, 2010, 10:04:57 AM »
So do we still need to make 36 different variables? That seems to be the bottom line, if I am understanding correctly, which is fine...it will probably take up more heap than I would like (as well as be mindnumbingly boring ;))

Offline gumby

Re: Can a variable have multiple values?
« Reply #12 on: December 03, 2010, 10:35:28 AM »
How many holes are on a board at one time?  Maybe you could just give up tracking all the squares, and just track the holes.  Maybe...
Code: [Select]
   var holeList[10]     // ten holes possible
 
   // And then just stuff the location of where the hole is into the array, like this:
   = holeList[0] 7     // square #7, wherever that is...
   = holeList[1] 2    // etc...

Then every time the ego moves to a new square, loop through the hole list (no pun intended), and see if that square has a hole.
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline MusicallyInspired

Re: Can a variable have multiple values?
« Reply #13 on: December 03, 2010, 05:39:52 PM »
This is what the SCI Studio Help file has to say about arrays just to put it out there:

Code: [Select]
(local
  // no value defined, set to zero
  ZeroVariable
  // A normal variable
  SomeLocal = 30
  // 30 word sized variables (60 bytes total), all set to zero
  LocalArray[30]
  // The six variables in the array are set, the rest will be zero
  DeclaredArray[10] = (0 1 2 $ABCD 3 4)
)

(procedure (VarProc)
  // Read a variable normally
  (if(== SomeLocal 20)
    // do something
  )

  // Use a variable for looping and set
  // the first 20 entries in LocalArray to 0-19
  (for (= SomeLocal 0) (< SomeLocal 20) (++SomeLocal)
    (= LocalArray[SomeLocal] SomeLocal)
  )

  // An example of using a variable for strings
  (= SomeLocal "Hello")
  // gets the address of SomeLocal
  Display(@SomeLocal)
)


It doesn't go into any more detail than that.
Brass Lantern Prop Competition


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.023 seconds with 18 queries.