View Poll Results: Is anyone is really good in lua language?
Need help in function addShip 0 0%
place ship randomly to the screen 0 0%
Multiple Choice Poll. Voters: 0. You may not vote on this poll

Thread Tools Display Modes
11-30-14, 05:57 PM   #1
analytical
A Defias Bandit
Join Date: Nov 2014
Posts: 3
lua language

Hi!

I am trying to create a function with a single argument. Then this function has to pick random square from the grid to start building the ship. There is no error, I don't know what's wrong. function addship can't build any ship...

gridWidth = 9
gridHeight = 14

grid = {}

for i=1, gridWidth, 1 do
grid[i] = {}
end


function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(255, 0, 0)

else
event.target.ship = false
event.target:setFillColor(0,255, 0)

end
end






squareWidth = 25
squareHeight = 25
gap = 5


for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do

grid[i][j] = display.newRect((gap* i) + (i*squareWidth) , (gap* j) + (j* squareHeight), squareWidth, squareHeight)

end
end





for i =1, gridWidth, 1 do
for j =1, gridHeight, 1 do
grid[i][j].ship = false

grid[i][j]:addEventListener("tap", tappedRect)


end
end



function addShip(shipSize)
x = math.random(1, gridWidth)
y = math.random(1, gridHeight)

for i = 1, shipSize-1, 1 do
if grid[x-1][y].ship then
grid[x-i][y]:setFillColor(255,0,0)


end
end
end



addShip(2)





Please Help!
Attached Files
File Type: lua main.lua (1.5 KB, 172 views)
 
11-30-14, 10:41 PM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
This is a forum for World of Warcraft AddOns. I don't even know what this is that you just posted, but it isn't that.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
 
12-02-14, 10:35 AM   #3
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
As you initialize your grid with
Lua Code:
  1. grid[i][j].ship = false
I would guess that
Lua Code:
  1. if grid[x-1][y].ship then
is true for all grid elements.

Try
Lua Code:
  1. if grid[x-1][y].ship == false then
 
12-02-14, 10:55 AM   #4
AlleyKat
A Warpwood Thunder Caller
 
AlleyKat's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 93
First, to build a ship, you need some timber

Last edited by AlleyKat : 12-02-14 at 10:59 AM.
 
12-02-14, 11:13 AM   #5
sirann
A Flamescale Wyrmkin
Join Date: Mar 2007
Posts: 142
First I had to clean up your code lol:
Lua Code:
  1. gridWidth = 9
  2. gridHeight = 14
  3. grid = {}
  4.  
  5. for i=1, gridWidth, 1 do   
  6.     grid[i] = {}
  7. end
  8.  
  9. function tappedRect(event)
  10.     if event.target.ship then  
  11.         event.target.ship = true   
  12.         event.target:setFillColor(255, 0, 0)   
  13.     else
  14.         event.target.ship = false
  15.         event.target:setFillColor(0,255, 0)
  16.     end
  17. end
  18.  
  19. squareWidth = 25   
  20. squareHeight = 25  
  21. gap = 5
  22.  
  23. for i =1, gridWidth, 1 do  
  24.     for j =1, gridHeight, 1 do
  25.         grid[i][j] = display.newRect((gap* i) + (i*squareWidth) , (gap* j) + (j* squareHeight), squareWidth, squareHeight)
  26.     end
  27. end
  28.  
  29. for i =1, gridWidth, 1 do  
  30.     for j =1, gridHeight, 1 do
  31.         grid[i][j].ship = false
  32.         grid[i][j]:addEventListener("tap", tappedRect) 
  33.     end
  34. end
  35.  
  36. function addShip(shipSize)
  37.     x = math.random(1, gridWidth)
  38.     y = math.random(1, gridHeight)
  39.     for i = 1, shipSize-1, 1 do
  40.         if grid[x-1][y].ship then
  41.             grid[x-i][y]:setFillColor(255,0,0)
  42.         end
  43.     end
  44. end
  45.  
  46. addShip(2)

Then I saw
Lua Code:
  1. for i=1, gridWidth, 1 do    
  2.     grid[i] = {}
  3. end
which I'm pretty sure creates nested tables at keys of 1-9-1, which is a bit redundant. Should just be
Lua Code:
  1. for i = 1, gridWidth do

Then you try to perform an if check that will always be false as you never declared the event table:
Lua Code:
  1. if event.target.ship then  
  2.         event.target.ship = true    
  3.         event.target:setFillColor(255, 0, 0)    
  4.     else
  5.         event.target.ship = false
  6.         event.target:setFillColor(0,255, 0)
  7.     end

More redundancy of for loops over writing table values
Lua Code:
  1. for i =1, gridWidth, 1 do  
  2.     for j =1, gridHeight, 1 do

