Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Are global functions good practice in Gideros? — Gideros Forum

Are global functions good practice in Gideros?

NinjadoodleNinjadoodle Member
edited October 2017 in General questions
Hi guys

I have a lua file with a bunch of reusable functions and I like to use in my scenes/levels. I'm just wondering whether this is good practice.

Here is a simple example of one of them ...
------------------------------
 
------ GLOBAL FUNCTIONS ------
 
------------------------------
 
function shakeScreen(container)
 
	local dy = (application:getLogicalHeight() - 320) / 2
	container:setPosition(-10, dy - 10)
	GTween.new(container, 0.25, {x = 0, y = dy}, {delay = 0, ease = easing.outBounce})
 
end
Thank you in advance :)

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    It depends on your religion :) To me locals should be preferred for maintainability and to prevent name clash. Other than that, there is nothing wrong in using globals if they make the code faster or easier to read.

    Likes: antix, Ninjadoodle

    +1 -1 (+2 / -0 )Share on Facebook
  • antixantix Member
    edited October 2017 Accepted Answer
    You could always make your reusable code into a utility class..
    Utility = Core.class()
     
    function Utility:init()
    end
     
    function Utility:shakeScreen(container)
      local dy = (application:getLogicalHeight() - 320) / 2
      container:setPosition(-10, dy - 10)
      GTween.new(container, 0.25, {x = 0, y = dy}, {delay = 0, ease = easing.outBounce})
    end
    Using the same principle as I outlined with the player class in main.lua..
    UTILITY = Utility.new()
    Then in the classes that need it just use..
      self.utility = UTILITY
    After 3 published games this method has worked out without any negative consequences that I know of :)
    +1 -1 (+2 / -0 )Share on Facebook
  • Thanks @hgy29 :)

    @antix - Thanks! It seems like this would be a good way to do it on a larger project.

    I appreciate the help!

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • totebototebo Member
    edited October 2017
    I tend to extend existing classes, like Table.shuffle(). It's quite rare that you need a global method that is completely independent (in my experience), but I do tend to create them like your first example. But that would be stuff like rPrint() (recursive print) and stuff like that. I would make screenShake part of the camera class, or gameworld class.

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Camera Class :)
  • ... depending on whether you want camera shake or earthquake shake. :)
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    Could I have milk shake instead ? ;)

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • Soy Shake for me thanks (Strawberry made with real fruit). I've given up consuming infant bovine growth hormone (with the added pus, eww) :D
  • @totebo - when you refer to a game world class, would that be the level class in my case?

    Stage01 = gideros.class(Sprite)

    function Stage01:init()

    The issue here is that I have many levels with different code, so I would have to do the setup for every level.

    There are so many ways to do the same thing, I'm starting to get confused / feel like I'm doing things wrong lol.
  • antixantix Member
    edited October 2017
    @Ninjadoodle the different code in levels is because these are like little sub games that all require different code right? Or something else?
  • @antix - yeah, basically all the levels would be different tiny games :)
  • @Ninjadoodle you will either have to create a class for each type of level, or have one huge-as class with a lot of conditionals in there.

    I think personally I would go with a separate class for each sub game type or it will most likely soon become unmanageable.
Sign In or Register to comment.