Add value via Bitfocus COMPANION for scoreboard

Hello, I am currently a French student in broadcast engineering (sorry for my approximate grammar). I am currently working on a game project in which I created an HTML template for the score. But here I would have liked to create a key in Companion with a function that adds +1 to the score. I looked on the side of the HTML code of the template but I don’t think this is the right solution. So I’m leaning more towards sending a request from companion to the server, but here I have no knowledge, can someone guide me on a sequence of commands or logic to add +1 to a value already existing in the template?

First of all: Does your templates code support setting values via Caspars commands like CG ADD or CG UPDATE? In other words, can you play the template using the official standard CasparCG client app and change the score? If that works you only need to add a function, that adds 1 to the existing value. Test it using a CG INVOKE from the official client, and copy the command, that it issues, from the console or log file. You can then add the Caspar connector to Companion and make it a custom AMCP command with the code copied earlier. Don’t forget to add a similar command to subtract one from the value, to correct a mistake.

Yes my HTML template, supports the addition of values. I created a new AMCP command (CG 1-10 INVOKE 1 “TEMPLATENAME”) but it does not affect any of the two values present in the template. How do I proceed?

Yes my HTML template, supports the addition of values. I created a new AMCP command (CG 1-10 INVOKE 1 “TEMPLATENAME”) but it does not affect any of the two values present in the template. How do I proceed?

I found this command line on the GITHUB HELP.

CG [video_channel:int]{-[layer:int]|-9999} INVOKE [cg_layer:int] [method:string]

But I don’t understand the method tab and how to dissociate the addition of on the 2 different values?

That looks wrong. In your templates JavaScript you need to define a function:

function AddHome() {
   //Add 1 to your score for the home team and make it visible
}

Then you need to CG ADD or CG PLAY your template first, so that it is showing on your output. By issuing

CG 1-10 INVOKE 1 “AddHome”

You trigger your function. This command can then be sent from a Companion button.

Okay, the INVOKE command apply a JavaScript function include in the template.

So i have add this code in my templates :

function addteam1 () {TEAM1+1}

TEAM1 is the value of my Team1 score text

After i launch a CG ADD command and the INVOKE command :

CG 1-10 INVOKE 1 “addteam1"

But nothing happen, I think my problem comes from the Java Script, should I define a variable “var a=1” and then define a function that adds this variable to my TEAM1 value? I also don’t know if my function is well placed within my HTML tree structure?

Anyway thank you very much for helping me on this project, I’m learning a bit more about how CASPAR CG commands work

You need to be sure, that you update not only a variable in code but also the text object in the HTML. Like the content of a div or whatever it is.

Here is a example with one way to do it. In the html part of the template, make a div with id:

 <div id="team1">0</div>

And in the script part have this function:

function addScore(teamId, addValue) {
            let element = document.getElementById(teamId);
            let score = element.innerHTML;
            let newScore = Number(score) + addValue;
            element.innerHTML = newScore;
        }

and then trigger this from a companion button:(if template loaded on layer 10):

CG 1-10 INVOKE 1 “addScore(‘team1’,1)”

If you want a button to subtract i could be like this:

function subtractScore(teamId, addValue) {
            let element = document.getElementById(teamId);
            let score = element.innerHTML;
            let newScore = Number(score) - addValue;
            element.innerHTML = newScore;
        }

and a companion button with:
CG 1-10 INVOKE 1 “subtractScore(‘team1’,1)”

I hope that helps.