title: Using a method on a custom named (IE named at runtime) property within Bucklescript
body: <p>So, I'm trying to program an AI for the game Screeps, whose docs are found <a href="https://docs.screeps.com/api" rel="nofollow noreferrer">here</a>. </p>
  
  <p>I'm trying to write my AI in OCaml, which I'm then compiling to Javascript via <strong>Bucklescript</strong>, whose docs are found <a href="http://bucklescript.github.io/bucklescript/Manual.html" rel="nofollow noreferrer">here</a>. </p>
  
  <p>Anywho, within Screeps' API is the method <code>Game.spawns.SPAWN_NAME.createCreep</code>, in which <code>SPAWN_NAME</code> corresponds to the name of the 'spawn' object in question. It takes in a string array corresponding to the various body parts of the 'Creep' it is helping to spawn, and given a correct function call (with enough energy reserves), your Creep will spawn in game.</p>
  
  <p>An example call (in JS) would be <code>Game.spawns['Spawn1'].createCreep(["body","move"]);</code></p>
  
  <p>I already have code which gives me a <code>string array</code> of all the spawns in OCaml. That code is:</p>
  
  <pre><code>let spawns : string array = [%bs.raw{|Object.keys(Game.spawns)|}]
  let spawnsArray : string array = spawns
  </code></pre>
  
  <p>Lets say that I have one spawn called <code>Spawn1</code>, and that  I also have a string array for the body composition in OCaml:</p>
  
  <pre><code>let spawnName : string = "Spawn1" 
  let body : string array = [|"body","move|]
  </code></pre>
  
  <p>I then iterate over each string within that array, using a for loop like the one below:</p>
  
  <pre><code>for i=0 to Array.length spawns - 1 do
    // I WANT TO CALL SOMETHING ANALOGOUS TO MY FUNCTION HERE
  done
  </code></pre>
  
  <p>I just can't, for the life of me, figure out how to format the Bucklescript bindings so that I can dynamically call the <code>createCreep</code> function with <code>body : string array</code> and <code>spawnName : string</code>. Any help would be amazing. I know there are the <code>bs.get</code> and <code>bs.set</code> methods described briefly within the Bucklescript docs, but I don't know how to use them.</p>
  
  <p>Thanks in advance for the help.</p>
  
  <hr>
  
  <h2>EDIT:</h2>
  
  <p>I managed to 'work around' the issue by writing my own 'interfacing' functions in a Javascript module which I can then call via the <code>bs.module</code> Bucklescript binding.</p>
  
  <p>IE I wrote a function </p>
  
  <pre><code>function spawnCreepHelper(spawnName, body) {
    Game.spawns[spawnName].createCreep(body);
  }
  </code></pre>
  
  <p>which I'm then able to call via </p>
  
  <pre><code>external spawnCreepHelper : string -&gt; string array -&gt; unit = "" 
  [@@bs.module "./supplemental", "Supplement"]
  </code></pre>
  
  <p>Seems kind of hacked together to me, so if anyone has another way of approaching it that doesn't involve rewriting their API myself, please let me know.</p>