Page 1 of 3

database write delay ?

Posted: 30 May 2020, 16:08
by tchobello
hello

I'm struggling with a value just updated in DB.
I need this value as an argument in the State CombatChoices.
PHP

Code: Select all

$sql = "UPDATE token SET token_specialpower_counter='-10' WHERE token_id='".$part_token['id']."
self::DbQuery( $sql );
$twoHandedSword ++;
																			
if( $twoHandedSword > 0 ){
	 $this->gamestate->nextState( 'combatchoices' );

    function argCombatChoices()    {
        $token = self::getObjectListFromDb( "SELECT token_id id, token_type type FROM token WHERE token_specialpower_counter ='-10' ");
	$nb_tokens = count($token);
	$token_type = $token[0]['type'];
        return array(
            'nb_tokens' => $nb_tokens,
            'token_type' => $token_type
			);
JS returns 'Cannot read property 'token_type' of null'
and it's working perfectly when hitting F5...

Can someone explain to me what is happening here ?

Re: database write delay ?

Posted: 30 May 2020, 17:44
by Tisaac
This is not an issue with the db but more like an issue with the js onEnteringState (that is solved when refreshing since it's the setup fonction that handle it).
Can you post the corresponding states.php and .js ?

Re: database write delay ?

Posted: 30 May 2020, 19:07
by tchobello
Thanks for answering

STATES

Code: Select all

    4141 => array(
        "name" => "combatChoices",
        "description" => clienttranslate('${actplayer} must choose a Combat option'),
        "descriptionmyturn" => clienttranslate('${you} must choose a Combat option'),
        "type" => "activeplayer",
        "args" => "argCombatChoices",
        "possibleactions" => array( "chooseCombatOption" ),
        "transitions" => array( "chooseCombatOption" => 48 )
PHP

Code: Select all

    function argCombatChoices()
    {
        $token = self::getObjectListFromDb( "SELECT token_id id, token_type type FROM token WHERE token_specialpower_counter ='-10' ");
		$nb_tokens = count($token);
		$token_type = $token[0]['type'];
        return array(
            'nb_tokens' => $nb_tokens,
            'token_type' => $token_type
	    );
	}
JS ONUPDATEACTIONBUTTONS

Code: Select all

         case 'combatChoices':
			if( args.token_type == 'twohandedsword' ){
				for( var i=0;i<=args.nb_tokens;i++ ){
					this.addActionButton( 'combatchoice_'+args.token_type+'_'+i, dojo.string.substitute( _("Use ${nb} Two-Handed Sword(s)"), {nb: _(i) } ), 'onCombatChoice' );
				}
		        }
		        break;

Re: database write delay ?

Posted: 30 May 2020, 19:31
by Benoit314
Did you try with args.args.token_type?

Re: database write delay ?

Posted: 30 May 2020, 20:08
by tchobello
Benoit314 wrote: 30 May 2020, 19:31 Did you try with args.args.token_type?
yep.
does not work...

Re: database write delay ?

Posted: 30 May 2020, 20:55
by Benoit314
If you use console.log(), what values are returned in args?

Re: database write delay ?

Posted: 30 May 2020, 22:38
by tchobello
Benoit314 wrote: 30 May 2020, 20:55 If you use console.log(), what values are returned in args?
null at first
twohandedsword (as expected) after F5

Re: database write delay ?

Posted: 30 May 2020, 23:09
by Lymon Flowers

Code: Select all

				for( var i=0;i<=args.nb_tokens;i++ ){
Isn't the <= problematic? You should probably use "i<args.nb_tokens" instead.

Re: database write delay ?

Posted: 31 May 2020, 12:22
by DrKarotte
Maybe the args are not available in the updateActionButtons, I think I had some trouble with that.
I stored the info I need during the enteringState in a global variable.

Apart from that it is always args.args.xyz, unless you define var args=args.args;

Re: database write delay ?

Posted: 31 May 2020, 13:26
by tchobello
DrKarotte wrote: 31 May 2020, 12:22 Maybe the args are not available in the updateActionButtons, I think I had some trouble with that.
I stored the info I need during the enteringState in a global variable.

Apart from that it is always args.args.xyz, unless you define var args=args.args;
I need the arguments only for displaying buttons. Therefore, I only use the OnUpdateActionsButton part.
I think it's args.args.xyz in OnEnteringState and args.xyz in OnUpdateActionsButton...