Could really use some help in Lua, have hit a wall and can't move forward.

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
Paz
Prole
Posts: 11
Joined: Sat Jun 09, 2018 12:13 pm

Could really use some help in Lua, have hit a wall and can't move forward.

Post by Paz » Mon Jun 18, 2018 1:52 pm

I'm trying to use Lua in combination with Love2d to animate a sprite. However there is a programming problem that I have run into that I can't figure out.

I'm not sure how to recognize multiple specific keys in the table. For example both image 1 and 2 have x values. How would I specify in code "if the x positions of image 1 and 2 exceed the full width of the display set them both to 0"? I attempted this on lines 22 and 23 as you can see.

On line 21 I tried to identify all the x values as well in the specific keys in the table in order to incrementally increase them all simultaneously. However I just don't know how to accomplish that really. My code for line 21 actually works perfectly. It just seems incredibly convoluted and unintuitive. And also the same method cannot be applied to line 22 and 23 unfortunately.

Here is the code: https://ideone.com/sAyXNQ

User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by joedono » Mon Jun 18, 2018 3:50 pm

Hard to say without knowing what you actually want this code to do. It looks like you actually meant for line 23 to end with para.x = 0 instead of para[currentFrame].x = 0. Since as soon as the player lifts off of the "d" key, the currentFrame will switch to the image in para that hasn't had it's x value updated.

Also, without using some sort of array helper library, like 21 is the correct way to update something in each object of an array.

User avatar
Paz
Prole
Posts: 11
Joined: Sat Jun 09, 2018 12:13 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by Paz » Mon Jun 18, 2018 6:31 pm

joedono wrote:
Mon Jun 18, 2018 3:50 pm
Hard to say without knowing what you actually want this code to do. It looks like you actually meant for line 23 to end with para.x = 0 instead of para[currentFrame].x = 0. Since as soon as the player lifts off of the "d" key, the currentFrame will switch to the image in para that hasn't had it's x value updated.

Also, without using some sort of array helper library, like 21 is the correct way to update something in each object of an array.


Essentially the code is supposed to animate a single sprite. However, I'm having a really hard time accomplishing that, honestly I don't even know if I'm going about this the right way. It does seem very archaic how I have to update every frame of animation independently, it feels like I'm basically mushing together a bunch of different sprites instead of one cohesive one.

The first image is supposed to be the resting frame of animation. The second image is supposed to be appear while the sprite is "running" which is accomplished via holding the "d" key down. Assuming like you said line 21 is correct (which I kinda doubt tbh with you even though it works) how would I apply a similar design to make like 22/23 work? I want to compare the x values of the two images in a conditional. However I can't think of a way to accomplish this.... I tried using a for statement as you can see to look at every x value but it didn't work. How would I take all those specific values from the table and put them into an if statement? The objective of line 22/23 is to set the x position back to zero if they exceed the width of the display.

pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by pedrosgali » Mon Jun 18, 2018 7:48 pm

Wouldn't this be easier if you had a sprite position that you change with the d key and then drew either sprite at that position? That way you only need to check one value and just determine which image you need to draw. I may have misunderstood what you are asking for but that seems like the easiest way. You shouldn't have to hold screen coordinate's for each image but rather have one table with all the 'player' data like x and y position and then draw the image at that position.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end

User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by joedono » Mon Jun 18, 2018 7:57 pm

