How to call a function in view.php from game.php

Game development with Board Game Arena Studio
Post Reply
User avatar
GoDodyGo
Posts: 42
Joined: 14 August 2020, 07:30

How to call a function in view.php from game.php

Post by GoDodyGo »

I have this code in my view.php file. It puts player names onto the board:

Code: Select all

	$template = self::getGameName() . "_" . self::getGameName();
        
        // this will inflate our goDownArea block with actual players data
        $this->page->begin_block($template, "goDownArea");
		
        foreach ( $players as $player_id => $info ) {
            $this->page->insert_block("goDownArea", array ("PLAYER_ID" => $player_id,
                    "PLAYER_NAME" => $players [$player_id] ['player_name'],
                    "PLAYER_COLOR" => $players [$player_id] ['player_color']
	    ));
        }
However, after each hand ends, I clear the board and the text disappears, which is bad. It seems view.php is not run again, which has this code.

So:
1) How can I get this view.php code to run from some function call in game.php after clearing the board?

2) Or, can I put a modified version of the above code into game.php so it will insert that stuff into the page?? How would the references change?

3) Or, how in game.php can I do the equivalent of having the user press F5, which seems to rerun the view.php file?
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: How to call a function in view.php from game.php

Post by Tisaac »

Short answer : you should not try to do that. The purpose of the view is to display things that is not dynamic.
If you need something dynamic, use js templating. I actually always have almost empty view file and tpl, everything is constructed via js.
User avatar
GoDodyGo
Posts: 42
Joined: 14 August 2020, 07:30

Re: How to call a function in view.php from game.php

Post by GoDodyGo »

Thanks for the reply. I got it working in the way you suggested. I don't have any more questions on this. I'm posting in the chance that this might be helpful to others (much of this is in the documentation also). To those considering something like what did, don't do it that way :-)

Instead of having the view.php file print names onto the board, I have changed my code. Note that the variable 'player' is the player ID, such as 3163742:

This TPL code defines areas on the page:

Code: Select all

<div id="downArea">
	<!-- BEGIN goDownArea -->
		<div id="playerDown_A_{PLAYER_ID}" class="downWhite"></div>
		<div id="playerDown_B_{PLAYER_ID}" class="downWhite"></div>
		<div id="playerDown_C_{PLAYER_ID}" class="downWhite"></div>
	<!-- END goDownArea -->
</div>
And this JS code is used to set the text to the player names. It is run in F5 / refresh page function and also after the board is cleared after each hand (my game is a card game where cards end up all over the place at the end of each of several hands):

Code: Select all

// Add playername text to down areas
$("playerDown_A_"+ player).innerHTML = this.gamedatas.players[ player ][ 'name' ];
$("playerDown_B_"+ player).innerHTML = this.gamedatas.players[ player ][ 'name' ];
$("playerDown_C_"+ player).innerHTML = this.gamedatas.players[ player ][ 'name' ];
And this code in my view.php file (which expands the above TPL block onto the page):

Code: Select all

$this->page->begin_block($template, "goDownArea");
foreach ( $players as $player_id => $info ) {
    //$dir = array_shift($directions);
    $this->page->insert_block("goDownArea", array(
        "PLAYER_ID" => $player_id,
	"PLAYER_NAME" => $players [$player_id] ['player_name'],
	));
}
Post Reply

Return to “Developers”