Randomness + AI

@tnlogy - thats a great link. I knew he does some sort of lecture circuit - but didn’t know he’d done one at GDC. :smiley:

@creed Half of what you said went clear over my head. I know hardly any advanced math . Perlin noise sounds cool though.

@Ignatz Ohhhh, I din’t know that. I thought that was just, ya know, vanilla noise, din’t think it was Perlin noise. I looked at it for a bitt and read some of @andymac3d 's link and i think I’ll wait a while before getting into that as very little of it made any sense to me.

On a slight off topic direction to the thread title, I’ve made good head way on a cell generator.
The only problem is that tables and vectors don’t seem to get along.

The generator creates a grid of points that it putts into a table. Oddly, the vectors that are putt into the table are correct, but it seems that there not being stored properly as they default to (500, -532) when read back out from the table.

I tried to make a smaller program to just showcase this, but to no avail.

The code below will print out each point BEFORE its putt into the table. But if you comment out the print in MarkGridPos and uncomment PrintGrid in setup, then it will print out the contents of the table, grid, which is (500,-532). Does anyone know the cause of this, maybe I’m storing them wrong?

-- Grid Program
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
    CreateGrid()
    --PrintGrid()
end

-- This function gets called once every frame
function draw()

    background(40, 40, 50)
    strokeWidth(10)
    stroke(255, 0, 162, 255) 
    for i = 1, #grid do
       -- print(grid[i].x, grid[i].y)
        line(grid[i].x, grid[i].y, grid[i].x, grid[i].y)  

    end
end

-- Makes a grid
function CreateGrid()
    genGrid = true
    grid = {}
    gridSpac = 50
    
    gridDim = vec2(WIDTH, HEIGHT)
    
    -- Pos is in the top left of the grid
    gridPos = vec2(0, HEIGHT)
    
    markerPos = vec2(gridPos.x, gridPos.y)
    MarkGridPos(markerPos)    
    rowCount = 0
    runTimer = 0
    
    -- While loop that goes through procudures to create the table
    while genGrid == true do
        RunTimer()
        
        SlideMarker()
        
        if CheckRowEnd() == true then
            
            --print("new row")
            
            if CheckGridEnd() == true then
                genGrid = false
                break
                
            else
                NewRow()
                
            end
            
        else
            MarkGridPos(markerPos)
        end        
    end          
end

-- Marks the given pos in the table
function MarkGridPos(pos)

    print(pos)
    grid[#grid + 1] = realPos
    --print("Marked Pos:"..  pos.x..", ".. pos.y)
    --print(" pos " .. #grid)
end

-- Stops infinate loops
function RunTimer()
    runTimer = runTimer + 1

    if runTimer >= 400 then    
        genGrid = false
        --print(#grid)
        --print("false")
    end
end

-- Slides the marker to its right by one unit of spacing
function SlideMarker()
    markerPos.x = markerPos.x + gridSpac
    --print("x" .. markerPos.x)
end

-- Checks to see if the markerPos.x is greater then the gridDim.x to make a new row or not 
function CheckRowEnd()
    local retVal = false
    
    if markerPos.x > gridDim.x then
        retVal = true
    end
    
    return retVal

end

-- Prints the contents of the grid table
function PrintGrid()
    for i =1, #grid do
        print(grid[i])        
    end
end       

-- Checks if the grid has reach its max size        
function CheckGridEnd()
    
    local retVal = false
    local potNewRowY = markerPos.y - gridSpac
    
    if potNewRowY > gridDim.y then
        retVal = true
    end
    
    return retVal
end
 
-- Re-sets the markerPos for a new row of points
function NewRow()
    rowCount = rowCount + 1
    markerPos.x = gridPos.x
    markerPos.y = gridPos.y - rowCount * gridSpac
end

@tnolgy Thats a really cool program

I think this is the exact same storage method, but it works just fine. Investigation continues.

-- Grid Ex

-- Use this function to perform your initial setup
function setup()
    pos = vec2(100, 100)
    pos.x = pos.x + 50
    print(pos)
    
    posTable = {}
    
    StorePos(pos)
    
    print(posTable[1])
end

function StorePos(pos)
    posTable[#posTable + 1] = pos
end 

Bingo! This code replicates the problem. For some reason the whole table replicates the last item in the table. The first two prints should be the same as the 3rd and 4th! But there not! Yes!

-- Grid Ex

-- Use this function to perform your initial setup
function setup()
    pos = vec2(100, 100)
    pos.x = pos.x + 50
  
    posTable = {}
    
    StorePos(pos)
    pos.x = pos.x + 50
    StorePos(pos)
    
    PrintTable()
end

function StorePos(pos)
    posTable[#posTable + 1] = pos
    print(pos)
end 

function PrintTable()
    for i=1, #posTable do
        print(posTable[i])
    end
end

@Goatboy76. When you store pos, then change pos.x and store it again, you are actually storing two references to the same object. You would need to make a copy of the vec2 to store a new value.

@syntonica That makes sense now. I din’t know that vectors are stored as references and not copies.

I changed this line in the example and it worked, hurrah! It works because the variable is local and resets every time and it creates a copy and not a reference.

Thanks

local realPos = vec2(pos.x, pos.y)

@Goatboy76 Glad I was able to be some use around here. :slight_smile: