GLua - part of the string library - lua

I have gui wherein I need show information from table
When I open gui, I have this error:
"attempt to index a string value with bad key ('Index' is not part of the string library)"
SH file. In this file I put table information
MyList = {}
MyList = {
Index = 1,
Name = "Name 1",
Class = "exampleclass",
Description = "Desc 1",
Model = "modelname",
Color = Color(255, 255, 255, 255)
}
CL file. This file contains the part of the code where the error occurs. The error occurs on the first line
for k, v in SortedPairsByMemberValue( MyList, "Index" ) do
if v.Class == "exampleclass" then
local mainbuttons = vgui.Create( "DCollapsibleCategory", category )
mainbuttons:Dock(TOP)
mainbuttons:DockMargin(0, 6, 0, 2)
mainbuttons.Header:SetTall(24)
mainbuttons:SetExpanded(0)
mainbuttons:SetLabel("")
mainbuttons.Text = v.Name
function mainbuttons:Paint(w, h)
local h = 24
surface.SetDrawColor(v.Color)
surface.DrawRect(0, 0, w, h)
surface.SetFont("NovuxFont.ArialLight.16")
local textw, texth = surface.GetTextSize(self.Text)
surface.SetTextColor(Color(255, 255, 255))
surface.SetTextPos(16, h / 2 - texth / 2)
surface.DrawText(self.Text)
end
local craftpanel = vgui.Create( "DPanel", mainbuttons )
craftpanel:SetPos( 0, 25 )
craftpanel:SetSize( mainframescroll:GetWide(), 250 )
craftpanel.Paint = function() -- Paint function
surface.SetDrawColor( 65, 65, 65, 0 )
surface.DrawRect( 0, 0, craftpanel:GetWide(), craftpanel:GetTall() )
end
local spoilertext = vgui.Create( "DLabel", mainbuttons )
spoilertext:SetText( v.Description )
spoilertext:SetTextColor( Color( 255, 255, 255 ) )
spoilertext:SetFont( "NovuxFont.ArialLight.16" )
spoilertext:SetPos( 108, 32 )
spoilertext:SetSize( mainframescroll:GetWide(), 25 )
spoilertext.Paint = function( self, w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 102, 102, 102, 0 ))
end
local modelframe = vgui.Create( "DModelPanel", mainbuttons )
modelframe:SetPos( 0, 65 )
modelframe:SetSize( 300, 200 )
modelframe:SetModel( v.Model )
modelframe:GetEntity():SetAngles( Angle( -10, 0, 15 ) )
local mn, mx = modelframe.Entity:GetRenderBounds()
local size = 0
size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) )
size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) )
size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) )
modelframe:SetFOV( 45 )
modelframe:SetCamPos( Vector( size, size, size ) )
modelframe:SetLookAt( (mn + mx) * 0.5 )
function modelframe:LayoutEntity( Entity )
return
end
end
end

The problem is that you put the whole item in MyList where by logic of SortedPairsByMemberValue you supposed to put new table inside MyList.
This fixes the problem:
MyList = {
{
Index = 1,
Name = "Name 1",
Class = "exampleclass",
Description = "Desc 1",
Model = "modelname",
Color = Color(255, 255, 255, 255)
}
}
Notice that now this is a nested table.

Related

why is this error coming out in Corona sdk?

