Page 1 of 1
Accessing Information without if then statements
Posted: Wed Apr 15, 2015 11:54 pm
by Cookie10monster
I know the title is confusing and I'm sorry if this is in the wrong place in the forum, but I need help with a problem I've been having. I am making a 2d voxel game similar to terraria(not a clone) and want to make it so that different blocks take different amounts of time to mine, and take different times with different tools. The only way I can think of doing this is making a lot of if Block == Grass then TimeToMineBlock = 10 (not actual code) statements. I could use some help with this, thanks!
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 1:43 am
by arampl
Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block] (if Block == "Grass" then TimeToMineBlock[Block] = 10 automatically).
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 6:19 am
by ivan
arampl wrote:Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block]
Yep, that's a nice solution.
Although if you want to have different tools you can do:
Code: Select all
tools = {}
tools.Shovel = { Grass = 10, Ground = 5 }
tools.PickAxe = { Grass = 3, Ground = 2 }
local timeToMineBlock = tools[currentTool][typeOfTile]
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 10:38 am
by Cookie10monster
ivan wrote:arampl wrote:Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block]
Yep, that's a nice solution.
Although if you want to have different tools you can do:
Code: Select all
tools = {}
tools.Shovel = { Grass = 10, Ground = 5 }
tools.PickAxe = { Grass = 3, Ground = 2 }
local timeToMineBlock = tools[currentTool][typeOfTile]
Thanks for the idea, but I keep getting errors whenever the code is run. I have tried making a new program and doing this and I still get this error:
Error
main.lua:467: attempt to index a nil value
Traceback
main.lua:467: in function 'update'
[C]: in function 'xpcall'
This is the line where I am getting the error
if BreakBlockCounter == tools[Pickaxe][grass] then
and here is where I make the table
tools = {}
tools.Pickaxe = {grass = 10, dirt = 10, stone = 15}
I think I am misunderstanding something, help is appreciated
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 11:26 am
by Robin
You mistake strings for variables.
table.entry is the same as table["entry"],
not table[entry].
So you'll want to use:
Code: Select all
if BreakBlockCounter == tools["Pickaxe"]["grass"] then
or in the actual game, use variables like ivan said:
Code: Select all
if BreakBlockCounter == tools[currentTool][typeOfTile] then
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 9:51 pm
by Cookie10monster
Robin wrote:You mistake strings for variables.
table.entry is the same as table["entry"],
not table[entry].
So you'll want to use:
Code: Select all
if BreakBlockCounter == tools["Pickaxe"]["grass"] then
or in the actual game, use variables like ivan said:
Code: Select all
if BreakBlockCounter == tools[currentTool][typeOfTile] then
This helped me with setting the time taken for a sepcific block (eg. "grass"), but when I access the table where what kind of block is being stored, It doesnt do anything. It doesnt crash or give me an error, and everything else is working fine. (The counter for breaking them is still going up) I think that the values in the table(like grass or stone) aren't the right kind of information. Do i need to insert them as "grass" or "stone"? Sorry for any trouble I have caused and thanks!
Re: Accessing Information without if then statements
Posted: Thu Apr 16, 2015 10:39 pm
by Duster
Cookie10monster wrote:Robin wrote:You mistake strings for variables.
table.entry is the same as table["entry"],
not table[entry].
So you'll want to use:
Code: Select all
if BreakBlockCounter == tools["Pickaxe"]["grass"] then
or in the actual game, use variables like ivan said:
Code: Select all
if BreakBlockCounter == tools[currentTool][typeOfTile] then
This helped me with setting the time taken for a sepcific block (eg. "grass"), but when I access the table where what kind of block is being stored, It doesnt do anything. It doesnt crash or give me an error, and everything else is working fine. (The counter for breaking them is still going up) I think that the values in the table(like grass or stone) aren't the right kind of information. Do i need to insert them as "grass" or "stone"? Sorry for any trouble I have caused and thanks!
I don't know if I'm reading you right, so bear with me if I'm misunderstanding
Your values should be the length of time/number of hits/whatever it takes to break a given block (number). The
key for each value needs to be a string containing the name of the block, or else accessing a table in that way won't work.
Ex.
Code: Select all
tools = {
"Pickaxe" = {"grass"=2, "stone"=5, etc},
}
Re: Accessing Information without if then statements
Posted: Fri Apr 17, 2015 8:05 am
by Robin
Duster, you mean
Code: Select all
tools = {
Pickaxe = {grass=2, stone=5, etc},
}
or
Code: Select all
tools = {
["Pickaxe"] = {["grass"]=2, ["stone"]=5, etc},
}
Re: Accessing Information without if then statements
Posted: Fri Apr 17, 2015 11:26 am
by s-ol
OP (and anyone else having trouble with tables) should definetely go read PIL (again), at least the first few chapters.
You might get your problem(s) fixed with help from the community and trial and error, but a thorough understanding of tables is crucial for programming anything moderately complex in lua (and therefore löve).
Re: Accessing Information without if then statements
Posted: Tue Apr 28, 2015 3:20 pm
by Cookie10monster
Thank you for everybody who helped! I have solved the problem and it is working well now. Thanks again!