Can you give me hints to improve my defold programmation? (SOLVED)

Hello guys,

I want to learn the right way to create GO scripts. So if it is possible, see the code bellow and give hints to improve it. Thank you!!!

info about my project:
Window: 540x435
Ball Objetc: 128x128

Mission: Do the ball move through the canvas from left to right and stop moving when de right edge of ball “touch” the right edge of canvas.

My Game object script. (it is working. but im sure that can be improved) thank you guys!

local speed;
local WorldWidth = tonumber(sys.get_config("display.width")) -- is it the best way to access end store this info?

function init(self)
    msg.post(".", "acquire_input_focus")
   self.speed    = 6
   self.myWidth  = 128 -- is it the best way to access end store this info?
   self.myHeight = 128 -- is it the best way to access end store this info?
end

local function goToRight(self) 
    local p = go.get_position()
    p.x = p.x + self.speed
    local worldRightLimit = WorldWidth -  self.myWidth/2
    if p.x <= worldRightLimit then
    	 go.set_position(p)
    end

end


function update(self, dt)
     goToRight(self) -- is it the best way to do this movement? Or is there native functions?
end

That looks pretty good to me. A few notes:

  1. Updating the position of a game object can be done in two ways, either by calling go.set_position() or by “tweening” (or animating) the “position” property. When you want to set the position from input or when you have logic tied to the movement your solution is good.

  2. The local speed on the first line is unused so you can safely drop that.

  3. You might want to look into using the variable “dt” that is sent to update(). It contains the delta time since last frame. Usually this is 1/60 (0,0166666) seconds if you run at 60 frames per seconds. Your speed self.speed is currently expressed as “6 pixels per frame” which works fine as long as you don’t drop any frames. It is good practice to express movement in units per second instead so:

function init(self)
   ...
   self.speed = 360  -- 360 pixels per second
   ...
end

local function goToRight(self, dt)
    local p = go.get_position()
    p.x = p.x + self.speed * dt -- <---- multiply with the delta t value
    ...
end

function update(self, dt)
     goToRight(self, dt)
end

Thanks Sicher.

Tip 1:

1- Where can i leran more about this? is there some link?

Tip 2: I did it!.

Tip 3: I did it too!. this was a important hint… I didn’t know which was the dt variable objective. Thank you.

Check out “Property animation” on http://www.defold.com/manuals/animation/

A good tip in general when learning a new tool, is to simply browse through all documentation a couple of times.

First, a quick glance to see what main topics there are: In our case, the “API Reference”, “Manuals”, and “Tutorials”.
Secondly, step into each of those, and do a quick glance to see what topics in there.
Third, chose one topic you want to learn more about.
Repeat these steps until you feel you know enough to move ahead. E.g. code style, keywords, various terminology.

2 Likes

Yes Mat, I have read the documentation and tutorials. I’m working hard to learn Defold soon as possible, and start to build professionals games.

Before Defold, i was a volunteer at DFJUG Brazil, a Java User’s Group that teach game programing to beginner’s using Greenfoot Framework.

To help theirs project, I translated the english official course to Brazilian Portuguese, built some short games and i recorded a lot of special teach videos in our native languange to offer more material for ours students.

But greenfoot it is only to beginners and now i want build professional games, and to do this I choosen Defold.

So… lets study. :smiley:

Thanks for your time!

2 Likes