Jump to content

New Trait : "Vegetarian"


RAINBOW

Recommended Posts

I've created two lua files.  One simply called "EatMeat.lua" and The other is a working "Custom Trait", the creation of a custom trait seems to be the best documented example of lua modding in PZ, and so I've decided to start here for my modded server.  I've a background in java, the problem i'm having is surfing the documentation for the correct methods to call! :(

 

The custom trait code works fine, but I'll post it here at the end of the forum post anyway, in case someone is wanting to learn from it, the following piece of code "EatMeat.lua" is the troubling one!

 

My problem is finding a method which will identify -what- has been eaten by the PC.

 

i.e) A "GetLastEatenBy()" just doesn't exist. 

 

Can anyone help? Here's the code:

 

-- EatMeat.lua EatMeat = {} local function EatMeat.EatFoodOrder(0) if getTrait(TraitFactory.vegetarian) == true      -- do stuff hereend Events.OnGameBoot.Add(initOrder);

 

The function EatMeat.EatFoodOrder(0) is the only method I could find, relating to eating and I simply don't know where else to look for documentation as I'm new to modding for PZ.  (Note: the argument "0" is just "player 1" here)

 

 

Here's the creation of a custom trait (this code is working) code chunks below for the curious:

 

-- PlayerTraits.lua local function initTraits()TraitFactory.addTrait("vegetarian", "Vegetarian", -2, "Meat is Murder.\nEating meat causes Nausea!", false);end  Events.OnGameBoot.Add(initTraits);

 

 

 

 

 


I'm really just looking for whatever methodology a 'good' modder uses when they want to do something in PZ modding-

 

For example I cruise this documentation for methods, but there is simply no explanations, it's pretty damn dry!

 

http://theindiestone.com/zomboidjavadocs/

 

Also, I know its VERY early days here on PZ, and I can't remember being this excited about a set of modding tools since "Neverwinter Nights" came out, about 10 years ago. 

Link to comment
Share on other sites

Currently you can't know what is eaten by the player, the only thing to do is override the ISEatFoodAction.lua.

 

But I can add a getLastFoodEaten if you really need it, but it'll be better (if I understand corretly what you need) to throw a lua event when the player eat something, nope ?

 

Also, to test if a player have or not a trait, just do : player:HasTrait("your_trait_id") (so in your case if 0:HasTrait("vegetarian") do...)

Link to comment
Share on other sites

Yes! the solution then is to override the "ISEatFoodAction.lua"?

 

I think my real question is, and probably a few other people-

 

"How do I know where to look for this method?" or in other words "Where can I find this method?"

 

To repeat myself a wee bit- The only resource I have as a modder is this documentation here: http://theindiestone.com/zomboidjavadocs/

 

I'm not an idiot tho, its early alpha and I'm expecting the modding community here to be huge, so I know there will be floods of modding tutorials coming soon! (I'm utterly utterly eager, and love this game!) 

 

calling that java code : " player:HasTrait("your_trait_id")" makes a lot more sense than using an if statement in lua too! thanks for noticing that one!

Link to comment
Share on other sites

Well, for lua it's not really documented, I try to comment my code, but I often forget to do so (woops !)

 

You just need to get used to how we code, every action are called "Timed Action", you can find the UI in the client/ISUI/ folder etc...

 

Check this, it's a really good start : http://pz-mods.net/guide/

 

Or just do like I did when I was a modder : download other mods, and see how they do stuff.. :)

 

For example in eating, what you could do : To eat I need to right click on the food item in the inventory, this code is handled in InventoryPaneContextMenu.lua.

You search for "eat" and should find something like : if instanceof(item, "Food") then canEat = true end

Then check where I use canEat, it'll show you a function "onEat", and then I call the ISEatFoodAction method, and there you go : how the game handle the eat action (but every bonus/malus food-thingy are handled in java ;))

Link to comment
Share on other sites

I think my real question is, and probably a few other people-

 

"How do I know where to look for this method?" or in other words "Where can I find this method?"

In practice, your best bet is just to figure out what feature of the game does something similar to what you want to do, and then find the .lua file where it's done and work off that example. A competent grep implementation (GrepWin for Windows, as example) helps a lot here.

Link to comment
Share on other sites

I found a whole tonne of .lua examples from the ISUI! Thanks a load for that- But where can they be found in terms of the windows folder system?

 

For example, In my "C:/User/Zomboid/" folder, I have a media/lua folder, but it is empty!

Link to comment
Share on other sites

Ah yeah, to find this.. Well.. Look for modding tutorial, I think it should be down there..

 

InventoryPaneContextMenu for right clicking action in inventory

WorldObjectContextMenu for right clicking action in the world (like clicking on a window, etc.)

 

But I also add custom menu for things, like farming, building.. To avoid having everything in one file, I use lua event like : Events.OnFillWorldObjectContextMenu.Add to fill the world context menu with more stuff


I found a whole tonne of .lua examples from the ISUI! Thanks a load for that- But where can they be found in terms of the windows folder system?

 

For example, In my "C:/User/Zomboid/" folder, I have a media/lua folder, but it is empty!

 

It's in steam/steamApps/common/Project Zomboid/media/lua

 

I recommend you using an IDE like IntelliJIdea with a lua plugin, or eclipse with the same plugin, it's way easier then :)

Link to comment
Share on other sites

YAY! We got there in the end! :)

 

"steam/steamApps/common/Project Zomboid/media/lua"

 

This has EVERYTHING I will need!  

 

With regards to IDE, I've been using Notepad++ to write my code, don't think anyone has made a plugin for PZ with it yet (I had a brief look for one)... Is IntelliJIdea free?  Eclipse is free I believe- or at least it is a reasonable price.  I remember it being very similiar to Netbeans.  I'll probably switch to Eclipse- But I am just too excited to start modding for PZ that I wanted to start coding right away so I am using Notepad++ ! :)

 

I'm going to get stuck right into your "Quest" methods, and dissect the "Last Stand" map, and see what the possibility is for a persistent world in Project Zomboid.  Multiplayer questing? Yes please! :)

 

Thanks for the help, feel free to consider this thread closed, I really appreciate the support you guys have for the modding community.  I'm migrating my old friends from Neverwinter Nights here because I have alot of faith in java but also the game is simply looking superb.  

Link to comment
Share on other sites

Hmmm... Further experimentation leads to me ripping apart other mods and learning how to make additions.  For example using :

 

"Events.OnFillWorldObjectContextMenu.Add"

 

Like you said! Funnily Enough!

 

But!- I'm trying to hook into existing functions not make additions.  I know I can edit the "Standard" templates in  "C:Steam/SteamApps...", in the media/lua folder there.  And this is a solution.  But a messy one.  I'd much rather be able to figure out how to "hook" into the existing lua from my "Mod" folder, so everything is isolated to the mod, and not overwriting base code.  

 

Does it involve the "require" chunk at the top of code?  I'm not certain what I'm doing, lol.  I'm aware of "#include", is require the same as that?

 

Here's my lua code I'm trying to hook into the event, it doesn't do anything other than Print statements for debugging purposes, or err... it SHOULD be doing that, but it isn't ! :)

 local function custom_eating()--debug -- A SHOTGUN of print statements for bug testing!print(getItemText(item:getName()));print(item);print(player, item);print(item:getName());  --if item == item:getName("Ham") --endendend -- This will be fired whenever A player opens up the inventory right click.Events.OnFillInventoryObjectContextMenu.Add(custom_eating);

I appreciate the help incidentally, and I cringe that you might even think I am "trying to get you to write my code for me", I literally cringe - I'm wanting to learn the language.  Very much so.  For me this is about learning lua.  I've experience with java, and have fallen in love with PZ, it's just taking these baby steps.  So don't ever doubt I appreciate your aid! 

 

Thanks

Link to comment
Share on other sites

You miss the parameter of the function, check where I launch this event (in InventoryPaneCOntextMenu)

 

-- use the event (as you would 'OnTick' etc) to add items to context menu without mod conflicts.
    triggerEvent("OnFillInventoryObjectContextMenu", player, context, items);

 

So you have the player (well, his ID, do a getSpecificPlayer(player) to have the IsoPlayer), the context menu (so you can add stuff in the already done menu), and the items selected when i right clicked

Link to comment
Share on other sites

Hahaha, you are NOT going to believe this.  I feel like an absolute idiot.  I've spent all day trying to figure out why this is not working-

 

I didn't ENABLE the mod, in the modloader.  What an absolute waste of the day! AGH.

Link to comment
Share on other sites

Here's my current code, note the first remark/comment I put in there:

 --where should I be calling this function from with regards to the Events.etc.etclocal function custom_eating(_player, _context, _items) local player = _player;local menu = _context;local item = _items; --debugprint(player);print(player:getName());print(getItemText(item:getName()));print(item);print(player, item);print(item:getName()); print(menu);print(menu:getName());  --if item == item:getName("Ham")  --endend -- This will be fired whenever A player opens up the inventory right click and selects "Eat":Events.OnFillInventoryObjectContextMenu.Add(player, context, items);

My problem in understanding is coming from a combination of not understanding that last line, here, I've highlighted it(Blue)-

 

Events.OnFillInventoryObjectContextMenu.Add(player, context, items);
 
In the modding tutorials I've stuck into, (RoboMats, etc) I noticed that events take the following format usually:
 

function derpFunction(player,item)
player:Say(player:getName().." is attacking with "..item);
end

Events.onWeaponSwing.Add(derpFunction);
 
