LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   Beginners LUA Problem: Calling function located in another file (https://www.lotrointerface.com/forums/showthread.php?t=1100)

lerusius 09-28-2010 06:48 AM

Beginners LUA Problem: Calling function located in another file
 
Hello! I'm a real beginner in Lua and programming. But I'd like to learn so here goes my first question!

I want to when I use /bs getsize to return the size of all bags you currently have unlocked. but I get the error
"...\Main.lua:16 : Attempting to index global 'BagspaceLeftFunction' (a nil value)"

I have marked line 16 with blue.

What I'm trying to do is to use the function BagspaceLeftFunction:BagSize() that is located in another file. Any ideas how to fix this?

_init_.lua
Code:

-- Common imports.
import "Turbine";
import "Turbine.Gameplay";
import "Turbine.UI";
import "Turbine.UI.Extensions";
import "Turbine.UI.Lotro";
import "Turbine.Utils";

--BagspaceLeft specific
import "Lerusius.BagspaceLeft.BagspaceLeft";

Main.lua
Code:

--Just for clarification on what I'm doing, changing name on
--the Turbine.Shell Command() to BSCommand

BSCommand = Turbine.ShellCommand();

--Could put this inside the Execute function but decided on having
--it here instead take a look at line 18 on how I'm using it

function BSCommand:GetHelp()
        return "This is the Help Command"
end

--adds the commands /bs or /bagspaceleft to lotro
Turbine.Shell.AddCommand("bs:bagspaceleft",BSCommand);

--Here comes the real adding of commands to the plugin. for example
--"/bs bagsize" will call the command BagspaceLeft:BagSize() that lies in
--BagspaceLeft.lua

function BSCommand:Execute( command, arguments)
        if (arguments=="getsize") then
                BagspaceLeftFunction:BagSize();
        elseif (arguments == "help") then
                Turbine.Shell.WriteLine(self:GetHelp());
        end
end

--Welcome Message when loading the plugin
Turbine.Shell.WriteLine("BagspaceLeft by Lerusius");

BagspaceLeft.lua
Code:

function BagspaceLeftFunction:BagSize()
        local player = Turbine.Gameplay.LocalPlayer();
        self.backpack = player:GetBackpack();
        Turbine.Shell.WriteLine (self.backpack:GetSize());
end


rushl 09-28-2010 11:23 AM

I did something similar for Horsey. I ran your test code, and found a few changes that should get you going. First, none of the imports in your common file are reaching the later scripts. So I removed it altogether.

For your Main.lua, I ended up with:

Code:

import "Lerusius.BagspaceLeft.BagspaceLeft";

--Just for clarification on what I'm doing, changing name on
--the Turbine.Shell Command() to BSCommand
BSCommand = Turbine.ShellCommand();

--Could put this inside the Execute function but decided on having
--it here instead take a look at line 18 on how I'm using it
function BSCommand:GetHelp()
        return "This is the Help Command"
end

--adds the commands /bs or /bagspaceleft to lotro
Turbine.Shell.AddCommand("bs:bagspaceleft",BSCommand);

--Here comes the real adding of commands to the plugin. for example
--"/bs bagsize" will call the command BagspaceLeft:BagSize() that lies in
--BagspaceLeft.lua
function BSCommand:Execute( command, arguments)
        if (arguments=="getsize") then
                BagspaceLeftFunction();
        elseif (arguments == "help") then
                Turbine.Shell.WriteLine(self:GetHelp());
        end
end

--Welcome Message when loading the plugin
Turbine.Shell.WriteLine("BagspaceLeft by Lerusius");

Changes in yellow. I moved the function include here, and the function call name didn't need the index.

For your BagspaceLeft.lua:

Code:

import "Turbine.Gameplay";

function BagspaceLeftFunction()
        local player = Turbine.Gameplay.LocalPlayer();
        local backpack = player:GetBackpack();
        Turbine.Shell.WriteLine (backpack:GetSize());
end

Here I moved the Turbine.Gameplay import to this script. Also, I changed the now shortened BagspaceLeftFunction() name. Finally, the self.backpack index won't work here, so it's now a local variable too.

Running that, I get a return of "75" when I enter /bs getsize!

Hope this helps!

rushl

lerusius 09-28-2010 11:51 AM

Thank you for your help rushl! The naming off stuff was taken kinda directly from the turbine examples :P I should probably start thinking on my own ;).

So the big problem was that the imports didn't work. I thought that init.lua took care of that but looks like I was wrong :p. So what is __init__.lua doing then?:confused:

rushl 09-28-2010 12:34 PM

You know, I've used it to include classes. I've not used it to include other things, although I'm not 100% sure of it's exact specific function...

SanDBoX 09-28-2010 04:33 PM

the __init__.lua is used if you are importing the package directory, all the examples i have seen use something similar to this:


in your main program file you load:
import "SandBox.Crafters"

which is the line that will call the __init__.lua file which in turn you have all your other imports located:

import "SandBox.Crafters.Main"
import "Sandbox.Crafters.CraftersWindow"
etc...

so any of your other packages you want to have access to the reasources in another page you just have to import the parent directory which calls the __init__

does that clear things up for you?

lerusius 09-30-2010 05:08 PM

Thank you so much for your helping and explaining of how things work! =)


All times are GMT -5. The time now is 11:23 AM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI