1

I'm working on converting a lua program into a C++ program but I've hit a road block, I can't figure out how to convert this into C++

function newPool()
    local pool = {}
    pool.species = {} --imports data from local species = {}
    pool.generation = 0
    pool.innovation = Outputs
    pool.currentSpecies = 1
    pool.currentGenome = 1
    pool.currentFrame = 0
    pool.maxFitness = 0

    return pool
end

I know many basics of both languages and i know it works in lua but i need it in C++. Can anyone help me?

1
  • well isnt pool a struct here? just declare a struct and create some kind of constructor function and initialise this struct and give back its pointer. Be sure to alloc the struct within the constructir otherwise itll be only local for the function scope and its adress is a dangling pointer ur giving back. Commented Mar 11, 2016 at 6:04

1 Answer 1

1

Lua has something called Tables which allows you to add key-value pairs without a predefined struct as in C/C++. So the Lua code you posted is adding key-value pairs to pool (read comments in code):

local pool = {}           -- Declare a new Table
pool.species = {}         -- Add another Table to pool called 'species'
pool.generation = 0       -- Add the key 'generation' with value '0'
pool.innovation = Outputs -- Add the key 'innovation' with value 'Outputs'
pool.currentSpecies = 1   -- Add the key 'currentSpecies' with value '1'
pool.currentGenome = 1    -- Add the key 'currentGenome' with value '1'
pool.currentFrame = 0     -- Add the key 'currentFrame' with value '0'
pool.maxFitness = 0       -- Add the key 'maxFitness' with value '0'

In C++ you have several options. 1) you can create a struct and declare what you need (I'm guessing on some datatypes but if you have the full Lua program you can figure them out):

struct Pool
{
   Species species;     // You'll have to define Species in another struct
   int generation;
   SomeEnum innovation; // You'll have to define SomeEnum in an enum
   int currentSpecies;
   int currentGenome;
   int currentFrame;
   int maxFitness;
}

If you have a class then you can use the struct Pool shown below (add the struct Pool definition from above to the .h file below above class Kingdom):

// I'm doing this as a class since you are programming in C++ and I
// assume you will want to add more functions to operate on similar
// objects.
class Kingdom
{
public:
   Kingdom();
   Pool* NewPool();
private:
   Pool _pool;
}

In your .cpp file:

#include "Kingdom.h"

Kingdom::Kingdom()
{
  // _pool.species = whatever you define struct Species as
  _pool.generation = 0;
  _pool.innovation = SomeEnum::Outputs; // You'll have to define SomeEnum
  _pool.currentSpecies = 1;
  _pool.currentGenome = 1;
  _pool.currentFrame = 0;
  _pool.maxFitness = 0; 
}

Pool* Kingdom::NewPool()
{
  Pool* newPool = new Pool;
  memcpy(newPool, &_pool, sizeof(Pool)); // Make a copy
  return newPool;                        // Return the new copy

  // The newPool value is dynamic memory so when the calling function is done
  // with newPool it should delete it, example:
  // Kingdom myKingdom;
  // Pool* myNewPoolStruct = myKingdom.NewPool();
  // ... do some coding here
  // delete myNewPoolStruct;
}

Option 2) would be if all of your key-value pairs were the same type; i.e. all keys were std::string and all values were int. Remember, the Lua code is using Tables so you can create the equivalent in C++ with std::map<>. Then you could use std::map<std::string, int> as follows:

// In your .h file change
Pool* NewPool();
Pool _pool;
// to
std::map<std::string, int> NewPool();
std::map<std::string, int> _pool;

Then in your .cpp file change the constructor to:

Kingdom::Kingdom()
{
  _pool["species"] = 0;    // Some int representation of species
  _pool["generation"] = 0;
  _pool["innovation"] = 1; // Some int representation of Outputs
  _pool["currentSpecies"] = 1;
  _pool["currentGenome"] = 1;
  _pool["currentFrame"] = 0;
  _pool["maxFitness"] = 0;
}

std::map<std::string, int> NewPool()
{
  std::map<std::string, int> newPool;
  newPool = _pool;  // Copy - double check this against std::map
  return newPool;   // Double check this is a true copy and not a pointer
}

With std::map you can create key-value pairs on the fly just like the Lua code you provided. In short, I'd go with the struct Pool approach because with std::map<> you'll have to remember strings which isn't good practice and your IDE should have intellisense which will always show you the contents of struct Pool whenever you hit the . or -> operators.

Sign up to request clarification or add additional context in comments.

2 Comments

can assume that the setup is the same for newSpecies() and newGenome()? both of which and a few more use the same lua setup but with different values.
You can assume the same. Create a struct for both Species and Genome and inside of struct Pool add them as I have shown in the code above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.