1

I have a fairly simple nested table Aurora64.Chat containing a couple of functions (where the main Aurora64 class is initialized elsewhere but I inserted it here for completeness):

Aurora64 = {};

Aurora64.Chat = {
    entityId = 0;
    Init = function()
        local entity; --Big long table here.
        if (g_gameRules.class == "InstantAction") then
            g_gameRules.game:SetTeam(3, entity.id); --Spectator, aka neutral.
        end
        entityId = Entity.id;
        self:LogToSystem("Created chat entity '" .. entity.name .. "' with ID '" .. entity.id .. "'. It is now available for use.");
    end

    LogToSystem = function(msg)
        System.LogAlways("$1[Aurora 64]$2 " .. msg);
    end
}

The above code fails (checked with the Lua Demo) with the following:

input:14: '}' expected (to close '{' at line 3) near 'LogToSystem'

I have tracked it down to the LogToSystem function and its usage (if I remove the function and the one time it is used, the code compiles perfectly), and I thought it was to do with my use of use of concatenation (it wasn't).

I'm thinking I might have missed something simple, but I checked the documentation on functions and the function & its call seem to be written properly.

What exactly am I doing wrong here?

AStopher
  • 4,207
  • 11
  • 50
  • 75
  • Probably, `self:LogToSystem` should be outside of the `Aurora64.Chat` table? – Egor Skriptunoff Jun 30 '16 at 11:50
  • You' re not familiar to Lua tables, huh? First, you' re trying to include `LogToSystem` in `Init`, which isn't possible because it isn' t declared yet and even then you' re declaring it as part of the Chat table itself and not as global. Then you're also trying to declare `self:LogToSystem`, probably thinking that `self` is sone sort of keyword referring to `Chat` itself. I see you're confused. Try explaining how you want those methods to be called and I will probably be able to help you. – user6245072 Jun 30 '16 at 12:27
  • It's also unclear what are you trying to do with `entityId` and other `entity.id`s. – user6245072 Jun 30 '16 at 12:51
  • @user6245072 `Init` and `LogToSystem` are completely separate functions, as can be seen in the code. I'm pretty sure my code has them completely separate but if not, please give me more information so I can fix it. I've been doing Lua for 7 years but haven't done any programming in it for the last 2, and I've learnt a few more programming languages since then so it's easy to see how/why I can be confused. I need to get back into 'the groove'. – AStopher Jun 30 '16 at 14:49

1 Answers1

1

You are missing a comma before LogToSystem and you need to define it a bit differently (by adding self as an explicit parameter):

    end,

    LogToSystem = function(self, msg)
        System.LogAlways("$1[Aurora 64]$2 " .. msg);
    end
}

It's not possible to use the form obj:method with anonymous functions assigned to table fields; you can only use it with function obj:method syntax.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thanks, I thought it would be something as simple as that (I took a hiatus from Lua for two years and I've learnt a few more since then, so it's pretty easy to see how I became confused). – AStopher Jun 30 '16 at 14:51
  • @cybermonkey there are a few other mistakes if I get it right. In `Init` you're referring to `entityId`, and `self:LogToSystem`, which both can't be used as it is. You need to replace `entityId` with `Aurora64.Chat.entityId` and to use `self:LogToSystem` the function `Init` has to include `self` as the first argument and has to be called using the colon operator. – user6245072 Jun 30 '16 at 15:41