Accessing Information without if then statements

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Accessing Information without if then statements

Post 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!
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Accessing Information without if then statements

Post 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).
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Accessing Information without if then statements

Post 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]
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: Accessing Information without if then statements

Post 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 :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Accessing Information without if then statements

Post 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
Help us help you: attach a .love.
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: Accessing Information without if then statements

Post 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!
User avatar
Duster
Prole
Posts: 32
Joined: Fri Dec 19, 2014 8:34 pm
Location: Ontario, Canada

Re: Accessing Information without if then statements

Post 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},
}
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Accessing Information without if then statements

Post 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},
}
Help us help you: attach a .love.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Accessing Information without if then statements

Post 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).

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: Accessing Information without if then statements

Post by Cookie10monster »

Thank you for everybody who helped! I have solved the problem and it is working well now. Thanks again!
Post Reply

Who is online

Users browsing this forum: No registered users and 138 guests