Lua <eof> expected near 'print' - lua

I'm having trouble with in lua.
Problem one:
Every time I run the following program the console tells me:
'end' expected (to close 'function' at line 1) near 'local'
Please notice where I've marked in all caps comments details about the error
function m11()
local inst = mc.mcGetInstance() -- controller instance
local gcodeLineNbr = mc.mcCntlGetGcodeLineNbr(inst) -- get current Gcode line number
local gcodeLineStr = mc.mcCntlGetGcodeLine(inst, gcodeLineNbr) -- get current Gcode line
function valFromLine(string, axis)
local startPoint = string.find(string, axis) + 1
local outputVal = ""
local isNum = true
while isNum do
local num = string.sub(string, startPoint, startPoint)
startPoint = startPoint + 1
if num ~= " " then
outputVal = outputVal .. num
else
isNum = false
end
end
outputVal = tonumber(outputVal)
end
return outputVal
--COMPILER HIGHLIGHTS FOLLOWING LINE AS LOCATION OF ERROR
local gcodeLocX = valFromLine(gcodeLineStr, "X")
local curLocX = mc.mcAxisGetPos(inst, 0) -- get current X axis value
local curLocY = mc.mcAxisGetPos(inst, 1) -- get current Y axis value
local curLocZ = mc.mcAxisGetPos(inst, 2) -- get current Z axis value
local curAngB = mc.mcAxisGetPos(inst, 4) -- get current C axis value
local curAngC = mc.mcAxisGetPos(inst, 5) -- get current C axis value
local toolOffset = mc.mcCntlGetToolOffset(inst, 2) -- get tool offset for axis Z
function rotateToolB()
local comHypot = toolOffset * math.sin(angle) -- get XY planar dist from C pivot to tool centre point
local compDestinX = comHypot * math.sin(math.rad(90) - curAxisC + curLocX
end
end
--END OF M11() FUNCTION SHOULD BE HERE
if (mc.mcInEditor() == 1) then
m11()
end
I can't see why it's expecting m11() to close so early.
Problem two:
I rewrote valFromLine() in a completely separate file and I get:
'eof' expected near 'print'
function valFromLine(string, axis)
local startPoint = string.find(string, axis) + 1
local outputVal = ""
local isNum = true
while isNum do
local num = string.sub(string, startPoint, startPoint)
startPoint = startPoint + 1
if num ~= " " then
outputVal = outputVal .. num
else
isNum = false
end
end
outputVal = tonumber(outputVal)
end
return outputVal
print(valFromLine("GO1 X254.348 Y1039.456 Z150.13 B90.23 C13 M11", "X"))
I've counted my 'end' statements, I can't find what's wrong with them in either examples of code. I'm at a complete loss for ideas at this point, please help. Thanks.

The line return outputVal is at the wrong position. Move it into the function valFromLine.
You cannot return outside the function.
correct:
function someFunction()
-- do something
local something = "something"
return something
end
wrong:
function someFunction()
-- do something
local something = "something"
end
return something
Defining global functions withn a function is also not very clean, use locals.

Related

Computercraft program printing new line instead of updating previous line

I increment y (the y index of the monitor) by one each time I print a line to advance the y value (basically a newline). I'm running into an issue however, when I have done mon_1.setCursorPos(x, y+1) it updates the previous line as it should, however when I do y = y+1 mon_1.setCursorPos(x, y) it prints a new line without updating the previous line. Any suggestions?
Here's the full code
--Define some colors
color_fuel = 16
color_ok = 2243
color_error = 224455
color_blank = 0
color_active = 2240
--Tables
reactors={}
max_energy={}
produced={}
percent_capacity={}
--Globals
x = 1
y = 1
i = 0
--Lamps
lamp_1 = peripheral.wrap("colorful_lamp_0")
lamp_2 = peripheral.wrap("colorful_lamp_1")
lamp_3 = peripheral.wrap("colorful_lamp_2")
--Monitors
mon_1 = peripheral.wrap("monitor_3")
--Cap banks
cap_1 = peripheral.wrap("capacitor_bank_2")
--Helper functions
function flash_fuel_lamp()
lamp_2.setLampColor(color_blank)
sleep(0.01)
lamp_2.setLampColor(color_error)
for i = 10,1,-1 do
lamp_1.setLampColor(color_fuel)
sleep(1)
lamp_1.setLampColor(color_blank)
sleep(1)
end
lamp_1.setLampColor(color_fuel)
end
function clear_lamps()
lamp_1.setLampColor(color_blank)
lamp_2.setLampColor(color_blank)
lamp_3.setLampColor(color_blank)
end
--Boot Sequence
--Set lamps to black and clear screen
clear_lamps()
mon_1.clear()
--Turn on the reactors and set the status light to green
for k,v in pairs(peripheral.getNames()) do
if(string.find(v, "BigReactors")) then
reactors[i] = peripheral.wrap(v)
i = i + 1
else
--print("Other Peripheral Detected")
end
end
for k,v in pairs(reactors) do
v.setActive(true)
end
lamp_2.setLampColor(color_ok)
lamp_3.setLampColor(color_active)
--Main loop
for key,value in pairs(max_energy) do
mon_1.setCursorPos(x, y)
mon_1.write("Reactor " .. key .. " Max Energy: " .. value)
y = y + 1
end
while true do
--Check the fuel level
if(redstone.getAnalogInput("bottom") == 0) then
lamp_3.setLampColor(color_blank)
for k,v in pairs(reactors) do
v.setActive(false)
end
flash_fuel_lamp()
end
for k,v in pairs(reactors) do
if(v.getActive() == false) then
lamp_3.setLampColor(color_blank)
end
--Calculate the energies
max_energy[k] = v.getEnergyStored()
produced[k] = v.getEnergyProducedLastTick()
percent_capacity[k] = (max_energy[k] / produced[k]) * 100
end
--[[max_energy = reactor.getEnergyStored()
energy_produced = reactor.getEnergyProducedLastTick()
percent_capacity = (max_energy / energy_produced) * 100]]
max_energy_cap = cap_1.getMaxEnergyStored()
energy_stored = cap_1.getEnergyStored()
percent_capacity_cap = (energy_stored / max_energy_cap) * 100
y=y+1
mon_1.setCursorPos(x, y)
for key,value in pairs(percent_capacity) do
for k,v in pairs(reactors) do
mon_1.write("Reactor " .. k .. " Percent Capacity: " .. value)
y=y+1
mon_1.setCursorPos(x, y)
mon_1.write("Capacitor output per tick: " .. cap_1.getAverageOutputPerTick())
y=y+1
mon_1.setCursorPos(x, y)
mon_1.write("Capacitor input per tick: " .. cap_1.getAverageInputPerTick())
y=y+1
mon_1.setCursorPos(x, y)
end
end
sleep(0.01)
end```
So I think that you might be forgetting that when you do mon_1.setCursorPos(x, y+1) you're not incrementing the y variable, you're only passing a higher value to the called function, while with y = y+1 mon_1.setCursorPos(x, y) you are incrementing y.
This explains why the previous line is not being updated when incrementing y, since the nested for-loops at the bottom of your code use that y variable as well.

Attempt to index field (a nil value), for an Object

I have a problem with trying to add things to a LOVE2D version of Match-3. (From the CS50 course)
I added a function swapTiles() into my Board class and made a class object called self.board in a Class called PlayState. Then when I try to access the new function, it says this error:
Error
src/states/PlayState.lua:155: attempt to index field 'board' (a nil value)
I'll provide my Board and PlayState class below:
Board: (keep in mind the new function is literally in the code)
Board = Class{}
function Board:init(x, y, level) -- Added "level" as an integer for the block variations.
self.x = x
self.y = y
self.matches = {}
self.level = level
self:initializeTiles()
end
function Board:swapTiles(tile1, tile2)
-- swap grid positions of tiles
local tempX = tile1.gridX
local tempY = tile1.gridY
tile1.gridX = tile2.gridX
tile1.gridY = tile2.gridY
tile2.gridX = tempX
tile2.gridY = tempY
-- swap tiles in the tiles table
self.tiles[tile1.gridY][tile1.gridX] = tile1
self.tiles[tile2.gridY][tile2.gridX] = tile2
end
function Board:initializeTiles()
self.tiles = {}
-- There should only be two shiny tiles.
for tileY = 1, 8 do
-- empty table that will serve as a new row
table.insert(self.tiles, {})
for tileX = 1, 8 do
self.isPowerup = false
if math.random(1, 25) == 4 then
self.isPowerup = true
end
-- create a new tile at X,Y with a random color and variety
table.insert(self.tiles[tileY], Tile(tileX, tileY, math.random(18), math.min(8, math.random(1, self.level)), self.isPowerup))
end
end
while self:calculateMatches() do
-- recursively initialize if matches were returned so we always have
-- a matchless board on start
self:initializeTiles()
end
end
--[[
Goes left to right, top to bottom in the board, calculating matches by counting consecutive
tiles of the same color. Doesn't need to check the last tile in every row or column if the
last two haven't been a match.
]]
function Board:calculateMatches()
local matches = {}
-- how many of the same color blocks in a row we've found
local matchNum = 1
-- horizontal matches first
for y = 1, 8 do
local colorToMatch = self.tiles[y][1].color
matchNum = 1
-- every horizontal tile
for x = 2, 8 do
-- if this is the same color as the one we're trying to match...
if self.tiles[y][x].color == colorToMatch then
matchNum = matchNum + 1
else
-- set this as the new color we want to watch for
colorToMatch = self.tiles[y][x].color
-- if we have a match of 3 or more up to now, add it to our matches table
if matchNum >= 3 then
local match = {}
-- go backwards from here by matchNum
for x2 = x - 1, x - matchNum, -1 do
-- add each tile to the match that's in that match
table.insert(match, self.tiles[y][x2])
-- Shiny Check
if self.tiles[y][x2].isShiny == true then
for i = 1, 8 do
table.insert(match, self.tiles[y][i])
end
end
end
-- add this match to our total matches table
table.insert(matches, match)
end
-- don't need to check last two if they won't be in a match
if x >= 7 then
break
end
matchNum = 1
end
end
-- account for the last row ending with a match
if matchNum >= 3 then
local match = {}
-- go backwards from end of last row by matchNum
for x = 8, 8 - matchNum + 1, -1 do
table.insert(match, self.tiles[y][x])
end
table.insert(matches, match)
end
end
-- vertical matches
for x = 1, 8 do
local colorToMatch = self.tiles[1][x].color
matchNum = 1
-- every vertical tile
for y = 2, 8 do
if self.tiles[y][x].color == colorToMatch then
matchNum = matchNum + 1
else
colorToMatch = self.tiles[y][x].color
if matchNum >= 3 then
local match = {}
for y2 = y - 1, y - matchNum, -1 do
table.insert(match, self.tiles[y2][x])
if self.tiles[y2][x].isShiny == true then
for i = 1, 8 do
table.insert(match, self.tiles[i][x])
end
end
end
table.insert(matches, match)
end
matchNum = 1
-- don't need to check last two if they won't be in a match
if y >= 7 then
break
end
end
end
-- account for the last column ending with a match
if matchNum >= 3 then
local match = {}
-- go backwards from end of last row by matchNum
for y = 8, 8 - matchNum, -1 do
table.insert(match, self.tiles[y][x])
end
table.insert(matches, match)
end
end
-- store matches for later reference
self.matches = matches
-- return matches table if > 0, else just return false
return #self.matches > 0 and self.matches or false
end
--[[
Remove the matches from the Board by just setting the Tile slots within
them to nil, then setting self.matches to nil.
]]
function Board:removeMatches()
for k, match in pairs(self.matches) do
for k, tile in pairs(match) do
self.tiles[tile.gridY][tile.gridX] = nil
end
end
self.matches = nil
end
--[[
Shifts down all of the tiles that now have spaces below them, then returns a table that
contains tweening information for these new tiles.
]]
function Board:getFallingTiles()
-- tween table, with tiles as keys and their x and y as the to values
local tweens = {}
-- for each column, go up tile by tile till we hit a space
for x = 1, 8 do
local space = false
local spaceY = 0
local y = 8
while y >= 1 do
-- if our last tile was a space...
local tile = self.tiles[y][x]
if space then
-- if the current tile is *not* a space, bring this down to the lowest space
if tile then
-- put the tile in the correct spot in the board and fix its grid positions
self.tiles[spaceY][x] = tile
tile.gridY = spaceY
-- set its prior position to nil
self.tiles[y][x] = nil
-- tween the Y position to 32 x its grid position
tweens[tile] = {
y = (tile.gridY - 1) * 32
}
-- set space back to 0, set Y to spaceY so we start back from here again
space = false
y = spaceY
spaceY = 0
end
elseif tile == nil then
space = true
if spaceY == 0 then
spaceY = y
end
end
y = y - 1
end
end
-- create replacement tiles at the top of the screen
for x = 1, 8 do
for y = 8, 1, -1 do
local tile = self.tiles[y][x]
-- if the tile is nil, we need to add a new one
if not tile then
local tile = Tile(x, y, math.random(18), math.random(1, self.level))
tile.y = -32
self.tiles[y][x] = tile
tweens[tile] = {
y = (tile.gridY - 1) * 32
}
end
end
end
return tweens
end
function Board:getNewTiles()
return {}
end
function Board:testForMatches()
for y = 1, 8 do
for x = 1, 8 do
-- Test for left swap
if x > 1 then
end
end
end
end
function Board:render()
for y = 1, #self.tiles do
for x = 1, #self.tiles[1] do
self.tiles[y][x]:render(self.x, self.y)
end
end
end
Here's my PlayState: (look for PlayState:swapTiles(), keep in mind that the self.board is being used several times for and works fine, except when i try calling self.board:swapTiles().)
PlayState = Class{__includes = BaseState}
function PlayState:init()
-- start our transition alpha at full, so we fade in
self.transitionAlpha = 255
-- position in the grid which we're highlighting
self.boardHighlightX = 0
self.boardHighlightY = 0
-- timer used to switch the highlight rect's color
self.rectHighlighted = false
-- flag to show whether we're able to process input (not swapping or clearing)
self.canInput = true
-- tile we're currently highlighting (preparing to swap)
self.highlightedTile = nil
self.score = 0
self.timer = 60
-- set our Timer class to turn cursor highlight on and off
Timer.every(0.5, function()
self.rectHighlighted = not self.rectHighlighted
end)
-- subtract 1 from timer every second
Timer.every(1, function()
self.timer = self.timer - 1
-- play warning sound on timer if we get low
if self.timer <= 5 then
gSounds['clock']:play()
end
end)
end
function PlayState:enter(params)
-- grab level # from the params we're passed
self.level = params.level
-- spawn a board and place it toward the right
self.board = params.board or Board(VIRTUAL_WIDTH - 272, 16)
-- grab score from params if it was passed
self.score = params.score or 0
-- score we have to reach to get to the next level
self.scoreGoal = self.level * 1.25 * 1000
end
function PlayState:update(dt)
if love.keyboard.wasPressed('escape') then
love.event.quit()
end
-- go back to start if time runs out
if self.timer <= 0 then
-- clear timers from prior PlayStates
Timer.clear()
gSounds['game-over']:play()
gStateMachine:change('game-over', {
score = self.score
})
end
-- go to next level if we surpass score goal
if self.score >= self.scoreGoal then
-- clear timers from prior PlayStates
-- always clear before you change state, else next state's timers
-- will also clear!
Timer.clear()
gSounds['next-level']:play()
-- change to begin game state with new level (incremented)
gStateMachine:change('begin-game', {
level = self.level + 1,
score = self.score
})
end
if self.canInput then
-- move cursor around based on bounds of grid, playing sounds
if love.keyboard.wasPressed('up') then
self.boardHighlightY = math.max(0, self.boardHighlightY - 1)
gSounds['select']:play()
elseif love.keyboard.wasPressed('down') then
self.boardHighlightY = math.min(7, self.boardHighlightY + 1)
gSounds['select']:play()
elseif love.keyboard.wasPressed('left') then
self.boardHighlightX = math.max(0, self.boardHighlightX - 1)
gSounds['select']:play()
elseif love.keyboard.wasPressed('right') then
self.boardHighlightX = math.min(7, self.boardHighlightX + 1)
gSounds['select']:play()
end
-- if we've pressed enter, to select or deselect a tile...
if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
-- if same tile as currently highlighted, deselect
local x = self.boardHighlightX + 1
local y = self.boardHighlightY + 1
-- if nothing is highlighted, highlight current tile
if not self.highlightedTile then
self.highlightedTile = self.board.tiles[y][x]
-- if we select the position already highlighted, remove highlight
elseif self.highlightedTile == self.board.tiles[y][x] then
self.highlightedTile = nil
-- if the difference between X and Y combined of this highlighted tile
-- vs the previous is not equal to 1, also remove highlight
elseif math.abs(self.highlightedTile.gridX - x) + math.abs(self.highlightedTile.gridY - y) > 1 then
gSounds['error']:play()
self.highlightedTile = nil
else
self:swapTiles(self.highlightedTile, self.board.tiles[y][x], true)
end
end
end
Timer.update(dt)
end
function PlayState:swapTiles(tile1, tile2, swapBackAtNoMatch)
local tile1 = tile1
local tile2 = tile2
local swapBackAtNoMatch = swapBackAtNoMatch
self.board:swapTiles(tile1, tile2) -- Causes the nil error.
if swapBackAtNoMatch then
-- tween coordinates between two swapping tiles
Timer.tween(0.1, {
[tile1] = {x = tile2.x, y = tile2.y},
[tile2] = {x = tile1.x, y = tile1.y}
})
-- once they've swapped, tween falling blocks
:finish(function ()
local matches = self.board:calculateMatches()
if matches then
self.calculateMatches(matches)
else
-- swap back if there's no match
self.swapTiles(tile1, tile2, false)
gSounds['error']:play()
end
end)
else
-- tween coordinates between the two so they swap
Timer.tween(0.1, {
[tile1] = {x = tile2.x, y = tile2.y},
[tile2] = {x = tile1.x, y = tile1.y}})
end
end
--[[
Calculates whether any matches were found on the board and tweens the needed
tiles to their new destinations if so. Also removes tiles from the board that
have matched and replaces them with new randomized tiles, deferring most of this
to the Board class.
]]
function PlayState:calculateMatches()
self.highlightedTile = nil
-- if we have any matches, remove them and tween the falling blocks that result
local matches = self.board:calculateMatches()
if matches then
gSounds['match']:stop()
gSounds['match']:play()
-- add score for each match
for k, match in pairs(matches) do
local varietyPoints = 0 -- We'll keep track of the bonus variety points here
-- We'll use vareity to calculate points for each tile within a match
for j, tiles in pairs(match) do
varietyPoints = varietyPoints + tiles.variety * 25
end
self.score = self.score + (#match * 50) + varietyPoints
-- Also add one second times the number of match to the timer
self.timer = self.timer + #match * 1
end
-- remove any tiles that matched from the board, making empty spaces
self.board:removeMatches()
-- gets a table with tween values for tiles that should now fall
local tilesToFall = self.board:getFallingTiles()
-- first, tween the falling tiles over 0.25s
Timer.tween(0.25, tilesToFall):finish(function()
local newTiles = self.board:getNewTiles()
-- then, tween new tiles that spawn from the ceiling over 0.25s to fill in
-- the new upper gaps that exist
Timer.tween(0.25, newTiles):finish(function()
-- recursively call function in case new matches have been created
-- as a result of falling blocks once new blocks have finished falling
self:calculateMatches()
end)
end)
-- if no matches, we can continue playing
else
self.canInput = true
end
end
Honestly doesn't make much sense. Please help!
function PlayState:swapTiles(tile1, tile2, swapBackAtNoMatch) end
is syntactic sugar for
PlayState.swaptiles = function(self, tile1, tile2, swapBackAtNoMatch) end
That's the reason why you can work with self inside that function.
A function defined like that needs to be called using the colon operator as well to make this work. Or you explicitly provide the table as first parameter.
Hence in your call self.swapTiles(tile1, tile2, false)
self is going to be tile1
tile1.board is nil so self.board is nil in this function call which causes the error
You have to call self:swapTiles(tile1, tile2, false) or self.swapTiles(self, tile1, tile2, false)
Please make sure you fully understand the colon syntax.

Making a cubic ease-in-out curve dynamically switch its goal position

Using, Lua, I've made a function to cubically (easing in/"accelerating", then easing out/"decelerating" afterwards) interpolate from one number to another; SimpleSpline takes a number between 0-1 (the time the animation's been going, easiest put), and SimpleSplineBetween does the same, but keeps it in between two given minimum/maximum values.
function SimpleSpline( v )
local vSquared = v*v
return (3 * vSquared - 2 * vSquared * v)
end
function SimpleSplineBetween( mins, maxs, v )
local fraction = SimpleSpline( v )
return (maxs * fraction + mins * (1 - fraction))
end
It all works fine. However, I've run into a bit of an issue. I'd like this to be a bit more dynamic. For example, assume my "mins" is 0.5, and my "maxs" is 1, then I have a variable for time that I pass as V; we'll say it's 0.5, so our current interpolation value is 0.75. Now, let's also assume that suddenly, "maxs" is jerked up to 0.25, so now, we have a new goal to reach.
My current approach to handling situations like the above is to reset our "time" variable and change "mins" to our current value; in the above case 0.75, etc. However, this produces a very noticable "stop" or "freeze" in the animation, because it is being entirely reset.
My question is, how can I make this dynamic without that stop? I'd like it to transition smoothly from moving to one goal number to another.
local Start_New_Spline, Calculate_Point_on_Spline, Recalculate_Old_Spline
do
local current_spline_params
local function Start_New_Spline(froms, tos)
current_spline_params = {d=0, froms=froms, h=tos-froms, last_v=0}
end
local function Calculate_Point_on_Spline(v) -- v = 0...1
v = v < 0 and 0 or v > 1 and 1 or v
local d = current_spline_params.d
local h = current_spline_params.h
local froms = current_spline_params.froms
current_spline_params.last_v = v
return (((d-2*h)*v+3*h-2*d)*v+d)*v+froms
end
local function Recalculate_Old_Spline(new_tos)
local d = current_spline_params.d
local v = current_spline_params.last_v
local h = current_spline_params.h
local froms = current_spline_params.froms
froms = (((d-2*h)*v+3*h-2*d)*v+d)*v+froms
d = ((3*d-6*h)*v+6*h-4*d)*v+d
current_spline_params = {d=d, froms=froms, h=new_tos-froms, last_v=0}
end
end
Usage example according to your values:
Start_New_Spline(0.5, 1) -- "mins" is 0.5, "maxs" is 1
local inside_spline = true
while inside_spline do
local goal_has_changed = false
for time = 0, 1, 0.015625 do -- time = 0...1
-- It's time to draw next frame
goal_has_changed = set to true when goal is changed
if goal_has_changed then
-- time == 0.5 -> s == 0.75, suddenly "maxs" is jerked up to 0.25
Recalculate_Old_Spline(0.25) -- 0.25 is the new goal
-- after recalculation, "time" must be started again from zero
break -- exiting this loop
end
local s = Calculate_Point_on_Spline(time) -- s = mins...maxs
Draw_something_at_position(s)
wait()
end
if not goal_has_changed then
inside_spline = false
end
end

Error in function rembuff:floor() in lua code file: attempt to call method 'floor' (a nil value)

In Machine Translation Dataset I have successfully pre-trained my model in Lua. Now I move to train my model.
But I get the error in a Lua file in the function rembuff:floor()
Error: Attempt to call method 'floor' (a nil value)
This is that specific function :
function MarginBatchBeamSearcher:nextSearchStep(t, batch_pred_inp, batch_ctx, beam_dec, beam_scorer,gold_scores, target, target_w, gold_rnn_state_dec, delts, losses, global_noise)
local K = self.K
local resval, resind, rembuff = self.resval, self.resind, self.rembuff
local finalval, finalind = self.finalval, self.finalind
self:synchDropout(t, global_noise)
-- pred_inp should be what was predicted at the last step
local outs = beam_dec:forward({batch_pred_inp, batch_ctx, unpack(self.prev_state)})
local all_scores = beam_scorer:forward(outs[#outs]) -- should be (batch_l*K) x V matrix
local V = all_scores:size(2)
local mistaken_preds = {}
for n = 1, self.batch_size do
delts[n] = 0
losses[n] = 0
if t <= target_w[n]-1 then -- only do things if t <= length (incl end token) - 2
local beam_size = #self.pred_pfxs[n]
local nstart = (n-1)*K+1
local nend = n*K
local scores = all_scores:sub(nstart, nstart+beam_size-1):view(-1) -- scores for this example
-- take top K
torch.topk(resval, resind, scores, K, 1, true)
-- see if we violated margin
torch.min(finalval, finalind, resval, 1) -- resind[finalind[1]] is idx of K'th highest predicted word
-- checking that true score at least 1 higher than K'th
losses[n] = math.max(0, 1 - gold_scores[n][target[t+1][n]] + finalval[1])
-- losses[n] = math.max(0, - gold_scores[n][target[t+1][n]] + finalval[1])
if losses[n] > 0 then
local parent_idx = math.ceil(resind[finalind[1]]/V)
local pred_word = ((resind[finalind[1]]-1)%V) + 1
mistaken_preds[n] = {prev = self.pred_pfxs[n][parent_idx], val = pred_word}
delts[n] = 1 -- can change.....
else
-- put predicted next words in pred_inp
rembuff:add(resind, -1) -- set rembuff = resind - 1
rembuff:div(V)
--if rembuff.floor then
rembuff:floor()
I am unable to rectify this error :
Please help !

Corona SDK: Inserting into tables

I've read the other topics related to this subject and cannot make sense of them in the sense of my code. In the code below, I cannot get the make other word buttons into the table. I can generate the words but they will not go into the table. The previous function with the correct word works fine. What am I doing wrong? Do I have problems elsewhere in the code?
--main text
local content = require "content"
--chooses a random number according to the maximum number available in the table
local defaultWidth = 1024
local defaultHeight = 768
local displayWidth = display.viewableContentWidth
local displayHeight = display.viewableContentHeight
local yMargin = 20
local centerX = defaultWidth/2;
local centerY = defaultHeight/2;
local xAdjust = (defaultWidth - display.viewableContentWidth)/2
local yAdjust = (defaultHeight - display.viewableContentHeight)/2
local rnd = math.random
local maxSightwords = 3
local currQuestion = 0
local playOrder
local letterButtons
local wrongGraphic
local correctButton
--local wordButtons
function getRandomOrder(amount)
local order ={}
local i
local temp
local temp1
for n = 1,amount do
order[n] = n
end
for i=0,9 do
for temp = 1,amount do
n = math.random(1, amount)
temp1 = order[temp]
order[temp] = order[n]
order[n] = temp1
end
end
return order
end
-- assign random order for words
playOrder = getRandomOrder(#content)
function nextQuestion()
-- update question number index
currQuestion = currQuestion+1
if currQuestion > #playOrder then
currQuestion = 1
end
local questionNumber = playOrder[currQuestion]
print("Question# "..currQuestion)
print("id "..content[questionNumber].id)
-- make word buttons
wordButtons = {}
-- make word button for correct word
local word = content[playOrder[currQuestion]].word
table.insert(wordButtons, newWordButton(word))
correctButton = wordButtons[1].graphics
local buttonWidth = 150
print ("correct: "..word)
print (#wordButtons)
---[[
-- ****make other word buttons***
local otherWords = getRandomWords(content.word)
--print (otherWords)
for i=1, maxSightwords-1 do
table.insert(wordButtons, otherWords)
end
--]]
print (#wordButtons)
-- position letter buttons and add touch event listener
local randomWordOrder = getRandomOrder(#wordButtons)
local buttonSpacing = buttonWidth * 1.5
local buttonsWidth = (#wordButtons * buttonWidth) + ((#wordButtons-1) * (buttonSpacing/4))
local buttonsX = centerX - (buttonWidth)
for i=1, #wordButtons do
local button = wordButtons[i].graphics
button.y = centerY
button.x = buttonsX + (buttonSpacing * (randomWordOrder[i]-1))
button:addEventListener("touch", onWordTouch)
--local randomDelay = transitionDuration + (math.random(1,10) * 10)
--transition.from(button, {time = 500, delay = randomDelay, y = defaultHeight + button.height})
end
end
function clearQuestion()
-- remove wrongGraphic if present
if wrongGraphic then
wrongGraphic:removeSelf()
wrongGraphic = nil
end
-- remove all word buttons
for i=1,#wordButtons do
wordButtons[i].graphics:removeSelf()
wordButtons[i].graphics = nil
end
end
function onWordTouch(event)
local t = event.target
if "ended" == event.phase then
if t == correctButton then
onCorrect()
else
onIncorrect(t)
end
end
end
function onIncorrect(incorrectButton)
media.playSound("sounds/splat.wav")
wrongGraphic = display.newImageRect("images/graphics/wrong.png", 137, 136)
wrongGraphic.x = incorrectButton.x + incorrectButton.width/2
wrongGraphic.y = incorrectButton.y + incorrectButton.height/2
transition.to(incorrectButton, {time=100, delay=500, alpha=0})
transition.to(wrongGraphic, {time=200, delay=500, alpha=0, onComplete=wrongCompleteListener})
local wrongCompleteListener = function(obj)
obj:removeSelf()
obj = nil
incorrectButton:removeSelf()
incorrectButton = nil
end
end
function onCorrect()
-- play correct sound then display word
media.playSound("sounds/correct.mp3", playWord)
-- remove the letter buttons
clearQuestion()
-- disable the home button until new screen is shown
homeEnabled = false
end
function newWordButton(word)
local wordGraphic = display.newImageRect("images/words/"..word..".png", 150, 75)
local wordButton = {}
wordButton.graphics = display.newGroup()
wordButton.graphics:insert(wordGraphic)
wordButton.word = word
return wordButton
end
function getRandomWords ()
local wordGraphic = display.newGroup ()
for i=1,maxSightwords-1 do
--remove a word from content using a random index #
--Since the index will be between 1 and the number of words in content
--and each time through the loop a word is removed, you can be sure
--You will get 3 different words without repeats.
local next_word = table.remove(content, math.random(#content))
--next_word is a table with 'word' and 'id' fields so you can make the text display object from next_word.word
local wordText = display.newImageRect("images/words/"..next_word.id..".png", 150, 75)
wordText.x = display.contentWidth/2
wordText.y = display.contentHeight/2 - 100
wordGraphic:insert(wordText)
print (next_word.id)
end
end
nextQuestion ()
I think your problem is that you are resetting the wordButton = {} again in function newWordButton. You already made the table in function nextQuestion(), so by calling it again in the newWordButton function is reseting the entire table.
Take a look at this links:
http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/
http://docs.coronalabs.com/api/library/table/insert.html
I'm pretty sure the function should look like this:
function newWordButton(word)
local wordGraphic = display.newImageRect("images/words/"..word..".png", 150, 75)
wordButton.graphics = display.newGroup()
wordButton.graphics:insert(wordGraphic)
wordButton.word = word
return wordButton
end

Resources