12:39:31.002 ERROR: Runtime error
12:39:31.002 C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207: attempt to call method 'setFillColor' (a nil value)
12:39:31.002 stack traceback:
12:39:31.002 C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207: in function 'listener'
12:39:31.002 D:\a\corona\corona\subrepos\timer\timer.lua:331: in function 'method'
12:39:31.002 D:\a\corona\corona\platform\resources\init.lua:220: in function <D:\a\corona\corona\platform\resources\init.lua:189>
This is my code
local composer = require("composer");
local scene = composer.newScene();
local widget = require( "widget" )
local json = require("json")
display.setDefault("background", 55/255, 50/255, 47/255);
------------------------------------------------------------------------------------
local tapText
local casi = display.newText( "a", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 42 )
casi:setFillColor(0/255,196/255,253/255,1)
local yes = display.newText( "c", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 46 )
yes:setFillColor(6/255,204/255,36/255,1)
local no = display.newText( "b", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 44 )
no:setFillColor(219/255,26/255,15/255,1)
local bal
local balpl
local vrem
local scetcikBal = 0
local scetcikBalpl = 0
local scetcikLevel = 0
local settings = {
yesJ = 0, noJ = 0, procent = 0, procent2 = 0
}
local function saveSettings()
local path = system.pathForFile( "itoq.json", system.DocumentsDirectory )
local file = io.open( path, "w" )
if (file) then
local contents = json.encode(settings)
file:write( contents )
io.close( file )
return true
end
end
local secondL = 20
local function obnovVr( event )
secondL = secondL - 1
vrem.text = secondL
if secondL == 0 then
settings.yesJ = scetcikBal
settings.noJ = scetcikBalpl
settings.procent = (scetcikBal * 10)
saveSettings()
composer.gotoScene( "Level1Score")
end
end
local kor = {
{display.contentCenterX - 66, display.contentCenterY + 120},
{display.contentCenterX + 70, display.contentCenterY + 120},
{display.contentCenterX - 66, display.contentCenterY + 200},
{display.contentCenterX + 70, display.contentCenterY + 200}
}
local tb = {
{"1 + 2 = ?",{"3","2","4","1"},"1 + 2 = 3"},
{"2 + 3 = ?",{"5","4","6","3"},"2 + 3 = 5"},
{"3 + 1 = ?",{"4","3","5","1"},"3 + 1 = 4"},
{"2 + 2 = ?",{"4","2","3","1"},"2 + 2 = 4"},
{"3 + 2 = ?",{"5","3","4","1"},"3 + 2 = 5"},
{"1 + 1 = ?",{"2","5","4","1"},"1 + 1 = 2"},
{"1 + 3 = ?",{"4","1","5","2"},"1 + 3 = 4"},
{"2 + 1 = ?",{"3","2","1","4"},"2 + 1 = 3"},
{"4 + 1 = ?",{"5","3","4","1"},"4 + 1 = 5"},
{"1 + 4 = ?",{"5","4","2","3"},"1 + 4 = 5"}
}
local j
for i = #tb, 2, -1 do
j = math.random( i )
tb[i], tb[j] = tb[j], tb[i]
end
local j1
for i = #kor, 2, -1 do
j1 = math.random( i )
kor[i], kor[j1] = kor[j1], kor[i]
end
local button1
local button2
local button3
local button4
nVoprosa = 1
local t = 2
local function stop1()
button1:setEnabled( false )
button2:setEnabled( false )
button3:setEnabled( false )
button4:setEnabled( false )
tapText.text = tb[nVoprosa][3]
tapText:setFillColor(6/255, 204/255, 36/255, 1)
end
local function stop2()
button1:setEnabled( false )
button2:setEnabled( false )
button3:setEnabled( false )
button4:setEnabled( false )
tapText.text = tb[nVoprosa][3]
tapText:setFillColor(219/255, 26/255, 15/255, 1)
tapText.size = 50
end
local function vb()
t = t - 1
if t == 0 then
scetcikBal = scetcikBal + 1
bal.text = scetcikBal
scetcikLevel = scetcikLevel + 1
if ( scetcikLevel == 10) then
settings.yesJ = scetcikBal
settings.noJ = scetcikBalpl
settings.procent = (scetcikBal * 10)
saveSettings()
composer.gotoScene( "Level1Score")
else
button1:setEnabled( true )
button2:setEnabled( true )
button3:setEnabled( true )
button4:setEnabled( true )
tapText:setFillColor(255/255, 255/255, 255/255, 1 )
nVoprosa = nVoprosa + 1
tapText.text = tb[nVoprosa][1]
button1:setLabel( tb[nVoprosa][2][1] )
button2:setLabel( tb[nVoprosa][2][2] )
button3:setLabel( tb[nVoprosa][2][3] )
button4:setLabel( tb[nVoprosa][2][4] )
local j1
for i = #kor, 2, -1 do
j1 = math.random( i )
kor[i], kor[j1] = kor[j1], kor[i]
end
button1.x = kor[1][1]
button1.y = kor[1][2]
button2.x = kor[2][1]
button2.y = kor[2][2]
button3.x = kor[3][1]
button3.y = kor[3][2]
button4.x = kor[4][1]
button4.y = kor[4][2]
end
t = 2
end
end
local function vbn()
t = t - 1
if t == 0 then
scetcikBalpl = scetcikBalpl + 1
balpl.text = scetcikBalpl
scetcikLevel = scetcikLevel + 1
if ( scetcikLevel == 10) then
settings.yesJ = scetcikBal
settings.noJ = scetcikBalpl
settings.procent = (scetcikBal * 10)
saveSettings()
composer.gotoScene( "Level1Score")
else
button1:setEnabled( true )
button2:setEnabled( true )
button3:setEnabled( true )
button4:setEnabled( true )
tapText:setFillColor(255/255, 255/255, 255/255, 1)
tapText.size = 40
nVoprosa = nVoprosa + 1
tapText.text = tb[nVoprosa][1]
button1:setLabel( tb[nVoprosa][2][1] )
button2:setLabel( tb[nVoprosa][2][2] )
button3:setLabel( tb[nVoprosa][2][3] )
button4:setLabel( tb[nVoprosa][2][4] )
local j1
for i = #kor, 2, -1 do
j1 = math.random( i )
kor[i], kor[j1] = kor[j1], kor[i]
end
button1.x = kor[1][1]
button1.y = kor[1][2]
button2.x = kor[2][1]
button2.y = kor[2][2]
button3.x = kor[3][1]
button3.y = kor[3][2]
button4.x = kor[4][1]
button4.y = kor[4][2]
end
t = 2
end
end
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
stop1()
timer.performWithDelay( 300, vb, t )
--vb()
end
end
local function handleButtonEventN( event )
if ( "ended" == event.phase ) then
stop2()
timer.performWithDelay( 300, vbn, t )
--vbn()
end
end
---------------------------------------------------------------------------------------
function scene:create( event )
local sceneGroup = self.view
timer.performWithDelay( 1000, obnovVr, secondL )
tapText = display.newText("", display.contentCenterX, display.contentCenterY - 80, "Lato-Regular.ttf", 40 )
tapText.text = tb[1][1]
button1 = widget.newButton(
{
id = "button1",
label = tb[nVoprosa][2][1],
onEvent = handleButtonEvent,
labelColor = { default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 } },
fontSize = 36,
font = "Lato-Regular.ttf",
emboss = false,
shape = "roundedRect",
width = 120,
height = 60,
cornerRadius = 0,
fillColor = { default={55/255,50/255,47/255,1}, over={31/255,76/255,115/255,1} },
strokeColor = { default={0/255,196/255,253/255,1}, over={0.8,0.8,1,1} },
strokeWidth = 3
}
)
button2 = widget.newButton(
{
id = "button2",
label = tb[nVoprosa][2][2],
onEvent = handleButtonEventN,
labelColor = { default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 } },
fontSize = 36,
font = "Lato-Regular.ttf",
emboss = false,
shape = "roundedRect",
width = 120,
height = 60,
cornerRadius = 0,
fillColor = { default={55/255,50/255,47/255,1}, over={31/255,76/255,115/255,1} },
strokeColor = { default={0/255,196/255,253/255,1}, over={0.8,0.8,1,1} },
strokeWidth = 3
}
)
button3 = widget.newButton(
{
id = "button3",
label = tb[nVoprosa][2][3],
onEvent = handleButtonEventN,
labelColor = { default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 } },
fontSize = 36,
font = "Lato-Regular.ttf",
emboss = false,
shape = "roundedRect",
width = 120,
height = 60,
cornerRadius = 0,
fillColor = { default={55/255,50/255,47/255,1}, over={31/255,76/255,115/255,1} },
strokeColor = { default={0/255,196/255,253/255,1}, over={0.8,0.8,1,1} },
strokeWidth = 3
}
)
button4 = widget.newButton(
{
id = "button4",
label = tb[nVoprosa][2][4],
onEvent = handleButtonEventN,
labelColor = { default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 } },
fontSize = 36,
font = "Lato-Regular.ttf",
emboss = false,
shape = "roundedRect",
width = 120,
height = 60,
cornerRadius = 0,
fillColor = { default={55/255,50/255,47/255,1}, over={31/255,76/255,115/255,1} },
strokeColor = { default={0/255,196/255,253/255,1}, over={0.8,0.8,1,1} },
strokeWidth = 3
}
)
--
button1.x = kor[1][1]
button1.y = kor[1][2]
button2.x = kor[2][1]
button2.y = kor[2][2]
button3.x = kor[3][1]
button3.y = kor[3][2]
button4.x = kor[4][1]
button4.y = kor[4][2]
casi.x = 240
casi.y = 2
yes.x = 40
yes.y = 2
no.x = 140
no.y = 2
bal = display.newText(scetcikBal, 80, 3, "Lato-Regular.ttf", 30 )
balpl = display.newText(scetcikBalpl, 180, 3, "Lato-Regular.ttf", 30 )
vrem = display.newText("20", 290, 3, "Lato-Regular.ttf", 30 )
sceneGroup:insert(bal)
sceneGroup:insert(balpl)
sceneGroup:insert(yes)
sceneGroup:insert(no)
sceneGroup:insert(vrem)
sceneGroup:insert(casi)
sceneGroup:insert(tapText)
sceneGroup:insert(button1)
sceneGroup:insert(button2)
sceneGroup:insert(button3)
sceneGroup:insert(button4)
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif (phase == "did") then
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif (phase == "did") then
composer.removeScene( "level1" )
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return sceneenter code here
C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207: attempt to call method 'setFillColor' (a nil value)
In line 207 of level1.lua you do this setFillColor() which is not allowed as setFillColor is not a function value but a nil value.
setFillColor is a method of the ShapeObject class. Make sure you're doing using it like this: myShapeObject:setFillColor(myColor)

