Simple question about tables

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.
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Simple question about tables

Post by Imaculata »

I couldn't find any good documentation on tables in lua, and my problem is a very simple one. I'm coding a simple particle system for my game that spawns particle emitters on the fly. These particle systems are all predefined in a table, and I want to be able to call upon them, so that I can add them to a list of active emitters.

Currently this looks something like this:

Code: Select all

function setupEmitters()

	--Shark wake splash effect
	emittertypes.emitter_splash = {

	name = "splash",
	image = love.graphics.newImage("sprites/effect_splash.png"),
	imagesize = 100,
	frames = 5,
	looping = true,
	spawnrate = 0.1,
	rotspeed = 5,
	emitterlife = 5,
	limitedlife = false,
	particlelife = 5,
	scale = 1,
	startscale = 0.1,
	endscale = 1.0,
	startscalespeed = 0.1,
	endscalespeed = 0,
	sound = nil
	
	} 
	
	--Next particle effect here
end
This created the emitter for a water splash effect. I then want to be able to spawn this emitter from another script like this:

Code: Select all

createEmitter(emitter_splash, false, player1.X, player1.Y, 0, 0)
This way I only have to pass the name of the effect, along with it's location and some other data to the function that spawns the emitter. But here's my problem, I do not know what the correct syntax is to get the emitter named "emitter_splash" from the list of emitters called "emittertypes". Obviously there are many ways to find the correct data point in a table. But I don't want to check every name of every entry in the list to find the correct particle emitter. I just want the game to instantly get "emittertypes.emitter_splash"

I tried doing something like:

Code: Select all

local currentemitter = emittertypes.emittype --Where emittype is emitter_splash
but this does not work, because I'm sending a nil value. How can I pass on the correct name in the table from an external script?

What do I need to add after...

Code: Select all

local currentemitter = emittertypes.
...to get the correct value from the table? Because I want to be able to also get other emitters from the list, and not just emittertypes.emitter_splash.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Simple question about tables

Post by bartbes »

As the lua documentation mentions, in section 2.3, table indexing uses square brackets. So you can do sometable[1], sometable[2], but also sometable["name"]. It also mentions that there is a shorthand syntax, using a dot, which corresponds to the latter, so player1.X is equivalent to player1["X"], and emittertypes.emitter_splash is equivalent to emittertypes["emitter_splash"]. As you may have noticed, between the brackets is a value, not just a literal string, this suggests that you might be able to use variables instead. And you can! So if emittype is set to "emitter_splash", then emittertypes[emittype] returns emittertypes.emitter_splash.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Simple question about tables

Post by s-ol »

Imaculata wrote:I couldn't find any good documentation on tables in lua
tables pretty much make up two thirds of all Lua documentation and everything there is to know about the langauge itself is covered in PiL to some depth. Of course there are other sources of documentation that cover topics in more depth or differently that can help too, but generally you should be looking at PiL first for general topics. Also it is very strongly recommended to read PiL fully.

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
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Re: Simple question about tables

Post by Imaculata »

Thanks Bartbes! I knew it had to be something simple. I didn't realize that I could just substitute emittertypes[emitter_splash] for emittertypes["emitter_splash"], and that it would simply pick up the name that I had included in the table. That makes things a lot easier. It's working now. I'll post a preview once I've coded the particles themselves, because now that I have the engine for all sorts of mayhem, it is time to turn all this data into visuals.

BTW, I had read that documentation you mentioned. But it is not written very clearly. I find it very difficult to comprehend what they are saying in that text.
s-ol wrote: tables pretty much make up two thirds of all Lua documentation and everything there is to know about the langauge itself is covered in PiL to some depth. Of course there are other sources of documentation that cover topics in more depth or differently that can help too, but generally you should be looking at PiL first for general topics. Also it is very strongly recommended to read PiL fully.
What is PiL?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Simple question about tables

Post by raidho36 »

Programming in Lua. THE book about Lua. I would argue though that it has everything to know about Lua. Like it doesn't goes into any substantial detail about language quirks, such as table allocation algorithm and how it impacts length operator and generic for ipairs iterator. Plus, LÖVE uses LuaJIT that has its own quirks on top of that - and it barely has any documentation to speak of, let alone books written about it.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Simple question about tables

Post by zorg »

