About dt...(edit: and something else)

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
Skygrinder
Prole
Posts: 18
Joined: Wed May 30, 2012 12:48 am

Re: About dt...(edit: and something else)

Post by Skygrinder »

That article is saying exactly what I wanted to say. I mean, I'm reading the tutorial...and everything's fine, but suddenly, some word comes up, that I understand, so I keep on reading, and then I stop...and I'm like: "What the fuck does that mean?", and then I'm back on the start of the tutorial, reading it all again, trying to make some sense of it.

Other times, I just run into a word that I don't even get, I translate it, but it still makes no difference, when it comes to me understanding whatever the tutorial is about.

Anyway, that was a nice read for me.

I like the idea for that beginner tutorial. Sort of like, making a dairy of my learning process, and share it later, when I'm more experienced. I would definitely like to give something in return, to the community that is teaching me.

Yep, I get your example, and that's what I was thinking.

As for:

Code: Select all

for example:
enemies = {}

for a = 1, #enemies do
enemies[a] = {}
enemies[a].x = 0
enemies[a].y = 0
end

That just did what this did except for 100 instead of 1.
enemy_1_x = 0
enemy_1_y = 0
I just don't understand how those two things are the same, while the they're almost same in the size. Shouldn't there be some values for enemies, different values...

I mean, if I had 100 enemies, and all 100 enemies have completely different stats, how could I deal with that. other than typing it all out. Say, if we have Strength, Endurance and Agility, and these 100 enemies have different amount of each of those stats...is there really a way of setting stats for them, without actually typing them all out separately, for every different enemy?

Could you give me a real code with tables, to see if I can make any sense of it?
A code with nothing, but a usage of tables. I want to see how it is written, and used.

While looking at the example above, I'm wondering what is "for a = 1" and "#enemies do"...and what is "a" representhing, and how does that little code affect 100 different things, unless it's just a copy of 100 enemies, that are the same in essence.

Anyway, yeah, a real code example with just tables, and their usage, and nothing else would be nice. Maybe a .love file where I can see the results, and with enough comments in the code, that explain what is doing what, when you actually turn the game on, and see the result. I think that would be able to help, not just me, but all beginners that are having hard time with tables.

EDIT:
@Lynesth

You're good at this xD

Let's see if I get it, I think I do...

apple = {}
apple.one = 10
apple.two = 20
apple.three = 30

Now, if I wanted to see the price of all three apples together, I would do...

love.graphics.setColor(255, 255, 255)
love.graphics.print(apple.one + apple.two + apple.three, 50, 50)

That would, I'm pretty sure, print the number 60. Now, what I've been wondering after reading your reply...

If I use this method to define the table...would apple.one's value still be accessible by using apple[1]? or apple.two's value, by typing apple[2]?

or does that just apply to the method where you make table like "apple = { value, value, value...}"?

I wanted to try something more complicated, but I just couldn't figure it out...let's say now, all enemies have 3 stats (strength, agility and endurance), and I want to spawn 3 different enemies (different graphics), but I also want them to have different stats, how would I do that through tables, and is it possible?

or do stats need to be set manually for every enemy, as well as graphics?

I just don't see how tables can reduce the amount you're typing, because...you still need to write where the graphic is, the values in the table...

Could you do something like what I've asked above, I want to see how would you go on about doing that. I think I do understand the basic tables, but it feels like it has much more depth to it, that I don't understand yet.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: About dt...(edit: and something else)

Post by Lynesth »

Skygrinder wrote:@Lynesth

You're good at this xD
Thank you :p
Skygrinder wrote:Let's see if I get it, I think I do...

apple = {}
apple.one = 10
apple.two = 20
apple.three = 30

Now, if I wanted to see the price of all three apples together, I would do...

love.graphics.setColor(255, 255, 255)
love.graphics.print(apple.one + apple.two + apple.three, 50, 50)

That would, I'm pretty sure, print the number 60.
Yes it should work that way.
Skygrinder wrote:Now, what I've been wondering after reading your reply...

If I use this method to define the table...would apple.one's value still be accessible by using apple[1]? or apple.two's value, by typing apple[2]?

or does that just apply to the method where you make table like "apple = { value, value, value...}"?
It only works with the later method using "table = { value1, value2, value3 }"
Skygrinder wrote:I wanted to try something more complicated, but I just couldn't figure it out...let's say now, all enemies have 3 stats (strength, agility and endurance), and I want to spawn 3 different enemies (different graphics), but I also want them to have different stats, how would I do that through tables, and is it possible?

or do stats need to be set manually for every enemy, as well as graphics?

I just don't see how tables can reduce the amount you're typing, because...you still need to write where the graphic is, the values in the table...

