lotrointerface.com
Search Downloads


Go Back   LoTROInterface > LotRO > Developer Discussions > Lua Programming Help (L)

Reply
Thread Tools Display Modes
  #1  
Unread 09-28-2010, 06:48 AM
lerusius lerusius is offline
The Wary
 
Join Date: Sep 2010
Posts: 3
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
Reply With Quote
  #2  
Unread 09-28-2010, 11:23 AM
rushl rushl is offline
The Indomitable
Interface Author - Click to view interfaces
 
Join Date: Jun 2009
Posts: 14
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

Last edited by rushl : 09-28-2010 at 11:42 AM.
Reply With Quote
  #3  
Unread 09-28-2010, 11:51 AM
lerusius lerusius is offline
The Wary
 
Join Date: Sep 2010
Posts: 3
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 . So what is __init__.lua doing then?
Reply With Quote
  #4  
Unread 09-28-2010, 12:34 PM
rushl rushl is offline
The Indomitable
Interface Author - Click to view interfaces
 
Join Date: Jun 2009
Posts: 14
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...
Reply With Quote
  #5  
Unread 09-28-2010, 04:33 PM
SanDBoX's Avatar
SanDBoX SanDBoX is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Sep 2010
Location: Idaho
Posts: 40
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?
Reply With Quote
  #6  
Unread 09-30-2010, 05:08 PM
lerusius lerusius is offline
The Wary
 
Join Date: Sep 2010
Posts: 3
Thank you so much for your helping and explaining of how things work! =)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lua General Forum Bredic Lua Programming Help (L) 3 09-12-2010 02:34 AM
Lua Plug-in and UI? Elentir Interface Help (L) 4 09-09-2010 02:42 PM
I wrote a Lua plugin, now what? scrappy General Authoring Discussion (L) 3 09-08-2010 10:06 AM
Lua support coming to LOTRO Sparr General Authoring Discussion (L) 33 08-05-2010 07:52 PM
Lua support coming to LOTRO's UI! Dolby News 1 07-15-2010 09:36 AM


All times are GMT -5. The time now is 12:42 AM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui