ID:156834
 
Hey guys, I am having some issues. I am trying to make a little game to play, based off of the old classic, "Snake". I have thought through many aspects of the game, and still do have some more.

My problem is this: How can I efficiently create and move a "link" correctly? I am using an associative list to manage how many links I have, and the links create right, and actually move decently too. But I still have some issues:

-A Runtime error associated with the code below:
runtime error: bad index
proc name: Add Body (/mob/proc/Add_Body)
source file: Procs.dm,28
usr: Albro1 (/mob/player)
src: Albro1 (/mob/player)
call stack:
Albro1 (/mob/player): Add Body()
Albro1 (/mob/player): Add Link()

-The associative list does not seem to be working correctly, meaning somewhere I have screwed up. Whenever I create one, it should go behind/beside(depending on the direction) the last created link, if there was one. Instead, it always goes behind me.

mob
proc
Add_Body()
var/obj/O = new /obj/body(src.loc)
if(src.trails)
var/obj/N = src.trails["[src.trails.len] link"]
O.dir=N.dir
//movement stuff
src.trails["[src.trails.len+1] link"]=O
else
var/mob/M = src
O.dir=M.dir
//movement stuff
M.trails["1 link"]=O
You don't need an associative list at all, here. Just a linked list, where each segment points to the previous one. Like so:

obj/body
var/obj/body/previous
Move()
var/turf/oldloc = loc
.=..()
if(.)
previous.Move(oldloc)

mob
var/obj/body/previous
proc/Add_Body()
var/obj/body/B = new(loc)
B.dir = dir
if(src.previous)
B.previous = src.previous
src.previous = B
Move()
var/turf/oldloc = loc
.=..()
if(.)
previous.Move(oldloc)
In response to Garthor
Thanks. I have one problem though; how would I manage the links? If the mob turns, it doesn't necessarily mean a created link would turn just then. The body could become quite long, meaning the mob could be facing west while the last link could be facing south.
In response to Albro1
Base the turns off of the last added object and not the head object.