Could you do something like what I've asked above, I want to see how would you go on about doing that. I think I do understand the basic tables, but it feels like it has much more depth to it, that I don't understand yet.
What you can do here with tables would look like this (I'll try to split up things as much as I can so you can understand better) :

Code: Select all

--First I create all different types of enemies
enemytype1 = {str=100, agi=100, end=100, image=love.graphics.newImage('enemy1.png')}
enemytype2 = {str=200, agi=50, end=75, image=love.graphics.newImage('enemy2.png')}
enemytype3 = {str25, agi=25, end=300, image=love.graphics.newImage('enemy3.png')}

--Then I create every enemy this way
enemy1 = {x=0, y=400, health=100, type=enemytype1} --I've added some variables like x and y for positionning and health
enemy2 = {x=50, y=400, health=100, type=enemytype1}
enemy3 = {x=100, y=400, health=100, type=enemytype2}
enemy4 = {x=150, y=300, health=100, type=enemytype3}
enemy5 = {x=200, y=400, health=100, type=enemytype3}

--Then I can add them to my global table with all my ennemies in it
allenemies = {enemy1, enemy2, enemy3, enemy4, enemy5}

--Then when you need to do something (like drawing all of them) use a FOR statement
function love.draw()
    for i,v in ipairs(allenemies) do --Parses the "allenemies" table and store value in variable "v"
        love.graphics.draw(v.type.image, v.x, v.y)
        -- "v" is the actual value, v.type would return "enemytype1 2 or 3" and then, "image" will return
        -- the image value of the enemytype table returned just before.
        -- IF YOU DON'T UNDERSTAND THAT, feel free to ask for precisions.
    end
end
EDIT :

And let's say that you want to reduce the health of enemy2 which has just been shoot by the player, you would just have to :

Code: Select all

allenemies[2].health = allenemies[2].health - healthlost
But this all would be good if you have a small number of enemies. If not, I would instead advise you to use something like MiddleClass.

;)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Skygrinder
Prole
Posts: 18
Joined: Wed May 30, 2012 12:48 am

Re: About dt...(edit: and something else)

Post by Skygrinder »

So, doing damage to an enemy can ne done by making a code which would say that if anyone in "allenemies" table is hit by a bullet, they would lose healthlost (which is a number variable). Of course, if they had different kinds of defenses and resistances, it would deal less or more damage, thus affect every enemy differently. I see how that can be useful now.

As for MiddleClass, it's too advanced for me at the moment, I will leave it for now. I checked the example code they've shown, it just got me confused.

Also

for i,v in ipairs(allenemies) do
love.graphics.draw(v.type.image, v.x, v.y)

That part in your code in the end confuses me, but I didn't use "for" command yet, at all, so that's new to me.
User avatar
Skygrinder
Prole
Posts: 18
Joined: Wed May 30, 2012 12:48 am

Re: About dt...(edit: and something else)

Post by Skygrinder »

Thank you all for helping me! +karma ^^
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: About dt...(edit: and something else)

Post by Lynesth »

To stay with tables (considering the same tables as my last post), here is what you can do (all I will write down here is untested but should give you general idea) :