I applaud your effort to attempt to roll your own animation code. It can be a great learning experience. But if you want this to "just work," then I suggest using Anim8 (https://github.com/kikito/anim8) or another of the Love2D helper libraries for animation that the community has written.

User avatar
Paz
Prole
Posts: 11
Joined: Sat Jun 09, 2018 12:13 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by Paz » Mon Jun 18, 2018 8:56 pm

joedono wrote:
Mon Jun 18, 2018 7:57 pm
I applaud your effort to attempt to roll your own animation code. It can be a great learning experience. But if you want this to "just work," then I suggest using Anim8 (https://github.com/kikito/anim8) or another of the Love2D helper libraries for animation that the community has written.
I don't want to use code someone else wrote... I'm trying to learn how to program, that's why I'm asking this question. If I wanted to just have something animate I could literally just copy and paste someone else's code. I definitely don't want to use a separate library. Which completely goes against what I'm trying to achieve here.

User avatar
Paz
Prole
Posts: 11
Joined: Sat Jun 09, 2018 12:13 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by Paz » Mon Jun 18, 2018 9:07 pm

pedrosgali wrote:
Mon Jun 18, 2018 7:48 pm
Wouldn't this be easier if you had a sprite position that you change with the d key and then drew either sprite at that position? That way you only need to check one value and just determine which image you need to draw. I may have misunderstood what you are asking for but that seems like the easiest way. You shouldn't have to hold screen coordinate's for each image but rather have one table with all the 'player' data like x and y position and then draw the image at that position.
So you propose having a single variable tied to all the x and y positions? That would work fine but I actually want to modify other parameters associated with the draw function. For example I want to modify the scaling parameter. How would I accomplish that if all the draw function parameters were tied to a single table? Sorry if what I just wrote makes no sense. I'm really struggling to conceptualize this.

User avatar
zorg
Party member
Posts: 2747
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by zorg » Mon Jun 18, 2018 9:15 pm

Defining the local elapsedTime inside love.update itself is asking for the variable to be reset every frame; move that out of the function.

Also, to be more clear, whatever image or even sprite sheet one specific animation might use doesn't necessitate them to have separate x and y positions; logically those go to the object itself, of which there's only one; kirby itself.

The answer to your other question is, store stuff that can be different, like scaling, separately per frame/animation, but the world coordinates should not be duplicated; if anything, you'd store constant offsets if your images have positioning issues.

Other than "don't use pairs for integer indices", i didn't see an issue with that part of the code you posted, it did what it was supposed to do.



Finally, you could also just *look* at anim8, maybe you can spot ideas you could learn from / implement in your own code; it's not necessary to suffer through reinventing the wheel, you know :3
(Also please don't double-post.)
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
Paz
Prole
Posts: 11
Joined: Sat Jun 09, 2018 12:13 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by Paz » Mon Jun 18, 2018 11:02 pm

zorg wrote:
Mon Jun 18, 2018 9:15 pm
Defining the local elapsedTime inside love.update itself is asking for the variable to be reset every frame; move that out of the function.

Also, to be more clear, whatever image or even sprite sheet one specific animation might use doesn't necessitate them to have separate x and y positions; logically those go to the object itself, of which there's only one; kirby itself.

The answer to your other question is, store stuff that can be different, like scaling, separately per frame/animation, but the world coordinates should not be duplicated; if anything, you'd store constant offsets if your images have positioning issues.

Other than "don't use pairs for integer indices", i didn't see an issue with that part of the code you posted, it did what it was supposed to do.



Finally, you could also just *look* at anim8, maybe you can spot ideas you could learn from / implement in your own code; it's not necessary to suffer through reinventing the wheel, you know :3
(Also please don't double-post.)
I'll take a look at it. I'm just really hesitant to start giving up on doing stuff independently. My entire life I've always cast off actual exercises and practice in favor of viewing others. I'm really trying to do this myself. Is it normal to really struggle with basic animation? I just watched and read a ton of documentation/tutorials (lua is my first programming language) but this stuff is seriously confusing the fuck out of me atm. It feels weird to just watch someone code and then simply copy their technique in order to accomplish a certain task. I feel like I'm at a roadblock. Usually this is when I give up lol..

DarkShroom
Citizen
Posts: 86
Joined: Mon Jul 17, 2017 2:07 pm

Re: Could really use some help in Lua, have hit a wall and can't move forward.

Post by DarkShroom » Mon Jun 18, 2018 11:31 pm

i agree with you, well done, i have rarely agreed with some of these libraries myself, although i am not sure it might have been the case i should've used one of the camera ones as i am sort of rendering everything offscreen at the moment

i have no problem with animation in my framework, i simply string an array of images to cycle through, and have a separate optional frame duration array

i understand you're going for a tileset solution though... i don't want this yet as i consider myself to be prototyping... i feel if i do use tilesets, i will build these in lua itself from my separate frame assets (i don't like working with tile-sets, it makes me feel like i'm on a megadrive)

this answer may not help you, but sometimes it is best to get a prototype solution working first (although this may depend on your assets)


however, that said as it may be a required future optimisation i might make a point of tackling this tomorrow, assuming i finish my terrain deformation strategy and other pending task

in the meantime, have you read this yet, i don't remember it being at all complicated:

https://love2d.org/wiki/Tutorial:Effici ... _Scrolling

i see no "quads" in your code... good luck

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests