Storing information that is consistent between turns

How would we go about to store information/variables that can be changed during the deployment phase, but is consistent between turns? Like for example I want to assign a variable telling the program what ‘mode’ the algo is to perform in, and the algo must perform that ‘mode’ until something triggers to change the variable to change modes.

1 Like

Ah, what you are describing is a finite state machine (FSM). One possible implementation would be to create a separate class designed to be the FSM and call that class’s update function (or whatever you want to call it). Then, handle all of the state logic and decision making inside that class. The class could also then hold any state related data.

I have actually used this in my own algos and I recommend it. You can store things like “mode” variables right in the algo_strategy file that everything else is done in. Since the program modifies the “AlgoStrategy” class, just declare class variables, then reference or change them from any class function as “self.variableName”. Since your AlgoStrategy class is instantiated only once at the beginning of the match, these variables are continuous throughout the entire game.

So i should create a new class separate from the AlgoStrategy class? And whatever variables i put in there won’t change after each match?

You can do everything in the AlgoStrategy class. Add the class variables to that.

For example, lets say the start of AlgoStrategy looks like this:

class AlgoStrategy(gamelib.AlgoCore):

def __init__(self):

    self.mode = 0

def on_game_start(self, config):

Then anywhere in the class functions, you can change the mode variable:

def on_turn(self, turn_state):
        self.mode = 1

What if, say, I wanted variables, information and such saved during the match, and never deleted, so that with each match my program would amass information. Is it possible to have my algo do that, or does it get reset at the start of the match to what I uploaded in the first place?
Also, would there be any way to then download the modified algo back on to my PC?

When running locally you would save state by writing out to a file.

When running on the server though I don’t think there is anyway to save anything? But it would be nice if we could get at whatever our algorithm printed out through the logging module for instance.

I’m not sure about the saving between games to a file on the server, one of the devs will have to answer that.

Regarding saving data within a game it should be noted that while you can do everything inside of the AlgoStrategy class, I prefer to modulate my code so I can store different data respective to where it is related. Simply putting data into your main class is not a problem at first, but as your programs get larger and more complicated it is generally good practice to break it into components.

This is an object-oriented programming perspective, but it can really help keep your code clean.

So for example, rather than creating a bunch of functions in my AlgoStrategy class to make decisions, you could make a class called FSM (Finite State Machine) that stores all data related to the current state and the functions to decide which path to follow based on that state. This differentiates it from the main program and prevents cluttering of code.

1 Like

If its a variable you want to save between turns that’s easy. Just make a class variable. You can initialize the class variable either in __init__ or on_game_start and change it where ever using self.

def on_game_start(self, config):
        Read in config and perform any initial setup here 
        self.mode = 0

def on_turn(self, turn_state):
        This function is called every turn with the game state wrapper as
        an argument. The wrapper stores the state of the arena and has methods
        for querying its state, allocating your current resources as planned
        unit deployments, and transmitting your intended deployments to the
        game engine.
        game_state = gamelib.GameState(self.config, turn_state)
        gamelib.debug_write('Performing turn {} of your custom algo strategy'.format(game_state.turn_number))
        #game_state.suppress_warnings(True)  #Uncomment this line to suppress warnings.

        self.mode = self.mode + 1

You can’t save information between games on our servers.

can the algo store info from the past game to use for the next game?

No because all files in your zip get reset every game.

1 Like