Page 1 of 1

Need help with Lua + OOP

Posted: Tue Apr 18, 2023 12:44 am
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.

Re: Need help with Lua + OOP

Posted: Tue Apr 18, 2023 10:20 pm
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.

Re: Need help with Lua + OOP

Posted: Wed Apr 19, 2023 10:46 pm
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.

Re: Need help with Lua + OOP

Posted: Fri Apr 21, 2023 3:44 pm
by Makubo
I found the issue. It was because IDs in TileSet exported with Tiled starts counting from 0 while in TileMap from 1.

Re: Need help with Lua + OOP

Posted: Sat Apr 22, 2023 10:30 am
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.