ID:175437
 
I have an array in my program. However, I want to assign constant values to it.

If I type this:
obj
objname
var
array[4]
obj1
array[1] = 23
array[2] = 43

it complains that left-hand side must be an object variable.
The faulty rows are the ones with [1] and [2] in them. If I remove the [1] and [2] it compiles(like array = 23), but I think it won't have the desired results, they'll probably overwrite eachother instead of be inserted after one another.

How can I solve this?
There's a tab missing. obj1 is a subtype of objname of course.
In response to Xylpher
Byond doesnt use arrays, youll have to get around it with lists.
In response to Jotdaniel
Noooooo...

I love arrays, they're the best thing since sliced bread...

They even have em in the WC3 map editor, boy did I laugh when I saw a map where someone had named his variables x1 through x20, and did even more endless copying of code where he could've used the handy for loop...

Man, guess I'll have to find out how lists work in byond then.

I'm way too used to Java, it's such an excellent language.

Thanks for the swift answer.
In response to Xylpher
Wow, I hadnt noticed when you posted it, no problem there. I'm actually not quite sure how to simulate arrays with lists, im very poor at math.
In response to Xylpher
They are pretty much the same thing. Im pretty sure that they are even better. After all in Java you cant change the amount of positions in an array after youve declaired it, at least I dont think so.
When declairing them you just do list/varname[length], or you can make use of the list(), like so, list/varname = list(value1, value2, value3)
Anyway Im no teacher, so for more information consult the reference. Just press F1 in Dream Maker, then go to the next tab then type in what you want too look up.
In response to Xylpher
Xylpher wrote:
Noooooo...

I love arrays, they're the best thing since sliced bread...

List pretty much are arrays without the hassle of memory protection and problems with indexing too far(as far as accidently overwriting other stuff anyway :)).

The problem you have is that you can't set elements of a list at compile-time without using newlist but thats only for filling the list with objects. So you'll have to initialize the list from inside the New() proc.
In response to Xylpher
Lists are essentially the same as Vectors in Java, except there is no need to type cast the objects you retrieve from them, and don't require a wrapper class to store primitive types.
(This should be posted in Newbie Central or Code Problems, by the way.)
There's a lot of bad advice in this thread :-) Simply put, in BYOND we call arrays "lists". They're slightly different than most languages, since they're a class and not a primitive, but they work mostly how you'd expect them to.
You can initialize a list at compile time. It's done like so:

var/array[] = list(23, 43, 0, 0)

OR

var/list/array = list(23, 43, 0, 0)
(I put 0's in the rest of your list so that it'd be four elements long.)

Later, you can add to the list using array.Add() or just array += whatever. This does increase the size of the list, though (much like a Vector from Java, as Garthor pointed out.)

-AbyssDragon
In response to AbyssDragon
Okay, thanks a bunch, it's a very clear and useful answer.

One question though, about the adding with +=. If it works like that, if I have a variable length list (which it is, I just used the number as a "probable" maximum, would've rather used a constant like MAX_ARRAY_LENGTH, but don't know if that's possible here), would it be possible and best to just make an empty list (list[0]) and add all the values to it from there?
In response to Xylpher
After a bit more experimenting...

obj
objname
var
array[]
obj1
array[] = list(23,24)

"Left hand side must be an object variable" on the "array[] = list(23,24)" line

obj
objname
var
array[] = list (23,24)

Does work. However, I want multiple objects obj1/obj2/obj3 all with their own respective (constant) values.

Man I wish this thing worked with a database. Ah well it's pretty much the only program with which I can actually get what I code online.

In response to Xylpher
It's a bit confusing at first, but you only need the brackets when you're defining the array. When assigning values in code or in child objects, you just leave them off. It would look like this:

obj
objname
var
array[]
obj1
array = list(23,24)

Man I wish this thing worked with a database. Ah well it's pretty much the only program with which I can actually get what I code online.

If you plan on running the server for your game/program/whatever on Linux, BYOND has built-in MySQL access... but only on Linux. I don't know of anyone to use it yet, though... it's a relatively hidden feature.

-AbyssDragon
In response to AbyssDragon
Technically you don't need brackets at all unless you want to modify a specific value in the list. You can define lists without them, and you can create lists without them:

<code>mob var/list/array obj1 array = list(23, 24)</code>

Now that the array won't function as a list until you declare it to be a list, either with initial values or without:

<code>mob var/list/array1 // not a list, yet var/list/array2 = list() // empty list var/list/array3 = list(23, 24) // list with values</code>

From there, you can modify a specific item in a list by refering to that items position in a list.

<code>array[2] = 18</code>

Or if you're using an associative list:

<code>array["weight"] = 25</code>

But you won't be able to do anything with the list unless it is first declares to be "var = list()" somewhere.
In response to Foomer
Okay, I'm finally getting the desired results, and I think I now know all the syntax there is to arrays.

For such a small problem with such a simple concept, this took me way too long to solve, and I should've been able to solve it faster, but I'm not that used to the reference file yet either and I don't think I've been looking through it quite the right way.

Once you know how it's so easy. Anyway, thanks to everyone for putting up with me.