Need help with Lua + OOP

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
User avatar
Makubo
Prole
Posts: 3
Joined: Mon Apr 17, 2023 11:33 pm

Need help with Lua + OOP

Post by Makubo »

Hi guys. For learning purposes I am trying to recreate the "Battle City" game from NES with LÖVE. And now I am faced with strange behavior of "objects". I will appreciate any help.

Let me explain the project structure.
Now there is only TileMap realization and game object structure looks like:

Scene -> TileMap -> TileSet(multiple) -> Tile(multiple)

GameObject - is a base or root object.
Each object responsible to call update and draw functions of its child objects.

Here is the problem part of Tile.lua from Repository and branch: https://github.com/makubo/battle-city-w ... ee/oop-wip. (I deleted some junk-debug comments here in example)

Code: Select all

function Tile:draw(xPos, yPos)

	if xPos == nil then
	    	xPos = 0
	end
	if yPos == nil then
    		yPos = 0
	end

	if self.animation ~= nil then                                	-- < Do not enter inside condition. looks like every tile doesn't have animation (but in update function i have different results)
	    	print("Tile draw frame" .. self.frame)               	-- < So I can't see it
	end
	love.graphics.draw(self.texture, self.quads[self.frame], xPos, yPos)

end


function Tile:update(dt)

	if self.animation ~= nil then                                	-- < Enter inside condition as I expect
    		local frame = self.frame % #(self.animation) + 1
    		local durationSec = self.animation[frame].duration / 1000
    		if self.timer >= durationSec then
        		self.frame = self.frame % #(self.animation) + 1
	        	print("Frame - " .. self.frame)                 -- < I see the frame updates here
	        	self.timer = 0
    		end
	    	local timer = self.timer + dt
	    	self.timer = timer
	end
	
end
When you start the project it runs stage_04 TileMap which contains tiles of water which should be animated.

PS: In the master branch you can see animation realization without OOP and it works :(
PPS: The Tile class also was inheritor of the GameObject class but I rewrite it as standalone class, because I thought I don't understand Lua inheritance realization correctly.
Spiffy
Prole
Posts: 1
Joined: Tue Apr 18, 2023 10:05 pm
Location: Victoria, BC

Re: Need help with Lua + OOP

Post by Spiffy »

Hey I checked out the branch you linked. I tried assigning a unique id to each tile, to see why those functions are behaving differently. I think something else has gone wrong in your refactor, because the draw function is never being called on the tile that contains the animation.

Something seems to be going wrong with the isGlobalTidIn function in TileSet. I don't understand the code well enough to guess more than that.
User avatar
Makubo
Prole
Posts: 3
Joined: Mon Apr 17, 2023 11:33 pm

Re: Need help with Lua + OOP

Post by Makubo »

Thank you, Spiffy!

The isGlobalTidIn is just a function to handle Tile IDs in case TaleMap contains multiple tilesets. I will recheck it.
User avatar
Makubo
Prole
Posts: 3
Joined: Mon Apr 17, 2023 11:33 pm

Re: Need help with Lua + OOP

Post by Makubo »

I found the issue. It was because IDs in TileSet exported with Tiled starts counting from 0 while in TileMap from 1.
User avatar
dusoft
Party member
Posts: 611
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Need help with Lua + OOP

Post by dusoft »

Makubo wrote: Fri Apr 21, 2023 3:44 pm I found the issue. It was because IDs in TileSet exported with Tiled starts counting from 0 while in TileMap from 1.
Yeah, that's Lua. It recommends indexes starting from 1, so if you use that, it works better that way.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests