0

So im writing a script for the computer game Counter Strike: Global Offensive that changes a players crosshair ingame. the language the games engines uses is called Vscript. Vscript is not very well documented, with the only documentation i could fined being a long list of functions. But after trial an error, I found the syntax is almost identical to JavaScript, with the only real diffrences being that there are specific functions in the language like SetHealth or GetTeam for example that are specific to the game and dont exist in JavaScript.

class crosshair_generator {
    // Initialize
    crosshair_hidden = false;

    function hideCrosshair() {
        if (crosshair_hidden == true) {
            // Hide the crosshair 
            SendToConsole("crosshair 0");
            printl("crosshair hidden!");
            self.crosshair_hidden = false;

        } else if (crosshair_hidden == false) {
            // Show the crosshair
            SendToConsole("crosshair 1");
            printl("crosshair shown!")
            crosshair_hidden = true;

        }
    }

    function CrosshairAlpha(Add) {
        // cl_crosshairalpha
    }
} 

when i open the game and run the script, inside the games console it says

AN ERROR HAS OCCURED [the index 'crosshair_generator' does not exist]

CALLSTACK
*FUNCTION [main()] InputRunScript line [1]

LOCALS
[this] TABLE
 Entity script_crosshair encountered an error in RunScript()

If i define the crosshair_hidden variable inside the hideCrosshair() function crosshair_hidden = false; then every time i call the function it's value would be false. i need it to be defined outside the function, so that the variable can be accessed by other functions like CrosshairAlpha(), and so the variable value is not the exact same every time i call the function.

So how do you define a variable in a class in JavaScript, then pass that variable into a function?

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • where and How do you initialize the crosshair_generator class? to pass a variable into a class method you have to set this var globally, or add a constructor method into the main class so you can pass it while you are doing `new crosshair_generator(your_params)` – Sim1-81 Sep 23 '19 at 13:03
  • very first line of the code ```class crosshair_generator {``` it's initialised there –  Sep 23 '19 at 13:05
  • That's not what initialized means. Where are you instantiating it? ie `new crosshair_generator();` Also, class properties are a very new part of Javascript syntax, are you sure they're supported in Vscript? – Jared Smith Sep 23 '19 at 13:06
  • @Ironstone1_that is the declaration of class not the new instance – Sim1-81 Sep 23 '19 at 13:08
  • the game was released in 2012, so it might not support class properties then. So to initialize the class i should type ```new crosshair_generator();``` above the ```class crosshair_generator {``` line? –  Sep 23 '19 at 13:09
  • take a look [here](https://javascript.info/class) – Sim1-81 Sep 23 '19 at 13:10
  • The language is [squirrel](https://developer.valvesoftware.com/wiki/Squirrel), not Javascript. I've changed the question tagging. Here's the [documentation](http://squirrel-lang.org/doc/squirrel3.html). Good luck. – Jared Smith Sep 23 '19 at 13:12

1 Answers1

1

You can initialize the properties in the construct method and refer to them later using this:

class crosshair_generator {
    // Initialize
    constructor(crosshair_hidden = false) {
        this.crosshair_hidden = crosshair_hidden;
    }

    hideCrosshair() {
        if (this.crosshair_hidden == true) {
            // Hide the crosshair 
            SendToConsole("crosshair 0");
            printl("crosshair hidden!");
            this.crosshair_hidden = false;

        } else if(this.crosshair_hidden == false) {
            // Show the crosshair
            SendToConsole("crosshair 1");
            printl("crosshair shown!")
            this.crosshair_hidden = true;

        }
    }

    CrosshairAlpha(Add) {
        // cl_crosshairalpha
    }
}