Test Pluggin: Noooo....
#1
So, I'm trying to write a basic plugin to get a feel for how the plugins work. This is what I've gotten so far, and it doesn't work.
 
PLUGIN = nil

function Initialize(Plugin)
    Plugin:SetName("PluginTest")
    Plugin:SetVersion(1)
    
    -- Hooks
    cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_RIGHT_CLICKING_ENTITY, ExplodeHoe)
    PLUGIN = Plugin -- NOTE: only needed if you want OnDisable() to use Get() name or something like that
    
    -- Command Bindings
    [undefined=undefined]
    LOG("Initialised " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
    return true
end
    
function OnDisable()
    LOG(PLUGIN:GetName() .. " is shutting down...")
end

function ExplodeHoe(Player, Entity)
    local HeldItem = Player:GetEquippedItem();
    if (cItemCategory:IsHoe(HeldItem.m_ItemType)) then
        Player:GetWorld():DoExplosionAt(0,Entity:GetPosX(), Entity:GetPosY(), Entity:GetPosZ(), false, esPlugin)
    end
end
Now, what happens is that I get a complaint from the Server Console. It says:

[13:38:52] LUA: Plugins/PluginTest/main.lua:23: attempt to index global 'cItemCa
tegory' (a nil value)

I assume this is because cItemCategory isn't declared anywhere, and nothing is #include'ed. The thing is, I'm just following the example in the API documentation. How do I get this to work?

EDIT: Fixed the showcode. (The show code button glitched before and I didn't know the manual tags, so I just rolled with it.)
Reply
Thanks given by:
#2
I think it's ItemCategory:IsHoe not cItemCategory:IsHoe
Reply
Thanks given by:
#3
Okay, that helps. (The API example says to use cItemCategory)

But now I have a different error:

[14:06:19] LUA: Plugins/PluginTest/main.lua:23: error in function 'IsHoe'.
argument #1 is 'table'; 'number' expected.

HeldItem.m_ItemType should return a number, so I'm not sure what the problem is.
Reply
Thanks given by:
#4
What does the console say when you put:
print(HeldItem.m_ItemType)
after
local HeldItem = Player:GetEquippedItem()
Reply
Thanks given by:
#5
Narroo, please use [ shcode=lua ] and [ /shcode ] when pasting code, so that the forum uses proper highlighter for the code

Also, where in the API docs does it say to use cItemCategory? It's probably a bug in the docs, I'd like it fixed.
Reply
Thanks given by:
#6
I think it's in the example code.
Reply
Thanks given by:
#7
(02-19-2014, 07:18 AM)xoft Wrote: Narroo, please use [ shcode=lua ] and [ /shcode ] when pasting code, so that the forum uses proper highlighter for the code

Also, where in the API docs does it say to use cItemCategory? It's probably a bug in the docs, I'd like it fixed.

It's in the code example on the bottom.

(02-19-2014, 05:16 AM)STR_Warrior Wrote: What does the console say when you put:
print(HeldItem.m_ItemType)
after
local HeldItem = Player:GetEquippedItem()

It says: userdata: 079C3218 .
Reply
Thanks given by:
#8
Its because IsHoe is a static function not a method. So the correct code would be
if (ItemCategory.IsHoe(HeldItem.m_ItemType)) then

It seems that the error argument #1 is 'table'; 'whatever' expected often indicates calling a static function as though it was a method.
Reply
Thanks given by:
#9
Okay. I though ":" was the equivalent to C++'s "->". I need to reread Lua's reference manual.

Also, I was copying from the API documentation on the ItemCategory page, so that needs to be fixed.

Finally, there was another error, I was lacking the "SourceData" parameter which was being left off in the example. Anyways, what's SourceData? The API dump just says "The SourceData parameter is specific to each source type, usually it provides more info about the source.." What does that mean? Why would "DoExplosionAt()" require this?
Reply
Thanks given by:
#10
The SourceData specifies what exactly is exploding. If the explosion is from an entity, it is the entity instance. If the explosion is from a bed in the nether, it is a Vector3i with the bed's coordinates.
Here's a list: http://mc-server.xoft.cz/LuaAPI/OnExploded.html
I know the docs are lacking in this manner, the table should be with the esXXX constants documentation and it should be linked to all of the places where the constants are mentioned; somehow I didn't get around to fixing that.
Reply
Thanks given by:




Users browsing this thread: 5 Guest(s)