Page 1 of 1

References/Pointers/Tips on lua_item scripting

Posted: Fri Jul 28, 2017 12:46 am
by Spaceman
Does anyone have any experience using the functions defined in lua_item.h? I’ve been poking at them for awhile and I can’t get them to work like I can functions in the lua_baseentity, lua_trade_container, and luautils files. The usual player: or trade: syntax doesn’t work, and I’ve tried using item:, lua_item:, Get_Item:, GetItem:, and likely a few others I can’t recall at the present. Any advice would be greatly appreciated.

Re: References/Pointers/Tips on lua_item scripting

Posted: Wed Aug 02, 2017 8:18 am
by wadski
I've only messed with addMod and getID they seemed to work fine for me .. the others, have never tried.

would be easier if you could explain exactly what you are trying to write.

Re: References/Pointers/Tips on lua_item scripting

Posted: Wed Aug 02, 2017 4:30 pm
by TeoTwawki
Just as an example if trying to use entities like "player:" and yoru function you are inside only has parameters "target, item" then "player" is nil and you can do anything to it. Trade functions are intended for use in trades and noplace else. You'd need a different binding.

Below is the code for an acorn cookie and its effect because foods are special and have their effect right in the item script. I hope this helps.

Code: Select all

function onItemCheck(target)
    local result = 0;
    if (target:hasStatusEffect(EFFECT_FOOD) == true or target:hasStatusEffect(EFFECT_FIELD_SUPPORT_FOOD) == true) then
        result = 246;
    return result;

-- OnItemUse

function onItemUse(target)

-- onEffectGain Action

function onEffectGain(target,effect)
    target:addMod(MOD_AQUAN_KILLER, 10);
    target:addMod(MOD_SILENCERES, 10);
    target:addMod(MOD_MPHEAL, 3);

Re: References/Pointers/Tips on lua_item scripting

Posted: Wed Aug 02, 2017 6:22 pm
by Spaceman
Ha! You make a great point. That post made sense to me at 4am local time after tooling around with the code for 5-6 hours but I can see that it’s lacking direction for others.

First of all, I wanna say thanks to all of you devs for making and sharing the emulator. My spouse and I are having a ton of fun with it.

I’m trying to create the ‘Give a Moogle a Break’ quest as per the retail game (it may be already done by someone else; I’ve not checked the github in a while but I need the coding experience anyway). By the logic of the quest, a player has to have a bronze bed installed in their moghouse, have a native national fame of 3+, and trigger the moogle in their own moghouse to initiate the quest.

I’ve gotten a custom function to work in the moghouse.lua file that returns if a character is in their own moghouse. I’ve gotten ‘player:hasItem(5)’ to successfully check if the player has the bed, but what I’m hung up on is checking where the item is in the player’s inventory.

Seemingly, the function ‘getLocationID’ would do the job but I don’t know the syntax to get it to work. From what I’ve learned from other scripts I’ve looked at, it’s usually required to put a ‘player:’ or ‘trade:’ in front of a function to reference how/when/whom the function is applied, but trying something like item:getLocationID(5) returns an ‘attempt to index global ‘item’ (a nil/number value[this can vary based on what I’m trying to use])’ error.

I suppose what I’m asking is does anyone know some functional syntax to use for the lua_item functions to compare an item to something else, like a location or type?

If I can get this quest to work as per retail, I think it would only be a short stride to building a rudimentary, but functional, moghancement system.

Re: References/Pointers/Tips on lua_item scripting

Posted: Thu Aug 03, 2017 12:28 am
by TeoTwawki
Because like I was warning you of, in that context "item" does not exist. :) So you can't do what you are trying that way*. You don't act on the item, you act on the player because in this context player does exist.

But luckily, hasItem accepts an additional argument to check a specific container: ... .cpp#L1045

Code: Select all

    if (!lua_isnil(L, 2) && lua_isnumber(L, 2))
        uint8  locationID = LOC_INVENTORY;

        locationID = (uint8)lua_tointeger(L, 2);
        locationID = (locationID < MAX_CONTAINER_ID ? locationID : LOC_INVENTORY);

        lua_pushboolean(L, PChar->getStorage(locationID)->SearchItem(ItemID) != ERROR_SLOTID);
        return 1;
When not nil and is a number, it'll check the matching container and then return true/falls where it was invoked. Else it'll do its default behavior you've already seen by calling the charutils function.

When in doubt, search! Github has a very partially effective code search feature. Sometimes it'll miss stuff, but it mostly works. For unix ppl there is grep (can also do this on windows with git bash), and I sometimes use "SearchMyFiles" to do the same thing local because that tool is rather fast.[/size]

*In a long round about way it is possible to get the pointer to the item so you'd have an item: entity type to work with. But this would be a terribly bad way to handle this situation anyway.

Re: References/Pointers/Tips on lua_item scripting

Posted: Thu Aug 03, 2017 10:45 am
by Spaceman
Perfect. This is what I (basically) wanted to know: If there is syntax to treat an item like an entity or, as is the case, if there is a different/better way to
handle it. Wish I knew how to word what I was looking to do a little better in my previous posts. I only took this approach to checking the item because the charutils.cpp file does it in this manner under the 'LoadInventory()' function around line 922 when assessing and building character storage, but it does seem to use a pointer for the item itself in the process (if I'm reading the code right; still trying to learn c++ syntax).

I'll revisit my logic setup with this in mind and see what I can come up with. Thanks so much!

Re: References/Pointers/Tips on lua_item scripting

Posted: Thu Aug 03, 2017 7:25 pm
by TeoTwawki
90% of the answers are gonna be in luautils.cpp or luabaseentity.cpp