debugging php

Game development with Board Game Arena Studio
User avatar
Draasill
Posts: 197
Joined: 26 April 2020, 00:00

Re: debugging php

Post by Draasill »

Debugging can be a PITA =)

I mainly used an autocomplete snippet like :

Code: Select all

self::debug('<pre>' . var_export([
	'var1' => $var1,
	'var2' => $var2,
	'var3' => $var3,
], true) . '</pre>');
User avatar
docthib
Posts: 73
Joined: 10 August 2015, 14:05

Re: debugging php

Post by docthib »

Same, I added my own debug function ^^

Code: Select all

protected function dd($var, $die = false)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
        
    if ($die) {
        die();
    }
}
User avatar
developgame
Posts: 107
Joined: 15 June 2020, 20:56

Re: debugging php

Post by developgame »

Fusionpro wrote: 26 June 2020, 01:11 Not sure if I'm doing something wrong, but in the game.php file I was attempting to debug using:

var_dump('variable');

Turns out I was getting errors only when attempting to call var_dump in the setupNewGame() function. In order to check the results I had to move the call to getAllDatas() in order for the game to start - otherwise I would get halted with a

Unexpected error: Wrong formatted data from BGA gameserver 1 (method: createGame): ...

Just one example of things that have slowed my efforts at debugging PHP down. :?
Hi Fusion Pro

Not sure if you resolved your var_dump problem. I too am having problems with var_dump and other traces in php. I too have the same problem as you. I cannot do any var_dumps or traces in php except var dumps in getAllDatas().
I have tried the
self::dump( );
self::debug( );
self::trace( );

Theoretically, it is suppose to go to BGA request&SQL logs. I have never been able to find any of my traces there; However, I do not get syntax errors when using any of them outside setupNewGame() function

echo anywhere just gives me syntax errors. uuuuuuuuuuuuuuugggggggggggggggggghhhhhhhhhhhhhhhhhhhh!!!!!!!!!!!!!
So frustrating!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

It is so much easier with javascript with the console.log and console.dir. At least I could see what is happening

Thanks again, and all the best in 2021
Last edited by developgame on 23 November 2020, 20:20, edited 1 time in total.
User avatar
developgame
Posts: 107
Joined: 15 June 2020, 20:56

Re: debugging php

Post by developgame »

joezg wrote: 23 June 2020, 10:27 Here are a few tips I find useful for debugging a php code:
  • Use var_dump and die statements - you can always see output of these statements in javascript console in browser. Die statements are particularly useful when debugging some user action as their use will throw an error and rollback all the changes so you can repeatedly test same action. In your case you can put it at the beginning of setupNewGame method to see if it gets hit.
  • For debugging algorithms above wouldn't usually work good. I tend to encapsulate all game logic in php modules (you can put them in modules directory). As they are not coupled with BGA system and database I can debug them with local instance of php with xdebug enabled. You can see examples of it in my railroadink project. I have a couple of modules in modules directory and I test them with testBoard.php script in misc directory.
With this two techniques I tend to work smoothly on php code.

For your error, is there anything useful in error message you get? Remember that you can see php errors in javascript console when they disappear from the screen. Maybe there is some error in dbmodel.sql. Try to simplify code by removing most of it and then you can put it back chunk by chunk until you find the problematic code.
Thanks for your response, joezg. I just saw it, apologies for my late response to you.
Thanks for your suggestions. I will try it. It is really frustrating debugging with php on this BGA frame work.
I look forward to playing your railroadink game. Thanks for developing it. All the best in 2021
Last edited by developgame on 23 November 2020, 20:20, edited 1 time in total.
User avatar
developgame
Posts: 107
Joined: 15 June 2020, 20:56

Re: debugging php

Post by developgame »

Draasill wrote: 26 June 2020, 07:59 Debugging can be a PITA =)

I mainly used an autocomplete snippet like :

Code: Select all

self::debug('<pre>' . var_export([
	'var1' => $var1,
	'var2' => $var2,
	'var3' => $var3,
], true) . '</pre>');
Thanks for your response Draasill! apologies for my late response. I just saw this. I have to look into this more. It looks like it can solve my problem! Thanks. All the best in 2021
Last edited by developgame on 23 November 2020, 20:19, edited 1 time in total.
User avatar
developgame
Posts: 107
Joined: 15 June 2020, 20:56

Re: debugging php

Post by developgame »

docthib wrote: 26 June 2020, 09:40 Same, I added my own debug function ^^

Code: Select all

protected function dd($var, $die = false)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
        
    if ($die) {
        die();
    }
}
Thanks for your response, docthib! apologies for my late response. I just saw your response. I will give this a try.
Thanks again. All the Best in 2021
User avatar
RavingWanderer
Posts: 83
Joined: 12 May 2020, 16:08

Re: debugging php

Post by RavingWanderer »

Diagnosing game setup problems is to me one of the biggest hassles in BGA. Site admins, please find a way to capture diagnostics that are not currently available (i.e. all of those before the first active player state).

Tracking them down is possible. As others have posted, die() helps, but here are also a few development strategies that also help:

1. Get a working simple setup (all the way back to a basic tutorial game setup if you have to).
2. Make very small incremental changes, which you can back out if they cause a problem. (This also works in Javascript.) Scrutinize problem changes for things like parameter typing and unexpected type conversion, which can be silent evils in PHP and JS.

If you have a working game setup (enough to get to a first active player) but Something Is Wrong, add a database table where you can log your own information. You can then examine its contents in the database console for clues to your problems once the game reaches that stable state.
Post Reply

Return to “Developers”