So I'm new to Lua and I've got LuaJava running with my Java code, but I'm stuck trying to figure out how to run a foreach loop on a hashmap in Lua.
In my java code I have this:
Java
public class EntityManager {
public static EntityManager EntityManager = new EntityManager();
private Map<Integer, Entity> entities = new HashMap<Integer, Entity>();
public Map<Integer, Entity> entities() {
return entities;
}
....
}
public class Entity {
private int id;
....
public int getId() {
return id;
}
public EntityManager getManager() {
return EntityManager.EntityManager;
}
}
Then I've got my lua script and I'm trying to figure out how to iterate through all entities in the hashmap in EntityManager:
Lua
owner = {} -- I set this to an Java object that is a child of the Entity class
function doSomeStuff()
for i, e in pairs(owner:getManager():entities()) do
if (e:getId() ~= owner:getId()) then
-- Do some stuff here
end
end
end
I've pretty much got everything I need set up for my functions, etc. to work but I don't know how to go about creating a Lua equivalent of a Java foreach loop, to iterate through my Java hashmap in my Lua script.
Also I'd like to know how I can check the type of an object in Lua to see if it's an instanceof one of my Java classes. For example, in Java if I wanted to see if an object is of an object type I would do the following:
Entity e;
....
if (e instanceof EntityTypeA) {
// e is of Entity type A
} else if (e instanceof EntityTypeB)
// e is of Entity type B
}
Oh, and I'm not entirely sure if the Java object I'm passing into the Lua script is retaining it's Java class type, nor do I know how to go about doing such a thing.
By the way, I'm using LuaJava from Kepler Project.
Thanks.