Imaculata wrote:I didn't realize that I could just substitute emittertypes[emitter_splash] for emittertypes["emitter_splash"], and that it would simply pick up the name that I had included in the table.
You mean [emittype], instead of [emitter_splash], no?
bartbes wrote:emittertypes.emitter_splash is equivalent to emittertypes["emitter_splash"]
This just means that for string keys/indices/indexes, you can use the dot notation, a syntax sugar.
bartbes wrote:So if emittype is set to "emitter_splash", then emittertypes[emittype] returns emittertypes.emitter_splash.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Re: Simple question about tables

Post by Imaculata »

zorg wrote:You mean [emittype], instead of [emitter_splash], no?
Well, to clarify, what I was doing was sending the value emitter_splash to another script, which then used that value within emittype.

In other words, emittype is a local value that contains emitter_splash, which was nil, since I called it from an external script.

I had created the particle emitter called emitter_splash locally in a script where I define all my particle emitters. So if I'm asking for

Code: Select all

emittertypes[emitter_splash]
, that is no different as asking for

Code: Select all

emittertypes[nil]
, since it doesn't know what emitter_splash is, since I declared it locally. But if I ask for emittertypes["emitter_splash"], it finds the entry named "emitter_splash". I did not know I could do that. So thanks again for this clarification.
bartbes wrote:This just means that for string keys/indices/indexes, you can use the dot notation, a syntax sugar.
I was already aware of the dot notation, and prefer to use it whenever I can. But I was unsure how I could use the dot notation to ask for a specific local value. It's a syntax thing. I don't know all the syntax rules yet, and will occasionally stumble into a situation where I'm unsure how to connect two statements into one command.

But it's working now. So again, thanks for the quick response. This forum is incredibly useful.
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Re: Simple question about tables

Post by Imaculata »

So, I now use this script to copy a particle emitter template into a list of active emitters.

Code: Select all

function createEmitter(emittype, emitname, active, emitx, emity, xspeed, yspeed)

	local currentemitter = emittertypes[emittype]
	local newemitter = emitname
	emitterlist[newemitter] = {name = emitname, type = emittype, spawntimer = currentemitter.spawnrate, isActive = active, x = emitx, y = emity,
	emitxspeed = xspeed, emityspeed = yspeed, life = currentemitter.emitterlife}
	--emitterlist[#emitterlist+1] = newemitter
	
	print("Created emitter '"..emittype.."' as '"..emitname.."'! There are now "..#emitterlist.." emitters.")
end
emittype is the name of the emitter template being used, so I can look it up later if I need values.
emitname is the unique name of this emitter instance, in the form of a string. This allows me to switch specific emitter instances ON or OFF during the game.

But I noticed something troubling. When it runs the final line, it says "There are now 0 emitters".
Does this mean that if I index a table value with a string, it is no longer indexed as a number?

Because when updating the active emitters, I will need to count the number of emitters in the list, so I can update them one by one. But I also need to be able to find a specific emitter later and turn it ON or OFF. What would be the best way to tackle this issue?
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Simple question about tables

Post by pgimeno »

That's because the # operator only reports meaningful results for numeric indexes, not for string indexes, and then only for those starting with 1 and with no missing entries.

For example:

Code: Select all

$ luajit
LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> mytable = {}
> mytable["string1"] = true
> mytable["string2"] = true
> print(#mytable)
0
> mytable[1] = true
> mytable[2] = true
> print(#mytable)
2
>
If you want, you can keep track of how many there are, by using a numeric variable acting as a counter, that you increment each time you add an entry, but checking first if the entry exists before overwriting it (to not count it twice).

But I don't think you need to count the number of emitters. You can go through every element in a table by using pairs like this:

Code: Select all

for key, value in pairs(mytable) do
  -- Do stuff here with each of the keys and/or values.
  -- If the values are tables, 'value' contains the contained table,
  -- so you can reference it with e.g. value.name, value.type, etc.
  -- Of course you can use any names instead of key, value.
  -- For example, for index, element in pairs(...) do ...
  -- or whatever suits your needs.
end
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Re: Simple question about tables

Post by Imaculata »

Ah, much appreciated, thanks!

And my apologies if a lot of this is basic beginner stuff.
I greatly appreciate all the feedback and help.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], slime and 221 guests