Log In  

Hi, I have a function that takes a string in a specific format and then creates a table used elsewhere in my game from the string.
Currently the function parses the string correctly and creates the table but after I assign a variable to the newly created table it is then lost. Is this a thing where I'm assigning a reference and then the original is cleared by garbage collection? How could I get around this?

for i=1,#levelonepaths do
    --stringtopath is the parseing function levelonepaths is a table containing the strings
    local path = stringtopath(levelonepaths[i])
    --Printing the local variables 1st entries x co ordinate gives the correct result
    print(path[1].x)
    --I then assign where the string was to the new table currently stored in the local var path
    levelonepaths[i] = {}
    levelonepaths[i] = path
end
--this print gives nil
print(levelonepaths[1].x)
--this print gives nil
print(levelonepaths[1].y)
P#44921 2017-10-04 23:24 ( Edited 2017-10-05 08:41)

You appear to be mixing levels of indirection/indexing here in two places here.


First, let's start with the assignment in the loop. It looks like your stringtopath() returns a table that looks a bit like this:

{{x=6,y=2},{x=7,y=4}, ... ,{x=129,y=67}}

Basically an array of point objects.

I'm not sure if your levelonepaths[i] is meant to contain more than one of those paths, or if it's just meant to contain the One True Path returned by stringtopath(). You might be looking to save a list of possible paths to a target, or just one, I'm not sure.

Right now, you're assigning to levelonepaths[i] twice in a row, so this first assignment:

    levelonepaths[i] = {}

Is immediately wiped out by the second:

    levelonepaths[i] = path

If you mean to have levelonepaths[i] possibly hold more than one path, but begin with just the one returned by stringtopath(), then you want this:

    levelonepaths[i] = {path}

Otherwise just delete the first ={} assignment, as it serves no purpose, being immediately obliterated by the =path assignment.


Second, when printing the contents, if you mean to have just one path per entry in the levelonepaths[i] table, and you want to see the first point in the first path, you need to index both the path in the level and then the point in the path.

print(levelonepaths[1][1].x)
print(levelonepaths[1][1].y)

If, as I was guessing above, you might mean to leave open the possibility that each entry in levelonepaths could be a list of possible paths to a destination, say, then you'd actually need two more levels of indirection:

print(levelonepaths[1][1][1].x)
print(levelonepaths[1][1][1].y)

This would get you to the first set of paths in the list (ie. different ways to get to target point 1), the first path in that set, and the first point in that path.

Hope this is helpful...

P#44922 2017-10-05 04:19 ( Edited 2017-10-05 08:30)

Thank you loads.
It was the indexing problem that was messing it up. Something was wrong with it so I started testing it by itself and after fixing some things it still wasn't working in the tests because that indexing error was in the test cart.

P#44923 2017-10-05 04:41 ( Edited 2017-10-05 08:41)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 12:49:39 | 0.006s | Q:10