EnumMap with LuaJava (attempt to call a nil value)

236 Views Asked by At

As the title says, I have problem with LuaJava and EnumMap. I'm trying to do a RPG Library, so we have a Character with some Attributes and a list of them. In the code below, I'll report only important things.

I have a class called ConcreteAttribute that extends AbstractAttribute, that has a constructor with a String and two int.

public abstract class AbstractAttribute {

protected String name;
protected int baseValue;
protected double baseMolt;

public AbstractAttribute(String name, int valoreBase, double moltBase){
    this.name = name;
    this.baseValue = valoreBase;
    this.baseMolt = moltBase;
}

/*Getters and Setters*/

}

Then I have an Enum called StatType, that represents the stat you have in RPG games:

public enum StatType {
HP, MP, Attack, Defense;
}

I have also a class called PlayableCharacter, that has (I'll report only important things):

public abstract class PlayableCharacter extends Character{

protected EnumMap<StatType, ConcreteAttribute> statistiche;

//Constructor

public EnumMap<StatType, ConcreteAttribute> getStatistiche(){
    return statistiche;
}

and a "put" method from Map.

To create a new character, I'm currently doing this ("Eroe" extends PlayableCharacter):

    public static void main(String[] args) {

    PlayableCharacter pers = new Eroe("Eroe 1");
    ConcreteAttribute atk = new ConcreteAttribute("Attacco", 20, 0);
    pers.getStatistiche().put(StatType.Attacco_Fisico, atk);

}

and works fine, but I don't think it is the best solution when you have a lot of ConcreteAttribute and a lot of Character. So I was thinking to use Lua to create "concrete" istance of Character, this way (little example):

-- Eroe 1.lua
function create(eroe)
    eroe:setName("Eroe 1")
    attributes =  luajava.bindClass("personaggi.attributi.StatType")
    attacco = luajava.newInstance("personaggi.attributi.ConcreteAttribute", "Attacco", 20, 0) 
    eroe:getStatistiche():put(attributes.Attacco_Fisico, attacco)
 end

but I get this error: PANIC: unprotected error in call to Lua API (attempt to call a nil value). The problem is with the last line, but I'm really new to Lua and I don't even know if Lua can handle EnumMap.

Any solution to this?

Thanks to all and sorry for my bad English ^^

EDIT: I've edited the code, I found an error

1

There are 1 best solutions below

0
On BEST ANSWER

An enum constant is public member of its enum class, but you are accessing it like a method using :.

So instead of attributes:Attacco_Fisico you need to use attributes.Attacco_Fisico.