Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Is this bad practice? — Gideros Forum

Is this bad practice?

Tom2012Tom2012 Guru
edited October 2012 in General questions
I noticed that some of my code was getting a little long. So I have been creating additional .lua files such as 'game over.lua'

That have a function in them like
function endGame(scene) -- Game over
 
local self = {}
self.scene = scene;
 
	local function reduceChangeSceneCountDown()
 
		self.secondsUntilChange = self.secondsUntilChange +1;
		if(self.secondsUntilChange == 5) then
			self.timer:stop(); -- stop timer counting down
			-- decide which scene to show
			sceneManager:changeScene("Game Over", .5, SceneManager.crossfade, easing.outBack);
		end
	end
 
	if(self.scene.secondsLeft == 0) then
 
	self.secondsUntilChange = 0;
 
 
	local changeSceneCountDown = Timer.new(1000,5);
	changeSceneCountDown:addEventListener(Event.TIMER, reduceChangeSceneCountDown);
	changeSceneCountDown:start();
	self.timer = changeSceneCountDown;
 
	end
 
end
Then using this in my level one code (which was getting too long)
endGame(self)
This helps with organization, but is it 'allowed'?

Thanks

Comments

  • And what confuses you? =)
  • asakharovasakharov Member
    edited October 2012
    function reduceChangeSceneCountDown()
    	sceneManager:changeScene("Game Over", .5, SceneManager.crossfade, easing.outBack);
    end
     
    function endGame()
    	local changeSceneCountDown = Timer.new(5000, 1);
    	changeSceneCountDown:addEventListener(Event.TIMER, reduceChangeSceneCountDown);
    	changeSceneCountDown:start();
    end
    Using:
    ...
    if (scene.secondsLeft == 0) then
      endGame(self)
    end
    ...
    It seems that this code is equivalent to your code. But easier to read.
    What do you think?
  • I notice you're not using the OOP facilities that Gideros supplies. If you create different classes of object then you can have one Lua file for each class. Typically, this means that the files are manageable in size and the logical arrangement means it's easy to find the different functions.

    I generally have a separate class (and therefore file) for each of:

    1) the game itself. This is generally a singleton or global that I can reference from anywhere else.
    2) each scene
    3) each significant game object that has its own behaviour.

    Best regards
  • Strange, I thought everyone used the OOP facilities as @bowerandy said. There are plenty of examples provided to show you how it's done.
  • Hi guys. I used the OOP a lot in my code but sometimes it seems faster to do it this way. Just wondering if anyone else used this method.
  • function reduceChangeSceneCountDown()
    	sceneManager:changeScene("Game Over", .5, SceneManager.crossfade, easing.outBack);
    end
     
    function endGame()
    	local changeSceneCountDown = Timer.new(5000, 1);
    	changeSceneCountDown:addEventListener(Event.TIMER, reduceChangeSceneCountDown);
    	changeSceneCountDown:start();
    end
    Using:
    ...
    if (scene.secondsLeft == 0) then
      endGame(self)
    end
    ...
    It seems that this code is equivalent to your code. But easier to read.
    What do you think?
    Thanks, going to study this now.
  • function reduceChangeSceneCountDown()
    	sceneManager:changeScene("Game Over", .5, SceneManager.crossfade, easing.outBack);
    end
     
    function endGame()
    	local changeSceneCountDown = Timer.new(5000, 1);
    	changeSceneCountDown:addEventListener(Event.TIMER, reduceChangeSceneCountDown);
    	changeSceneCountDown:start();
    end
    Or even shorter:
    function reduceChangeSceneCountDown()
    	sceneManager:changeScene("Game Over", .5, SceneManager.crossfade, easing.outBack);
    end
     
    function endGame()
    	Timer.delayedCall(5000, reduceChangeSceneCountDown);
    end

  • Didn't even know about Timer.delayedCall ar2rsawseen

    You sir are a lifesaver! :-)))
Sign In or Register to comment.