ID:1512381
 
(See the best response by Kaiochao.)
Code:
var
dirs = list(NORTH=1, NORTHEAST=2, EAST=3, SOUTHEAST=4, SOUTH=5, SOUTHWEST=6, WEST=7, NORTHWEST=8)

client
New()
mob = new/mob/police
East()
dir = dirs[dir] + 1
if(dir > 8)
dir = 1
West()
dir = dirs[dir] - 1
if(dir < 1)
dir = 8


Problem description:

When I try East() and West(), all I get is a runtime error saying that it tried to add/subtract 1 from "NORTH", even though I've already associated a number with it.

Thanks.


- Gamermania.
Best response
If all you're trying to do is rotate 45 degrees in either direction, you can use turn().
East()
mob.dir = turn(mob.dir, 45)

West()
mob.dir = turn(mob.dir, -45)


The reason your code isn't working is either because list(NORTH=1, ...) is probably being read as list("NORTH" = 1), meaning NORTH is a string, while directions that BYOND uses aren't actually strings.
//  e.g.
NORTH != "NORTH"
NORTH == 1


And, lists can't associate numbers with anything. If you try to associate a number with a value, you'll just end up setting that index to the value (and probably getting an index out of range error).
Wait, so you mean you can't store non-integer values into integers like you can with arrays? This needs to be a feature!


Anyway, as for your turn snippet, it's not working. It seems to me that no matter what, the car will face to the east, and stays like that until pressing North or South.



EDIT: Nevermind, putting mob. in front of dir's fixed it. Thank you!



In response to GamerMania
What you wrote above would be this:
var dirs = list(NORTH=1, NORTHEAST=2, EAST=3, SOUTHEAST=4, SOUTH=5, SOUTHWEST=6, WEST=7, NORTHWEST=8)

// would become this:
var dirs = list(1=1, 5=2, 4=3, 6=4, 2=5, 10=6, 8=7, 10=8)

It doesn't really make much sense, and neither does this:

Wait, so you mean you can't store non-integer values into integers like you can with arrays? This needs to be a feature!
Everything involved is an integer, so I'm not sure what this means.

Anyway, as for your turn snippet, it's not working. It seems to me that no matter what, the car will face to the east, and stays like that until pressing North or South.
Are you actually trying to change the car's dir, not the client's? If you want to change the dir of the mob, you should be changing mob.dir, not client.dir. I edited my post with that.
In response to GamerMania
If you just wanted a list of dirs (numbers), you shouldn't be trying to associate anything by including any kind of assignment.
var dirs[] = list(NORTH, NORTHEAST, EAST, etc.)
In response to Kaiochao
Kaiochao wrote:
Everything involved is an integer, so I'm not sure what this means.


I mean like this:


(C++ Example)
string x[6];
string y[6];


//blah blah blah
x[5] = "Hello, world!";
y[4] = x[5];
cout << y[4] << endl;



This would display "Hello, world!" into the output.




In response to GamerMania
var x[6];
var y[6];

x[6] = "Hello, world!";
y[5] = x[6];
world << y[5];

That works, but I don't see how this is related to...
Wait, so you mean you can't store non-integer values into integers like you can with arrays? This needs to be a feature!
In response to Kaiochao
Kaiochao wrote:
> var x[6];
> var y[6];
>
> x[6] = "Hello, world!";
> y[5] = x[6];
> world << y[5];
>

That works, but I don't see how this is related to...
Wait, so you mean you can't store non-integer values into integers like you can with arrays? This needs to be a feature!

Wait, you can have arrays in BYOND?
In response to GamerMania
No, but you can initialize lists with an initial size. It'll be filled with nulls. It's faster to "set n pre-allocated elements" than to "add n elements to an empty list."
e.g.:
var stuff[0]
for(var/n in 1 to 1000)
stuff += new /obj

// this is faster:
var stuff[1000]
for(var/n in 1 to stuff.len)
stuff[n] = new /obj


Lists in DM are fairly flexible. You can associate strings, objects, and type paths to any kind of value. They're kinda like objects in JavaScript.
var js_object[] = new
// or
var js_object[0]

js_object["name"] = "Poop"
js_object["age"] = 42
js_object[new /mob] = "this is a mob"

src << "[js_object["name"]] is [js_object["age"]] seconds old."

src << js_object[locate(/mob) in js_object]