Page 1 of 1

Using bgaPerformAction with a Array as a arg

Posted: 08 July 2024, 08:12
by tony58uk
Hi there,

I'm having an issue I'd like help with please. I'm trying to pass an array through the bgaPerformAction method but getting the error:

Bad argument type: dice_array should be a value from the enumeration (5_7_apep)

Can arrays not be passed through? I cannot find an answer anywhere...

If I cannot, then guessing I should send through a string with implode?

Thanks for the help.

Code below

Code: Select all

       onConfirm: function ( evt ) {

            
            console.log('onConfirm');
            dojo.stopEvent (evt);

            if (this.diceToPlay > 0) {
                this.showMessage("You still need to place a die", 'info');
            } 
            else {
                console.log("Confirm Succesful");
                // clear the Dice shown
                
                
                
                arrayLength = this.placedDieThisTurn.length;
                this.clearRolledDie();
                console.log("Array Length = " + arrayLength);
                if (! this.checkAction('playedTwoDiceSetup')) {   //some shit here went wrong!!!
                        console.log("RETURN HAPPENED");
                        
                        return;
                }
                
                
                     //send placed die to AJAX called back to php
                    console.log("AJAX " + this.diceToPlace);

                    this.bgaPerformAction('playedTwoDiceSetup', 
                                {dice_array: this.placedDieThisTurn, die_type:this.diceToPlace},
                                {lock:true});
                    
                    console.log("playedTwoDiceSetup completed??");                           
            };
            


        },


Re: Using bgaPerformAction with a Array as a arg

Posted: 08 July 2024, 08:27
by thoun
You'll need to pass the array like this :

Code: Select all

this.bgaPerformAction('playedTwoDiceSetup',  {dice_array: this.placedDieThisTurn.join(','), die_type:this.diceToPlace});
(I also removed locked: true because it is by default).

On the action.php side, you'll read it this way :

Code: Select all

        $dice_array_str = $this->getArg("dice_array", AT_numberlist, true);
        $dice_array = array_map(fn($str) => intval($str), explode(',', $dice_array_str));
(beware of the empty list if it can happen, it isn't handled in this example)

Re: Using bgaPerformAction with a Array as a arg

Posted: 08 July 2024, 09:26
by tony58uk
Thanks!!! I worked it out from your examples. I thought I might have to implode and then explode the arg. It contains letters so doing it is slightly different. Also, you have to use ' ' (space) as a ',' isn't allowed in the AT_alphanum arg.

All sorted now cheers!!!!!


JS code

Code: Select all

 console.log("AJAX " + this.diceToPlace);

                    this.bgaPerformAction('playedTwoDiceSetup', 
                                {dice_array: this.placedDieThisTurn.join(' '), die_type:this.diceToPlace},
                                {lock:true});
                    
                    console.log("playedTwoDiceSetup passed" + this.placedDieThisTurn.join(' ')); 
action.php

Code: Select all

   public function playedTwoDiceSetup() {

      $this->setAjaxMode();
      $played_dice_str = $this->getArg("dice_array", AT_alphanum, true);
      $played_dice = explode(' ', $played_dice_str);
      $die_type = $this->getArg("die_type", AT_alphanum, true);
      $result = $this->game->playedTwoDiceSetup( $played_dice, $die_type);
      $this->ajaxResponse( );

    }