Question about values .

In another thread I was told to “print out the values of the things you are testing and make sure you’re getting the values  you are expecting.” I looked at a few tutorials on values and none of them tells you what code to put to set up the values . I want to know what values I should have and what code I should have .Here is my code :

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local physics = require( "physics" ) physics.start() physics.setGravity(0, 15) -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145; ground.y = 480 ground.myName = "ground" physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x = 60 crate.y = 20 crate.rotation = 0 physics.addBody( crate, "dynamic" , { friction=0.5, bounce=0.3 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isCrate and other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") end return true end crate.collision = onCollision crate:addEventListener( "collision" ) 

Thank you.

@true,

I think the advice you were given was to help you debug a problem like this…

Let’s pretend for a moment that you ran the code above and got an error (crate does not exist) about line 21 (second line in snippet below):

local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x = 60 crate.y = 20 crate.rotation = 0

While this is a pretty straightforward error, you might still verify it as follows:

local crate = display.newImageRect( "crate.png", 90, 90 ) print("create initialized? ", crate ) crate.x = 60 crate.y = 20 crate.rotation = 0

Now, if you get this in the console, you know there was a problem with the call to display.newImageRect():  

create initialized? nil

Then you could look at the file names and the way you called the function to see if there is a problem.

However, what if it printed out this?:

create initialized? table: 03CAECB0

This seems to imply the object was created and something else went wrong.  

Maybe you’re missing a semi-colon:

--crate.x = 60 crate.y = 20 crate.x = 60; crate.y = 20

Note: I’m not saying your code is wrong by the way, this is just a single example of using ‘print()’ to debug.

In closing, this is a basic example of using print to debug issues.   There are many other uses:

  • Print statements at entry point (first line) of functions to see you got in.
  • Print all passed arguments to a function. i.e. On first line, print name of function and all arguments to see that they were not nil.
  • Print statements just before return calls.  i.e. Print name of function and unique marker so you can later identify that as the point where the function was exited.
  • … The list goes on.

In short, there is no recipe for advance print statement placements.  

In fact, don’t put them in till you need them.  

When you do need them, put them in places to help you solve specific problems or answer questions.

I did what you said and it still doesn’t work . Here is what it says in the cmd prompt:

Windows simulator build date: Nov 18 2014 @ 18:35:32

Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2014.2511
WARNING: display.setStatusBarMode() not supported in the simulator for SM-G900S
device

Copyright © 2009-2014  C o r o n a   L a b s   I n c .
        Version: 3.0.0
        Build: 2014.2511
Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2014.2511
create initialized?     table: 02C2E110

Here is my main.lua code :

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local physics = require( "physics" ) physics.start() physics.setGravity(0, 15) -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145; ground.y = 480 ground.myName = "ground" physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) local crate = display.newImageRect( "crate.png", 90, 90 ) print("create initialized? ", crate ) crate.x = 60; crate.y = 20 crate.rotation = 0 physics.addBody( crate, "dynamic" , { friction=0.5, bounce=0.3 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isCrate and other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") end return true end crate.collision = onCollision crate:addEventListener( "collision" ) 

Here is restart.lua code:

local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view end -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145; ground.y = 480 -- display logo image local logo = display.newImageRect( "logo.png", 300, 200 ) logo.x = 160 logo.y = 20 group:insert(logo) group:insert(ground) scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )

What is the problem what corrections should I make.

Thank you.

Huh?

The way I understood your original post, you were saying, “Hey.  Folks have told me before I should use print() statements to debug code.  How would I do that, here is some code we can talk about.”

Now it seems you were trying to get us to fix the code?

I’m confused… nonetheless… try this code and the problem should be clear:

local composer = require( "composer" ) local physics = require( "physics" ) physics.start() physics.setGravity(0, 15) -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145 ground.y = 480 ground.myName = "ground" physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x = 60 crate.y = 20 crate.rotation = 0 physics.addBody( crate, "dynamic" , { friction=0.5, bounce=0.3 } ) local function onCollision( self, event ) local other = event.other print("onCollision()", event.phase, self.isCrate, other.isGround) if( event.phase == "began" and self.isCrate and other.isGround ) then timer.performWithDelay( 30, function() print("Calling composer open scene...") -- composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") end return true end crate.collision = onCollision crate:addEventListener( "collision" ) 

(Notice how nicely indented the above is…please indent your posts to make life easier for those who answer.  I also separated the logical segments into groups by removing blank lines, then reinserting them between unrelated bits of code.  It’s not perfect, but much more legible.)

Hint this is what prints:

onCollision() began nil nil onCollision() ended nil nil onCollision() began nil nil onCollision() ended nil nil onCollision() began nil nil

It seems to me that trueisawesome is having trouble at a more fundamental level than just the problem that’s in the code.

Unless I’m misunderstanding, you don’t know what people mean when they say “values”, so you don’t understand what they mean when they are telling you to “print out the values of the things you are testing and make sure you’re getting the values  you are expecting.”

trueisawesome, does that sound correct?

yes it does.

Thank you.

Values are things like numbers, strings, true/false, tables (actually a memory address to where the table is stored), functions (actually a memory address to where the function is stored).

Typically these values are stored in variables such as:

local myScore = 10

When someone asks you to print out the values and see if you are getting what you are expecting, the mean to insert a print statement like:

print( myScore )

or whatever variable you need to print and look in the terminal/console output window that we open and make sure you see values you expect to see.  In this case, print( myScore ) better print 10 since that’s what we set it to earlier.  When debugging your code where we have no idea what values you expect to be there, it’s helpful for you to print out the variables that you’re curious about and see what’s there.

BTW, it’s always more helpful to print out some additional information like the variable name you are printing:

print( "myScore", myScore)

prints:  myScore     10

In @roaminggamer’s example above he is printing out the string “onCollision()” so you know which line is doing the printing. He also prints out the collision phase (“began”, “ended”, etc.) and the values of the object’s .isCrate value and .isGround value. The reason he chose to print out those specific values is because you are doing a test:
 

if( event.phase == "began" and self.isCrate and other.isGround ) then

It is helpful to know what self.isCrate and other.isGround is where self and other are the two objects involved in the collision.  When the code runs, it says both values are nil.  At this point you should ask yourself why are they nil?  Then look at your code and see if you’re ever setting those values any where.

Rob

@true,

I think the advice you were given was to help you debug a problem like this…

Let’s pretend for a moment that you ran the code above and got an error (crate does not exist) about line 21 (second line in snippet below):

local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x = 60 crate.y = 20 crate.rotation = 0

While this is a pretty straightforward error, you might still verify it as follows:

local crate = display.newImageRect( "crate.png", 90, 90 ) print("create initialized? ", crate ) crate.x = 60 crate.y = 20 crate.rotation = 0

Now, if you get this in the console, you know there was a problem with the call to display.newImageRect():  

create initialized? nil

Then you could look at the file names and the way you called the function to see if there is a problem.

However, what if it printed out this?:

create initialized? table: 03CAECB0

This seems to imply the object was created and something else went wrong.  

Maybe you’re missing a semi-colon:

--crate.x = 60 crate.y = 20 crate.x = 60; crate.y = 20

Note: I’m not saying your code is wrong by the way, this is just a single example of using ‘print()’ to debug.

In closing, this is a basic example of using print to debug issues.   There are many other uses:

  • Print statements at entry point (first line) of functions to see you got in.
  • Print all passed arguments to a function. i.e. On first line, print name of function and all arguments to see that they were not nil.
  • Print statements just before return calls.  i.e. Print name of function and unique marker so you can later identify that as the point where the function was exited.
  • … The list goes on.

In short, there is no recipe for advance print statement placements.  

In fact, don’t put them in till you need them.  

When you do need them, put them in places to help you solve specific problems or answer questions.

I did what you said and it still doesn’t work . Here is what it says in the cmd prompt:

Windows simulator build date: Nov 18 2014 @ 18:35:32

Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2014.2511
WARNING: display.setStatusBarMode() not supported in the simulator for SM-G900S
device

Copyright © 2009-2014  C o r o n a   L a b s   I n c .
        Version: 3.0.0
        Build: 2014.2511
Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2014.2511
create initialized?     table: 02C2E110

Here is my main.lua code :

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local physics = require( "physics" ) physics.start() physics.setGravity(0, 15) -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145; ground.y = 480 ground.myName = "ground" physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) local crate = display.newImageRect( "crate.png", 90, 90 ) print("create initialized? ", crate ) crate.x = 60; crate.y = 20 crate.rotation = 0 physics.addBody( crate, "dynamic" , { friction=0.5, bounce=0.3 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isCrate and other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") end return true end crate.collision = onCollision crate:addEventListener( "collision" ) 

Here is restart.lua code:

local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view end -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145; ground.y = 480 -- display logo image local logo = display.newImageRect( "logo.png", 300, 200 ) logo.x = 160 logo.y = 20 group:insert(logo) group:insert(ground) scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )

What is the problem what corrections should I make.

Thank you.

Huh?

The way I understood your original post, you were saying, “Hey.  Folks have told me before I should use print() statements to debug code.  How would I do that, here is some code we can talk about.”

Now it seems you were trying to get us to fix the code?

I’m confused… nonetheless… try this code and the problem should be clear:

local composer = require( "composer" ) local physics = require( "physics" ) physics.start() physics.setGravity(0, 15) -- display ground image local ground = display.newImageRect( "ground.png", 500, 100 ) ground.x = 145 ground.y = 480 ground.myName = "ground" physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) local crate = display.newImageRect( "crate.png", 90, 90 ) crate.x = 60 crate.y = 20 crate.rotation = 0 physics.addBody( crate, "dynamic" , { friction=0.5, bounce=0.3 } ) local function onCollision( self, event ) local other = event.other print("onCollision()", event.phase, self.isCrate, other.isGround) if( event.phase == "began" and self.isCrate and other.isGround ) then timer.performWithDelay( 30, function() print("Calling composer open scene...") -- composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") end return true end crate.collision = onCollision crate:addEventListener( "collision" ) 

(Notice how nicely indented the above is…please indent your posts to make life easier for those who answer.  I also separated the logical segments into groups by removing blank lines, then reinserting them between unrelated bits of code.  It’s not perfect, but much more legible.)

Hint this is what prints:

onCollision() began nil nil onCollision() ended nil nil onCollision() began nil nil onCollision() ended nil nil onCollision() began nil nil

It seems to me that trueisawesome is having trouble at a more fundamental level than just the problem that’s in the code.

Unless I’m misunderstanding, you don’t know what people mean when they say “values”, so you don’t understand what they mean when they are telling you to “print out the values of the things you are testing and make sure you’re getting the values  you are expecting.”

trueisawesome, does that sound correct?

yes it does.

Thank you.

Values are things like numbers, strings, true/false, tables (actually a memory address to where the table is stored), functions (actually a memory address to where the function is stored).

Typically these values are stored in variables such as:

local myScore = 10

When someone asks you to print out the values and see if you are getting what you are expecting, the mean to insert a print statement like:

print( myScore )

or whatever variable you need to print and look in the terminal/console output window that we open and make sure you see values you expect to see.  In this case, print( myScore ) better print 10 since that’s what we set it to earlier.  When debugging your code where we have no idea what values you expect to be there, it’s helpful for you to print out the variables that you’re curious about and see what’s there.

BTW, it’s always more helpful to print out some additional information like the variable name you are printing:

print( "myScore", myScore)

prints:  myScore     10

In @roaminggamer’s example above he is printing out the string “onCollision()” so you know which line is doing the printing. He also prints out the collision phase (“began”, “ended”, etc.) and the values of the object’s .isCrate value and .isGround value. The reason he chose to print out those specific values is because you are doing a test:
 

if( event.phase == "began" and self.isCrate and other.isGround ) then

It is helpful to know what self.isCrate and other.isGround is where self and other are the two objects involved in the collision.  When the code runs, it says both values are nil.  At this point you should ask yourself why are they nil?  Then look at your code and see if you’re ever setting those values any where.

Rob