• entries
    437
  • comments
    1000
  • views
    335834

foreach

Sign in to follow this  

331 views

It's in, it's done... I've added the foreach extension to GameMonkey Script. I'm very pleased how it's turned out, actually.

There's two variants. The basic foreach() with a code block and one without the code block that stashes the items into a table.


foreach(item in items where())
{
// code
}


foreach(item in items where() into results);


So you can now write 'queries' such as:


entities = {
{ id=1, name="Glador", class="wizard", mana=100, hp=20 },
{ id=2, name="Bahr", class="warrior", mana=0, hp=100 },
{ id=3, name="Gandalf", class="wizard", mana=120, hp=50 },
{ id=4, name="SuperWizard", class="wizard", mana=150, hp=50 }
};


foreach(e in entities where (e.mana >= 100))
{
print(e.name, "has lots of mana");
}

foreach(e in entities where (e.class != "wizard"))
{
print(e.name, "is not a wizard");
}

foreach(e in entities where (e.class == "wizard") into wizards); // get all items that match into a table called 'wizards'

foreach(w in wizards where (e.hp > 30)) // query this new table
{
print(w.name, "has enough strength");
}




Very cool :)
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now