So, quite a bit of work (more than I posted) needs to be done to make this functional!
 
12-02-14, 02:24 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by sirann View Post
Then you try to perform an if check that will always be false as you never declared the event table
Trying to index a table that isn't declared (value is nil) should throw an error regardless of it being in an if...then statement.

Without any clue as to the API being used, there could be many more errors than we can see because none of it has anything to do with the WoW API.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
 
12-02-14, 10:38 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by sirann View Post
Then I saw
Lua Code:
  1. for i=1, gridWidth, 1 do    
  2.     grid[i] = {}
  3. end
which I'm pretty sure creates nested tables at keys of 1-9-1, which is a bit redundant. Should just be
Lua Code:
  1. for i = 1, gridWidth do
While it is redundant, it's not for the reason you think -- the third parameter in a for loop just specifies how much to increment i on each loop. 1 is the default value, so you do not need to explicitly specify 1. However, if you wanted to iterate through all even numbers from 4 to 9000, for example, you could do this:
Code:
for i = 4, 9000, 2 do ... end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
 
12-04-14, 11:25 AM   #8
analytical
A Defias Bandit
Join Date: Nov 2014
Posts: 3
lua language

Thank you all guys...

This function (addShip) need to decide randomly whether it will be horizontal or vertical rectangles. I am trying to change some rectangles from (ship) attributes to true in a line in order to add a ship. For example, if I try to call a ship of size 2 it will be 2 rectangles in a row or column with a ship attribute value of true.
This is where my last function is not doing anything...



local gridWidth = 9
local gridHeight = 14
local grid = {}


for i = 1, gridWidth do
grid[i] = {}
end

Touch function
local function tappedRect(event)
if event.target.ship then
event.target.ship = true
event.target:setFillColor(255,0,0)
else
event.target.ship = false
event.target:setFillColor(0,255,0)
end
end



local squareWidth = 25
local squareHeight = 25
local gap = 5


for i = 1, gridWidth do
for j = 1, gridHeight do


local xPos = (gap*i) + (i*squareWidth)
local yPos = (gap*j) + (j*squareHeight)
grid[i][j] = display.newRect(xPos, yPos, squareWidth, squareHeight)
grid[i][j]:addEventListener("tap", tappedRect)

end
end

--Add a ship
local function addShip( shipSize )

-- Set a random square location
local xPos = math.random(1, gridWidth)
local yPos = math.random(1, gridHeight)

for i = 1, shipSize-1, 1 do
if grid[x-i][y].ship then
grid[x-i][y]:setFillColor(255,0,0)


end
end
end



addShip(2)

If anyone has any idea please let me know

Thank you all...
 
12-04-14, 08:18 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
1. Please use [code] tags around your code. Otherwise it becomes an unformatted mess and is difficult to read.

2. This site is for World of Warcraft UI addons. There is no "addShip" function or "display.newRect" function in the WoW API, nor in Lua itself, so any help you get here will be general Lua syntax help only, especially when you have not even specified what API you're working with. What program will your script be running in? Is there not a forum specific for that API you can post on where people might actually know what you are talking about?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
 
12-04-14, 08:52 PM   #10
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
I bet a grand he is a freshman in some college and this is his homework.
 
12-04-14, 11:46 PM   #11
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Could be the code academy tutorial for Lua which does walk you throgh making a battleship game in python with a lua config.
__________________
Tweets YouTube Website
 
12-05-14, 02:48 AM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, in any case, (s)he is still posting on the wrong site, and is still not explaining clearly what they are actually doing and why they're asking for help with it on a site for WoW addons.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
 
12-05-14, 10:56 AM   #13
Gethe
RealUI Developer
 
Gethe's Avatar
Premium Member
Featured
Join Date: Sep 2008
Posts: 942
It looks like it's for Love. If that's the case, you'd be better off going there.
__________________
Knowledge = Power; Be OP

 
12-05-14, 11:07 AM   #14
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
I bet: a Corona mobile app.
 
12-05-14, 11:07 AM   #15
Cairenn
Credendo Vides
 
Cairenn's Avatar
Premium Member
WoWInterface Admin
Join Date: Mar 2004
Posts: 7,134
Yeah, no, none of that is at all related to WoW AddOns, as others have already said. You might want to look into sites that are directly related to whatever it is you are working on. Good luck!
__________________
“Do what you feel in your heart to be right — for you’ll be criticized anyway.” ~ Eleanor Roosevelt
~~~~~~~~~~~~~~~~~~~
Co-Founder & Admin: MMOUI
FaceBook Profile, Page, Group
Avatar Image by RaffaeleMarinetti
 

WoWInterface » Developer Discussions » Lua/XML Help » lua language

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off