LUA : Scene not being created. What is wrong?

I'm building a game where I have a start screen and clicking the start button it should take me to the FIRST level. Some stuff happens and then I will be taken to the SECOND level.
However I'm running into problems and can't seem to find a solution.
When I click the start button it seems to get stuck in the create function because i prints out something. I was told you don't have to place anything in the create function and just put everything in the SHOW FUNCTION. Was i mislead?
START SCENE
local composer = require("composer")
local widget = require("widget")
local options = {effect = "fade", time = 800}
local startBtn;
local function start(event)
-- load first scene
composer.gotoScene( "level1", options);
startBtn:removeSelf();
print("Start Game")
end
startBtn = widget.newButton(
{
left = 75,
top = 100,
id = "startBtn",
label = "Start",
onEvent = start
}
)
When I click the START button it should take me to the FIRST LEVEL which is here
and is where i'm running into problems.
local composer = require( "composer" );
local scene = composer.newScene();
local widget = require ("widget");
function scene:create( event )
local sceneGroup = self.view;
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
local params = event.params;
print(params);
if (phase == "will") then
print("Will")
elseif (phase == "did") then
print("Did")
local bg = display.newImage ("bg.png",
display.contentCenterX, display.contentCenterY);
------- ALEX KIDD ---------------------------------
local options =
{
frames = {
{ x = 1, y = 2, width = 16, height = 25}, --frame 1
{ x = 18, y = 2, width = 16, height = 25}, --frame 2
{ x = 35, y = 2, width = 16, height = 25}, --frame 3
{ x = 52, y = 2, width = 16, height = 25}, --frame 4
{ x = 1, y = 54, width = 16, height = 24}, --ready1
{ x = 19, y = 54, width = 16, height = 24}, --ready2
{ x = 37, y = 54, width = 29, height = 24}, -- rock
{ x = 67, y = 54, width = 33, height = 24}, -- scissor
{ x = 101, y = 54, width = 33, height = 24}, -- paper
}
};
local sheet = graphics.newImageSheet( "kidd.png", options );
-- Create animation sequence for animation
local seqData = {
{name = "normal", start=1 , count = 4, time = 800},
{name = "faster", frames={1,2,3,4}, time = 400},
{name = "shake", frames={5,6}, time = 500},
{name = "rock", frames={7}},
{name = "paper", frames={9}},
{name = "scissor", frames={8}},
}
local alex = display.newSprite (sheet, seqData);
alex.x = display.contentCenterX-100;
alex.y = display.contentCenterY+83;
alex.anchorX = 1;
alex.anchorY = 1;
---------- JANKEN ---------------------------------
local jankenOpt =
{
frames = {
{x= 154, y= 13, width= 39, height= 48 }, -- shake1
{x= 195, y= 13, width= 39, height= 48 }, -- shake2
{x= 236, y= 13, width= 32, height= 48 }, -- set
{x= 270, y= 13, width= 16, height= 48 }, --r/p/s
{x= 287, y= 13, width= 16, height= 48 }, --r/p/s
{x= 305, y= 13, width= 15, height= 48 }, --r/p/s
{x= 69, y= 13, width= 41, height= 48 }, --flap1
{x= 110, y= 13, width= 40, height= 48 }, --flap2
}
};
local jankenSheet = graphics.newImageSheet( "chars.png", jankenOpt );
-- Create animation sequence janken
local seqDataJanken = {
{name = "flap", frames={7,8}, time = 500},
{name = "shake", frames={1,2}, time = 500},
{name = "set", frames={3}, time = 10, loopCount=1},
}
local janken = display.newSprite (jankenSheet, seqDataJanken);
janken.x = display.contentCenterX+100;
janken.y = display.contentCenterY+83;
janken.anchorX = 1;
janken.anchorY = 1;
-------------- button setup
local btnOpt =
{
frames = {
{ x = 3, y = 2, width=70, height = 22}, --frame 1
{ x = 78, y = 2, width=70, height = 22}, --frame 2
}
};
local buttonSheet = graphics.newImageSheet( "button.png", btnOpt );
local function foo (wow)
alex:play();
janken:play();
end
local function bar (wow)
alex:pause();
janken:pause();
--alex:setFrame(2); --keep it at this frame
-- NEXT: generate more buttons
end
-- Function to handle button event
local function go( event )
-- event.target is the button widget
print (event.phase, event.target.id)
transition.to(alex, {time=2000, x=170, onStart = foo, onComplete = bar})
end
local btnGo = widget.newButton(
{
x = 200,
y = 20,
id = "btnGo",
label = "Go!",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = go,
}
);
-------------- play buttons
local function shoot (buttonID)
local randomHand = math.random(4,6);
-- position Janken and draw his hands
janken:setSequence("set");
hand = display.newImage (jankenSheet, randomHand, display.contentCenterX+61, display.contentCenterY+60);
if (buttonID == "btnRock") then
alex:setSequence("rock"); -- just show rock for now
elseif (buttonID == "btnScissor") then
alex:setSequence("scissor"); -- just show rock for now
else
alex:setSequence("paper"); -- just show rock for now
end
end
local function play (event)
if (event.phase == "ended") then
alex:setSequence ("shake");
alex:play();
janken:setSequence("shake");
janken:play();
local t = timer.performWithDelay (1500, function() shoot(event.target.id) end, 1);
print (event.target.id); -- which button was it?
end
end
local btnRock = widget.newButton(
{
x = 80,
y = 20,
id = "btnRock",
label = "Rock",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnPaper = widget.newButton(
{
x = 80,
y = 50,
id = "btnPaper",
label = "Paper",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnScissor = widget.newButton(
{
x = 80,
y = 80,
id = "btnScissor",
label = "Scissor",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local scoreAlex = display.newText ( {text="Alex: 0", x=230, y=60, fontSize=20});
scoreAlex:setFillColor (0,0,0);
scoreAlex.anchorX = 1;
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
transition.cancel(enemy);
elseif ( phase == "did" ) then
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
scene:addEventListener( "hide", scene )
return scene
Well, you are adding a listener scene:addEventListener( "enter", scene ) which should be scene:addEventListener( "show", scene ) (Change enter to show)
Your listener is not even being fired, because it was not added with the correct name.
Composer uses the following events: create, destroy, show, hide.

attempt to call global 'addButtons' (a nil value)

I need help with a really annoying error I've been getting with my GMOD gamemode. I am trying to make a custom menu, but I get this error:
[ERROR] gamemodes/tdm/gamemode/custom_menu.lua:20: attempt to call global 'addButtons' (a nil value)
1. gameMenu - gamemodes/tdm/gamemode/custom_menu.lua:20
2. unknown - gamemodes/tdm/gamemode/custom_menu.lua:32
3. include - [C]:-1
4. unknown - gamemodes/tdm/gamemode/cl_init.lua:3
Here is my code:
custom_menu.lua
local Menu
function gameMenu()
if(Menu == nil) then
Menu = vgui.Create("DFrame")
Menu:SetSize(750, 500)
Menu:SetPos(ScrW() / 2 - 325, ScrH() / 2 - 250)
Menu:SetTitle("Gamemode Menu")
Menu:SetDraggable(true)
Menu:ShowCloseButton(false)
Menu:SetDeleteOnClose(false)
Menu.Paint = function()
surface.SetDrawColor(60, 60, 60, 255)
surface.DrawRect(0, 0, Menu:GetWide(), Menu:GetTall())
surface.SetDrawColor(40, 40, 40, 255)
surface.DrawRect(0, 24, Menu:GetWide(), 1)
end
addButtons(Menu)
gui.EnableScreenClicker(true)
else
if(Menu:IsVisible()) then
Menu:SetVisible(false)
gui.EnableScreenClicker(false)
else
Menu:SetVisible(true)
gui.EnableScreenClicker(true)
end
end
end
concommand.Add("open_game_menu", gameMenu())
function addButtons(Menu)
local playerButton = vgui.Create("DButton")
playerButton:SetParent(Menu)
playerButton:SetText("")
playerButton:SetSize(100, 50)
playerButton:SetPos(0, 25)
playerButton.Paint = function()
--Color of entire button
surface.SetDrawColor(50, 50, 50, 255)
surface.DrawRect(0, 0, playerButton:GetWide(), playerButton:GetTall())
--Draw Bottom and Right borders
surface.SetDrawColor(40, 40, 40, 255)
surface.DrawRect(0, 49, playerButton:GetWide(), 1)
surface.DrawRect(99, 0, 1, playerButton:GetTall())
--Draw Text
draw.DrawText("Player", "DermaDefaultBold", playerButton:GetWide() / 2, 17, Color(255, 255, 255, 255), 1)
end
playerButton.DoClick = function(playerButton)
local playerPanel = Menu:Add("PlayerPanel")
end
local shopButton = vgui.Create("DButton")
shopButton:SetParent(Menu)
shopButton:SetText("")
shopButton:SetSize(100, 50)
shopButton:SetPos(0, 75)
shopButton.Paint = function()
--Color of entire button
surface.SetDrawColor(50, 50, 50, 255)
surface.DrawRect(0, 0, shopButton:GetWide(), shopButton:GetTall())
--Draw Bottom and Right borders
surface.SetDrawColor(40, 40, 40, 255)
surface.DrawRect(0, 49, shopButton:GetWide(), 1)
surface.DrawRect(99, 0, 1, shopButton:GetTall())
--Draw Text
draw.DrawText("Shop", "DermaDefaultBold", shopButton:GetWide() / 2, 17, Color(255, 255, 255, 255), 1)
end
shopButton.DoClick = function(shopButton)
local shopPanel = Menu:Add("ShopPanel")
end
end
--Player Panel
PANEL = {} -- Create an empty panel
function PANEL:Init() --Initialize the panel
self:SetSize(650, 475)
self:SetPos(100, 25)
end
function PANEL:Paint(w, h)
draw.RoundedBox(0, 0, 0, w, h, Color(0, 0, 0, 255))
end
vgui.Register("PlayerPanel", PANEL, "Panel")
--End Player Panel
--Shop Panel
PANEL = {} -- Create an empty panel
function PANEL:Init() --Initialize the panel
self:SetSize(650, 475)
self:SetPos(100, 25)
end
function PANEL:Paint(w, h)
draw.RoundedBox(0, 0, 0, w, h, Color(255, 255, 255, 255))
end
vgui.Register("ShopPanel", PANEL, "Panel")
--End Shop Panel
init.lua:
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "testhud.lua" )
local open = false
include ( 'shared.lua' )
local open = false
function GM:PlayerInitialSpawn(ply)
if(ply:GetPData("playerLvl") == nil) then
ply:SetNWInt("playerLvl", 1)
else
ply:SetNWInt("playerLvl", ply:GetPData("playerLvl"))
end
if(ply:GetPData("playerExp") == nil) then
ply:SetNWInt("playerExp", 0)
else
ply:SetNWInt("playerExp", ply:GetPData("playerExp"))
end
if(ply:GetPData("playerMoney") == nil) then
ply:SetNWInt("playerMoney", 0)
else
ply:SetNWInt("playerMoney", ply:GetPData("playerMoney"))
end
end
function GM:OnNPCKilled(npc, attacker, inflictor)
attacker:SetNWInt("playerMoney", attacker:GetNWInt("playerMoney") + 100)
attacker:SetNWInt("playerExp", attacker:GetNWInt("playerExp") + 100)
checkForLevel(attacker)
end
function GM:PlayerDeath(victim, inflictor, attacker)
attacker:SetNWInt("playerMoney", attacker:GetNWInt("playerMoney") + 100)
attacker:SetNWInt("playerExp", attacker:GetNWInt("playerExp") + 100)
checkForLevel(attacker)
end
function GM:PlayerLoadout(ply)
ply:Give("m9k_l85")
ply:Give("m9k_colt1911")
ply:Give("m9k_knife")
ply:Give("m9k_m61_frag")
ply:GiveAmmo(500, "ar2", true)
ply:GiveAmmo(100, "pistol", true)
return true
end
function checkForLevel(ply)
local expToLevel = (ply:GetNWInt("playerLvl") * 100) * 2
local curExp = ply:GetNWInt("playerExp")
local curLvl = ply:GetNWInt("playerLvl")
if(curExp >= expToLevel) then
curExp = curExp - expToLevel
ply:SetNWInt("playerExp", curExp)
ply:SetNWInt("playerLvl", curLvl + 1)
end
end
function GM:PlayerDisconnected(ply)
ply:SetPData("playerExp", ply:GetNWInt("playerExp"))
ply:SetPData("playerLvl", ply:GetNWInt("playerLvl"))
ply:SetPData("playerMoney", ply:GetNWInt("playerMoney"))
end
function GM:ShutDown()
for k, v in pairs(player.GetAll()) do
v:SetPData("playerExp", v:GetNWInt("playerExp"))
v:SetPData("playerLvl", v:GetNWInt("playerLvl"))
v:SetPData("playerMoney", v:GetNWInt("playerMoney"))
end
end
function GM:ShowSpare2(ply)
ply:ConCommand("open_game_menu")
end
Thanks!
-Graham
You call
concommand.Add("open_game_menu", gameMenu())
which calls addButtons(Menu) which is nil at this point as the function definition follows after concommand.Add("open_game_menu", gameMenu()).
It's all there. You just have to read the error message.
Change the order in your script to fix the error.

How to insert rows into TableView in Corona?

I'm trying to render the match results (string.find) in to a Row, with kinda works... but its only displaying the last match. so if I match 'jan' and 'kevin' it will; only list 'kevin'
Is there a way to fix this ?
code:
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
local name = display.newText(row, input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
local tableView = widget.newTableView
{
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
for i = 1, 1 do
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
onRender = onRowRender,
}
end
end
end
Your tableView being declared within the outer loop meant that the first instance of it would be transitioned over by the second instance. And a quick look over the docs indicates that each event.row allows an optional params table to include any data you may need to render the row.
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
-- ** moved from loop **
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
-- ** changed to use params table **
local name = display.newText(row, row.params.input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
-- ** moved from loop **
local tableView = widget.newTableView {
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
-- ** pass input1 to onRowRender **
params = { input1 = input1 }
}
end
end

How do you Parse this String in Lua "1 2 3 4"?

I am brand new to Lua from a pure VB.Net background. I have a large file of maplines that I need to iterate through. The data is in the form of:
;XX.XXX YY.YYY Name
;
[Airways]
58.50 -12.44 58.21 -13.73
58.21 -13.73 57.89 -15.02
57.89 -15.02 57.54 -16.30
57.54 -16.30 57.17 -17.58
57.17 -17.58 56.76 -18.84
56.76 -18.84 56.33 -20.10
56.33 -20.10 55.87 -21.35
54.33 -25.02 53.77 -26.22
I have tried this but keep getting an error.
local mapLines = {}
local filePath = system.pathForFile( "data.ini", system.DocumentsDirectory )
local file = io.open( filePath, "r" )
if file then
local contents = file:read( "*a" )
--print( "Contents of " .. filePath )
--print( contents )
io.close( file )
local t = display.newText( "Contents of ", 5, 80, nil, 16 );
t:setTextColor( 255, 255, 136, 255 );
local t = display.newText( filePath, 5, 100, nil, 10 );
t:setTextColor( 255, 255, 136, 255 );
local ylast = 130
for line in io.lines(filePath) do
local t = display.newText( line, 15, ylast, nil, 14 );
t:setTextColor( 255, 255, 255 );
ylast = ylast + 20
n = tonumber(line);
if n == nil then
local f = {}
s = "1 2 3 4"
for k, v in string.gmatch(s, "(%w+) (%w+)") do
f[k] = v
end
local myLine = newLine(tonumber(f[1]), tonumber(f[2]), tonumber(f[3]), tonumber(f[4]))
table.insert( mapLines, myLine )
end
end
end
-- Example of shape drawing func
local function newLine(x,y,x1,y1)
-- need initial segment to start
local Line = display.newLine( x, y, x1, y1 )
Line:setColor( 30, 155, 30, 100 )
Line.width = 3
return Line
end
Runtime:addEventListener( "enterFrame", mapLines )
Any help would be greatly appreciated!
Dave
answering the question in the topic:
local string_to_parse = '1 2 3 4'
for s in string_to_parse:gmatch('%d+') do
print(s)
end
sample code on codepad

Resources