Question

Did I mess up something somewhere? If i put a putsxy in the same box, it renders a string just fine, but for some reason this code hates objects. What this code is supposed to do is render a set of randomly positioned 't's on the screen, and this works fine without objects. However, when I add objects, nothing renders. What am I doing wrong? Help is much appreciated. Code is as follows:

-------------------------------
-- Asterix v0.0.3 --
-- Copyright 2013 WilliamTDR. --
-------------------------------

-- prerequestities --
require "actions" -- button input
require "math" -- random numbers

-- Global Variables --
local player = {
    health = 20, -- player health
    foodparts = 12, -- portions of food until the food level depletes by one
    food = 40, -- player food
    x = 50, -- player x position
    y = 50, -- player y position
    roomx = 0, -- x value of room player is in
    roomy = 0 -- y value of room player is in
}

local object = {
    x = 30,
    y = 30,
    roomx = 0,
    roomy = 0,
    name = "Unknown Object",
    touchingplayer = false,
    symbol = "u"
}

local objects = {}

function object:new (o)
    o = o or {}   -- create object if user does not provide one
    setmetatable(o, self)
    self.__index = self
    return o
end

function player.refresh()
    -- reset player values
    player.food = 40
    player.health = 20
    player.foodparts = 12
    player.x = 50
    player.y = 50
    player.roomx = 0
    roomy = 0
end

function player.die()
    rb.lcd_clear_display() -- clear the display
    rb.lcd_putsxy(1, 7, "You have died!")
    rb.lcd_putsxy(1, 45, "Respawning in 3...")
    rb.lcd_update()
    rb.sleep(rb.HZ) -- sleep for one second
    rb.lcd_putsxy(1, 45, "Respawning in 2...")
    rb.lcd_update()
    rb.sleep(rb.HZ)
    rb.lcd_putsxy(1, 45, "Respawning in 1...")
    rb.lcd_update()
    rb.sleep(rb.HZ)
    player.refresh()
    render()
end

function renderobjects()
    for _,object in ipairs(objects) do
        if player.roomx == object.roomx and player.roomy == object.roomy then
            rb.putsxy(object.x, object.y, object.symbol)
        end
    end
    rb.lcd_update()
end

function createnewobjects()
    obj1 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj2 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj3 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj4 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj5 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj6 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj7 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj8 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj9 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj10 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj11 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj12 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj13 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}             
    obj14 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj15 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj16 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj17 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj18 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj19 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    obj20 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
    table.insert(obj1, objects)
    table.insert(obj2, objects)
    table.insert(obj3, objects)
    table.insert(obj4, objects)
    table.insert(obj5, objects)
    table.insert(obj6, objects)
    table.insert(obj7, objects)
    table.insert(obj8, objects)
    table.insert(obj9, objects)
    table.insert(obj10, objects)
    table.insert(obj11, objects)
    table.insert(obj12, objects)
    table.insert(obj13, objects)
    table.insert(obj14, objects)
    table.insert(obj15, objects)
    table.insert(obj16, objects)
    table.insert(obj17, objects)
    table.insert(obj18, objects)
    table.insert(obj19, objects)
    table.insert(obj20, objects)
end

function checks()
    if player.foodparts < 1 then
        player.foodparts = 12
        player.food = player.food - 1
    end

    if player.food < 1 then
        player.health = player.health - 1
    end

    if player.health < 1 then
        player.die()
    end

    if player.x < 1 then
        player.roomx = player.roomx + 1
        createnewobjects()
        player.x = 92
    end

    if player.x > 93 then
        player.roomx = player.roomx - 1
        createnewobjects()
        player.x = 4
    end

    if player.y < 1 then
        player.roomy = player.roomy + 1
        createnewobjects()
        player.y = 92
    end

    if player.y > 93 then
        player.roomy = player.roomy - 1
        renderobjects()
        player.y = 4
    end

    end

function render()
    checks()
    rb.lcd_set_background(0, 255, 0) -- background is black
    rb.lcd_set_foreground(-1, 0, 0) -- foreground is white
    rb.lcd_clear_display()
    rb.lcd_fillrect(5, 3, (player.health * 2), 2)
    rb.lcd_set_foreground(88, 2, 2)
    rb.lcd_fillrect(45, 3, player.food, 2)
    rb.lcd_set_foreground(125, 125, 0)
    rb.lcd_putsxy(player.x, player.y, "*")
    renderobjects()
    rb.lcd_update()
end

function LoadGame()
render()
    while true do
        button = rb.get_plugin_action(0)
        if button == 182 then
            player.x = player.x - 1
            player.foodparts = player.foodparts - 1
            render()
        elseif button == 183 then
            player.x = player.x + 1
            player.foodparts = player.foodparts - 1
            render()
        elseif button == 180 then
            player.y = player.y - 1
            player.foodparts = player.foodparts - 1
            render()
        elseif button == 181 then
            player.y = player.y + 1
            player.foodparts = player.foodparts - 1
            render()
        elseif button == 186 then
            player.x = player.x - 4
            player.foodparts = player.foodparts - 4
            LoadGame()
        elseif button == 187 then
            player.x = player.x + 4
            player.foodparts = player.foodparts - 4
            LoadGame()
        elseif button == 184 then
            player.y = player.y - 4
            player.foodparts = player.foodparts - 4
            LoadGame()
        elseif button == 185 then
            player.y = player.y + 4
            player.foodparts = player.foodparts - 4
            LoadGame()
        elseif button == 191 then
            ShowMainMenu() -- start the program by displaying the main menu of the script.
        end
    end
end


function ShowMainMenu()
    mainmenu = {"New Game", "Load Game", "About MCRE", "Exit"}

    while true do
        s = rb.do_menu("Asterix v0.0.1", mainmenu, nil, false)

        if     s == 0 then LoadGame()
        elseif s == 1 then rb.splash(3 * rb.HZ, "No maps found! Create one first.")
        elseif s == 2 then rb.splash(5 * rb.HZ, "Game ©2013 WilliamTDR. Version 0.0.1, build 4. Minecraft is a registered trademark of Notch Development AB.")
        elseif s == 3 then os.exit()
        elseif s == -2 then os.exit()
        else rb.splash(2 * rb.HZ, "Whoops: Unexpected Button press!" .. s)
        end
    end
end

ShowMainMenu() -- start the program by displaying the main menu of the script.
Was it helpful?

Solution

You need to swap the order of the arguments to table.insert -- it should be

table.insert(objects, obj1)

not

table.insert(obj1, objects)

You can also use

setmetatable(objects,{__index=table})
objects:insert(obj1)
-- etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top