Let's say that the player just made lightning strike vertically a random place (it's a hero so he can do that).
First you want to check if lightning hit any ennemy. Since lignthning goes vertically, we just have to check if there's an enemy on it's x axis with something like that (Ask Google for more informations about Lua's FOR statement) :

Code: Select all

for i,v in ipairs(allenemies) do --This goes through the table, storing key in i and value in v one after another.
    if v.x == lightning.x then --If the enemy x position is equal to the lightning x position
        v.health = v.health - lightning.damage --We substract lightning damage from this enemy's health
        if v.health <= 0 then --We check if this enemy just died
            allenemies.i = nil --We remove this enemy from the list
        end
        break --This ends the FOR statement as we already found the enemy struck we don't need it to go any futher
    end
end
I assumed in this code that lightning.x (position of the lightning) and lightning.damage (damage done by this lightning) were already set and all.

Hope it helped you understand a bit more what you can do.
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: About dt...(edit: and something else)

Post by bartbes »

I am surprised I haven't seen physics involved in the dt talk. Alright, let's go:
So normally you specify speed/velocity as distance over time, so:
$$ v = \frac{x}{t} $$
This means that if you want to know how much you moved (or have to move) in a certain period of time, you multiply your velocity by time:
$$ x = v*t $$

You know this works on large scales, go 100 km/h for 2 hours and you go 100*2=200km, but this also works on small scales. Now, this is where your misunderstanding of dt comes in, dt (delta time, aka time difference) is not 1 over fps, but fps is 1 over time. Even though mathematically that's the same, I'm trying to explain the concept that fps is derived from dt, not the other way around.
$$ fps = \frac{1}{\Delta t} $$

Dt (above written with an actual delta symbol) is a time period, to be more exact the time difference between this and the past call to love.update, and as we've seen before, multiply a velocity by an amount of time and you get displacement, therefore:
$$ \Delta x = v * \Delta t $$

And therefore:
$$ x = x + v * \Delta t $$

Now, the last thing I'd like to say is that in most cases you should think of a speed as pixels/second, or in any case something/second.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: About dt...(edit: and something else)

Post by Nixola »

A "for" statement is 'simply' a way to repeat several times a chunk of code, writing it only once. There are 2 types of "for", the numeric for and the "general" for.

The numeric for:

for i = 1, 10 do
--some code that will run several times, the first one with the variable i equal to 1, the second one with i equal to 2, and so on until i = 10, before the script goes on
end

"for" tells lua that you're trying to create a for loop; i is simply the name of the variable (you don't need to create it before, it's automatically created here) that, every time the loop is run, will cycle from 1 to 10. "do" tells lua that you ended declaring the loop and that the code below is the one that has to be executed several times; then we have the code, and finally and 'end' to tell lua that the code that needs looping is ended. After looping 10 times, lua will continue reading the script.


The general for:
for i, v in ipairs(enemies) do
--some code
end

Again, for tells lua that you're creating a for loop. i and v are two variables, like the 'i' in the previous loop, that will cycle through the values provided by ipairs. ipairs is a function that will loop through the numeric indexes (or keys) of a table given as argument (enemies, in this case), 'providing' the index it's analyzing as the first variable (i in this case) and the value of that index as the second variable (v in this case). This is our enemies table:

Code: Select all

enemies = { 10, 25, 7, 66, 9 }
it has 5 elements, at index 1, 2, 3, 4 and 5. So, the loop will run 5 times, the first one having i = 1 and v = 10 (v is exactly the same as enemies), the second one having i = 2, v = 25, the third one having i = 3, v = 7, the fourth one having i = 4, v = 66 and the fifth one having i = 5, v = 9. If, for example, we had enemies[6] = nil and enemies[7] = 99 the ipairs loop would stop at 5, because it 'sees' that the next index is empty and it stops, even if there are more 'full' indexes later.
If you use pairs(enemies) instead of ipairs(enemies), the loop will cycle through every index of the table. For example, another table:

Code: Select all

enemies =  { 10, 25, 7, 66, 9, nil, 99, x = 15, y = 22 }
The ipairs loop would stop at index 5 and ignore index 7, and it wouldn't ever consider indexes other than numerical. The pairs loop will run through every index: 1, 2, 3, 4, 5, 7, x and y. It will ignore the index 6 because it's nil, and it won't necessarily loop in order, it could have i = 3 (and v = 7) the first time, i = 7 (and v = 99) the second time, but the loop will repeat 8 times (first 5 indexes + 7th indes + 'x' index + 'y' index).

If there's anything you didn't understand, let me know, please.

P.S: you don't need to call dt 'dt', if you write "function love.update(delta)" you'll have to refer at it as delta, not dt. If this confuses you (I hope it doesn't) ignore it
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Skygrinder
Prole
Posts: 18
Joined: Wed May 30, 2012 12:48 am

Re: About dt...(edit: and something else)

Post by Skygrinder »

@bartbes
I'd talk about physics, if I actually knew something about it. xD
Anyway, at the moment, I doubt I'll be able to understand DT any better than I do now...it could be because of the lack of sleep (I've been up all night, messing with love). I'll be sure to look through this topic all over again, once I get some sleep.

@Nixola

First of all, where are you from? :D (Asking because of your forum name, seems to me like you wanted Nikola, but it was taken or something, and Nikola is a very common name here xD)

Anyway...

I'm guessing "for" is a good way to do stuff like collection quest text notifications? From what I've understood, something like...

(questitem = 0)

for i = 1, 10 do
if questitem = questitem + 1 then
love.graphics.print("You've collected" .. questitem "apples!")
end
end

Would that work?

Of course, there would have to be a previous code that registers that you've picked up an item. Not really sure how to do that, so I can't write it :P

And I was wondering...what exactly is the best way to animate something in love? Haven't tried it yet, or even look it up for that matter. My only guess is by drawing and deleting pictures, on after the other, but doesn't seem like a very efficient way to do it. I'm guessing "for" can do it, as well, by reading images from the table, right?

Anyway, I'll look through these posts tomorrow some more, to try to understand it better, once I get some sleep. This love really pulled me in, it's very interesting. I actually didn't play any game since I started learning this stuff, and I'm a pretty hardcore gamer .__.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: About dt...(edit: and something else)

Post by Nixola »

Almost! My name's Nicola, I'm from Italy ^^
Anyway, that would simply repeat exactly the same chunk of code without variation, and a variable can never be equal to itself + 1 (unless it's equal to math.huge). An useful for loop takes advantage of a variable (or two variables) changing every time.
Note that a for loop runs (for example) 10 times in a row, this means that, if I have this code:

Code: Select all

x = 1
for i = 1, 10 do
  x = x + i
end
print(x)
print(x) won't print 11, then 13, then 16, then 20, then 25 and so on, it will print only 65 because the for loop blocks every other operation until it's done

EDIT: this means also that doing an animation inside a for loop will only draw its last frame
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Skygrinder
Prole
Posts: 18
Joined: Wed May 30, 2012 12:48 am

Re: About dt...(edit: and something else)

Post by Skygrinder »

It seems very hard to understand :/

Tomorrow, or...whenever I wake up...xD I'll try making something with "for" and tables, maybe that will help me understand it by doing it, rather than trying to understand it only as theory. ._.

Thank you for the help
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests