Accessing Information without if then statements
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Accessing Information without if then statements
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
Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block] (if Block == "Grass" then TimeToMineBlock[Block] = 10 automatically).
Then you can use it like TimeToMineBlock[Block] (if Block == "Grass" then TimeToMineBlock[Block] = 10 automatically).
Re: Accessing Information without if then statements
Yep, that's a nice solution.arampl wrote:Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block]
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]
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: Accessing Information without if then statements
ivan wrote:Yep, that's a nice solution.arampl wrote:Just make table TimeToMineBlock = {Grass = 10, Ground = ... , etc.}
Then you can use it like TimeToMineBlock[Block]
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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Accessing Information without if then statements
You mistake strings for variables.
table.entry is the same as table["entry"], not table[entry].
So you'll want to use:
or in the actual game, use variables like ivan said:
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
Code: Select all
if BreakBlockCounter == tools[currentTool][typeOfTile] then
Help us help you: attach a .love.
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: Accessing Information without if then statements
Robin wrote:You mistake strings for variables.
table.entry is the same as table["entry"], not table[entry].
So you'll want to use:or in the actual game, use variables like ivan said:Code: Select all
if BreakBlockCounter == tools["Pickaxe"]["grass"] then
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
I don't know if I'm reading you right, so bear with me if I'm misunderstandingCookie10monster 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:or in the actual game, use variables like ivan said:Code: Select all
if BreakBlockCounter == tools["Pickaxe"]["grass"] then
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!
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},
}
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Accessing Information without if then statements
Duster, you mean
or
Code: Select all
tools = {
Pickaxe = {grass=2, stone=5, etc},
}
Code: Select all
tools = {
["Pickaxe"] = {["grass"]=2, ["stone"]=5, etc},
}
Help us help you: attach a .love.
Re: Accessing Information without if then statements
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).
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).
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: Accessing Information without if then statements
Thank you for everybody who helped! I have solved the problem and it is working well now. Thanks again!
Who is online
Users browsing this forum: GetAsync and 3 guests