Skip to content

Using Script Variables

Robosturm edited this page Mar 4, 2022 · 2 revisions

What are Script Variables? Script Variables are Commander Wars way of adding variables to an object like a CO which are required over multiple function calls. For example the CO Mary needs to store for which buildings she gets a capture bonus. Several objects over you the option to create script variables. Here’s a list of the most important ones:

  1. Buildings
  2. Units
  3. CO’s
  4. Game-Scripts
  5. Weather-Scripts
  6. Campaign-Scripts

How do I use those variables? If you have a object that supports script variables call the getScriptVariables () function For example:

var variables = co.getVariables();

You can now access the variables either with createVariable(“VarName”) which will create the variable if it doesn’t exist else returns the existing one or getVariable(“VarName”) which may return a nullptr,but is a little bit faster. For example:

var variables = co.getVariables();
var counter = variables. createVariable(“counter”);

Will create the variable counter or return the existing variable. Accessing data from the variable:

var variables = co.getVariables();
var counter = variables. createVariable(“counter”);
var value = counter.readDataInt32();
value = value + 1;
counter.writeDataInt32(value);

This would increase the counter by one and store it in the object. Here’s a list of the most important write and read actions

  • writeDataListInt32/ readDataListInt32 writes/reads a list/vector of int32 to the variable
  • writeDataString/readDataString writes/reads a string tot he variable
  • writeDataInt32/readDataInt32 writes an int32 to the variable
  • writeDataUInt32/readDataUInt32 writes an uint32 to the variable
  • writeDataFloat/readDataFloat writes a float to the variable
  • writeDataBool/readDataBool writes a bool to the variable

Note: By writing a value to a variable the last stored type and data is discarded.

Clone this wiki locally