How do I combine these two things successfully in my example.  I know If I can just see it in this example, it will allow me to write better code in the future.  Nevermind "Working" code! :)
Link to comment
Share on other sites

If I'm right- And i -think- I am right, the idea is to "Locate" the relevant code chunk, using the manner you suggested- so that it covers anything pertaining to what it will need-

 

Then, I copy-paste that "Relevant" function into my custom lua file (What I called the derpFunction in the previous example-

 

And from there, I can hack away, and it should execute okay.... 

 

Okay, here goes.  Time to hack!

Link to comment
Share on other sites

Arg, no :

 

The Events will call a function, so :

 

Events.OnFillInventoryObjectContextMenu.Add(YOUR_FUNCTION); -- it mean, when OnFillInventoryObjectContextMenu is called, the function YOUR_FUNCTION will be executed.

 

And then the function will be called WITH parameters, to know which one check where I call this function, just look for "OnFillInventoryObjectContextMenu".

 

I already done some comments on each event, I don't think I posted them already, so here it is :

 

Spoiler

 

Need to finish this tho... But it can give you a pretty good hand, I think.

Link to comment
Share on other sites

Got it all figured out, yes.  It took some time, and it didn't help I spent all day trying things that wouldn't work for me because I forgot to enable my mod in the modloader.  It contributed no end to the confusion, so I made a cup of tea, started from fresh.   

 

I'm already taking a few little notes for some tutorials I will probably end up making in the future.  Essentially, for anyone curious about this thread, I copied the lua file in its entirety (Highlighted in Red) in the following fashion :

 

From

"C:\Program Files (x86)\Steam\SteamApps\common\ProjectZomboid\media\lua\ISUI\ISInventoryPaneContextMenu.lua"

To

"C:\Users\User\Zomboid\mods\SanctuaryMod\media\lua\mods\SanctuaryMod\ISInventoryPaneContextMenu.lua"

((Note: "SanctuaryMod" is my custom folder, set up in the traditional hierarchy explained in robomats beginners tutorial))

 

Edit: The above is a totally misunderstood way of doing things.  Using Events to hook into these functions creates far more beautiful code, and completely avoids conflicts with other mods.  

 

 

I then located the part of the code with the function name "ISInventoryPaneContextMenu.eatItem = function(item, player)", which could not of been achieved without your help Robert Johnson.  I simply had no experience how to navigate through the lua code until this forum post.  

 

I added the following code (Right after the final call, which a novice can identify by the words "TimedActionQueue" with regards to anything thats going to "Bring up" the action bar we all know and love in game :P )

--+ SANCTUARY MOD - Custom Eating (Vegetarian Trait) +--local string whatIam = item:getName();  --if you don't initialise your variables, they will default to "nil" if whatIam == "Ham" thenprint(whatIam);end

For novices trying to figure out where I "Plucked" the following from(highlighted in blue):

 

"local string whatIam = item:getName(); "

 

 

the way to navigate the heaps of methods you can call for literally everything is through the javadoc, you can check out the javadoc here: http://theindiestone.com/zomboidjavadocs/

 

As an addendum, In other scripting languages I've worked with, the "then" operand in an if statement was not compulsory.  I learned today that in lua, you -need- that statement.

 

Here's an example of what I mean:

if condition(true)     --do stuff  --won't work!end if condition(true) then    --do stuff  --Will now work!end 
Link to comment
Share on other sites

OMG.  I had one of those beautiful moments you get when you code, when it all fits together.  It's so unbelievably simple now.  Understanding that lua HOOK's into those functions through events is superb! I mean, really understanding it.  

 

I can completely compartmentalise my code.  It's a beautiful thing, I re-read your last post Robert, about Events. To clarify what I have been doing up until now- I have been copying the ENTIRE lua file from the steamapps folder, instead of simply hooking into it with events! What a nightmare! 

 

Is it possible to create my own events (If I feel comfortable with it not causing other mod conflicts) using this function?

"triggerEvent();"

 

I suspect that is a "hard coded" thing.  Am I wrong?

Link to comment
Share on other sites

RobertJohnson, this forum post inspired me to write a tutorial on where to go/how to navigate through the lua to write your custom code.

 

I've put it on my persistent worlds website.  The advanced tutorial will follow on from it, showing people how to write the custom code itself-  I reached a point where it is better to wait for some more development before I post the second part or continue with the custom trait-

 

The development, incidentally, is for MoodleFactory related methods, which (I think?), are currently not implemented(nothing in the javadoc), but probably on the way once higher priorities are sorted out.  

 

Note: The fact they are not implemented only bolsters my faith that I have joined this modding community at an early stage, and that wonderful things are on the horizon for my server, since I will have plenty of time to prepare my server before the game hits a peak player count.  

 

OH! Here's the link to my tutorial Edit: Has undergone major changes since a week ago-

 

http://www.sanctuary-pz.com/#!tutorialvegetarian/c1su

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...