Jump to content

Insert / Import new code in existing files?


Panda

Recommended Posts

Hi,

 

it's me again.

 

I finished my Watermelon Seeds mod now and it works fine for me. Now I would like to share it.... One problem: I modified the original files because it was easier for me and I am still a newb with lua...

 

I know that you can import lines in the original files with "table.insert" e.g. but for me that just works for the distributions... 

I simply can't figure out how to insert things in the other files that have to be modified.

For example:

 

farming_vegetableconf.lua contains the most important lines for the mod... The code I added looks like this (example, there is more...):

 

-- Watermelons (take even longer than strawberry)farming_vegetableconf.props[farmingText.watermelons] = {};farming_vegetableconf.props[farmingText.watermelons].seedsRequired = 20;farming_vegetableconf.props[farmingText.watermelons].texture = "TileThuztorCabbageLettuce7Phase_0";farming_vegetableconf.props[farmingText.watermelons].waterLvl = 90;farming_vegetableconf.props[farmingText.watermelons].timeToGrow = ZombRand(72, 100);farming_vegetableconf.props[farmingText.watermelons].minVeg = 2;farming_vegetableconf.props[farmingText.watermelons].maxVeg = 4;farming_vegetableconf.props[farmingText.watermelons].minVegAutorized = 5;farming_vegetableconf.props[farmingText.watermelons].maxVegAutorized = 6;farming_vegetableconf.props[farmingText.watermelons].vegetableName = "Base.Watermelon";farming_vegetableconf.props[farmingText.watermelons].seedName = "farming.WatermelonSeed";farming_vegetableconf.props[farmingText.watermelons].seedPerVeg = 3;

 

How can I import this code into the file now? In a proper way I mean. It's not possible with simply using "table.insert" is it?

 

I don't know how I could tell "table.insert" that it has to put the lines directly beneath the other .props, because there is no "head category". I hope you know what I mean, because I can not really explain it well :D.

 

The problem gets even bigger with these lines of code:

 

-- Watermelons-- Need 20 seeds-- Water lvl over 85-- Need much longerfarming_vegetableconf.growWatermelon = function(planting, nextGrowing, updateNbOfGrow)	local nbOfGrow = planting.nbOfGrow;	local water = farming_vegetableconf.calcWater(planting.waterNeeded, planting.waterLvl);	local diseaseLvl = farming_vegetableconf.calcDisease(planting.mildewLvl);	if(nbOfGrow == 0) then -- young		planting = growNext(planting, "tileThuztorFarming1_seededPotato", farming_vegetableconf.getObjectName(planting), nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);		planting.waterNeeded = 90;	elseif (nbOfGrow <= 4) then -- young		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.new = false;			planting = growNext(planting, "tileThuztorFarming1_potato" .. nbOfGrow, "Young " .. planting.typeOfSeed, nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);			planting.waterNeeded = farming_vegetableconf.props[planting.typeOfSeed].waterLvl;			planting.waterNeededMax = farming_vegetableconf.props[planting.typeOfSeed].waterLvlMax;		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (nbOfGrow == 5) then -- mature		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.nextGrowing = calcNextGrowing(nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);			basicFarming.removeTileFromGrid(planting.gridSquare);			local newTile = IsoObject.new(planting.gridSquare, "tileThuztorFarming1_potatoMature", farming_vegetableconf.getObjectName(planting));			newTile:setOutlineOnMouseover(true);			planting.gridSquare:AddTileObject(newTile);			planting.tile = newTile;			planting.new = false;			planting.hasVegetable = "true";		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (nbOfGrow == 6) then -- mature with seed		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.nextGrowing = calcNextGrowing(nextGrowing, 48);			basicFarming.removeTileFromGrid(planting.gridSquare);			local newTile = IsoObject.new(planting.gridSquare, "tileThuztorFarming1_potatoBloom", farming_vegetableconf.getObjectName(planting));			newTile:setOutlineOnMouseover(true);			planting.gridSquare:AddTileObject(newTile);			planting.tile = newTile;			planting.new = false;			planting.hasVegetable = "true";			planting.hasSeed = "true";		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (planting.state ~= "rotten") then -- rotten		basicFarming.rottenThis(planting);	end	return planting;end

 

They contain so much I can't imagine that it is possible to insert it... How can I solve this?

Thanks!

Link to comment
Share on other sites

That sounds perfect. I looked into your Sleeping Overhaul mod but sadly I could not figure out how to overwrite functions :/. I tried it in various ways (simply copied the functions in a new .lua file and requested the original) but either my game crashes at startup or the watermelon seeds are simply not in the game...

I will look into it again tomorrow but of course still appreciate any tips.

Okay, I think I figured it out now. But I have a special case now and don't know how to handle it.

The basics work fine, I can overwrite the functions and they are implemented in the game but how do I add a whole new function to an existing file?

It can't be table.insert, because the function is huge and it seems impossible to add it like that. And I obviously can't overwrite it either since there is no "head" function I could overwrite...

It's probably easier if I simply show you:

I want to add this (farming_vegetableconf.lua):

-- Watermelons-- Need 20 seeds-- Water lvl over 85-- Need much longerfarming_vegetableconf.growWatermelon = function(planting, nextGrowing, updateNbOfGrow)	local nbOfGrow = planting.nbOfGrow;	local water = farming_vegetableconf.calcWater(planting.waterNeeded, planting.waterLvl);	local diseaseLvl = farming_vegetableconf.calcDisease(planting.mildewLvl);	if(nbOfGrow == 0) then -- young		planting = growNext(planting, "tileThuztorFarming1_seededPotato", farming_vegetableconf.getObjectName(planting), nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);		planting.waterNeeded = 90;	elseif (nbOfGrow <= 4) then -- young		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.new = false;			planting = growNext(planting, "tileThuztorFarming1_potato" .. nbOfGrow, "Young " .. planting.typeOfSeed, nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);			planting.waterNeeded = farming_vegetableconf.props[planting.typeOfSeed].waterLvl;			planting.waterNeededMax = farming_vegetableconf.props[planting.typeOfSeed].waterLvlMax;		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (nbOfGrow == 5) then -- mature		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.nextGrowing = calcNextGrowing(nextGrowing, farming_vegetableconf.props[planting.typeOfSeed].timeToGrow + water + diseaseLvl);			basicFarming.removeTileFromGrid(planting.gridSquare);			local newTile = IsoObject.new(planting.gridSquare, "tileThuztorFarming1_potatoMature", farming_vegetableconf.getObjectName(planting));			newTile:setOutlineOnMouseover(true);			planting.gridSquare:AddTileObject(newTile);			planting.tile = newTile;			planting.new = false;			planting.hasVegetable = "true";		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (nbOfGrow == 6) then -- mature with seed		if(water >= 0 and diseaseLvl >= 0 or planting.new) then			planting.nextGrowing = calcNextGrowing(nextGrowing, 48);			basicFarming.removeTileFromGrid(planting.gridSquare);			local newTile = IsoObject.new(planting.gridSquare, "tileThuztorFarming1_potatoBloom", farming_vegetableconf.getObjectName(planting));			newTile:setOutlineOnMouseover(true);			planting.gridSquare:AddTileObject(newTile);			planting.tile = newTile;			planting.new = false;			planting.hasVegetable = "true";			planting.hasSeed = "true";		else			badPlant(water, nil, diseaseLvl, planting, nextGrowing, updateNbOfGrow);		end	elseif (planting.state ~= "rotten") then -- rotten		basicFarming.rottenThis(planting);	end	return planting;end

and those two parts:

farming_vegetableconf.icons[farmingText.watermelons] = "Item_Watermelon";

-- Watermelons (take even longer than strawberry)farming_vegetableconf.props[farmingText.watermelons] = {};farming_vegetableconf.props[farmingText.watermelons].seedsRequired = 20;farming_vegetableconf.props[farmingText.watermelons].texture = "TileThuztorCabbageLettuce7Phase_0";farming_vegetableconf.props[farmingText.watermelons].waterLvl = 90;farming_vegetableconf.props[farmingText.watermelons].timeToGrow = ZombRand(72, 100);--~ farming_vegetableconf.props[farmingText.watermelons].timeToGrow = ZombRand(10,11);farming_vegetableconf.props[farmingText.watermelons].minVeg = 2;farming_vegetableconf.props[farmingText.watermelons].maxVeg = 4;farming_vegetableconf.props[farmingText.watermelons].minVegAutorized = 5;farming_vegetableconf.props[farmingText.watermelons].maxVegAutorized = 6;farming_vegetableconf.props[farmingText.watermelons].vegetableName = "Base.Watermelon";farming_vegetableconf.props[farmingText.watermelons].seedName = "farming.WatermelonSeed";farming_vegetableconf.props[farmingText.watermelons].seedPerVeg = 3;

The whole thing looks like this (smallest example (farming_vegetableconf.icons)):

farming_vegetableconf.icons = {}farming_vegetableconf.icons[farmingText.carrots] = "Item_Carrots";farming_vegetableconf.icons[farmingText.broccoli] = "Item_Broccoli";farming_vegetableconf.icons[farmingText.radishes] = "Item_TZ_LRRadish";farming_vegetableconf.icons[farmingText.strawberry] = "Item_TZ_Strewberry";farming_vegetableconf.icons[farmingText.tomato] = "Item_TZ_Tomato";farming_vegetableconf.icons[farmingText.potato] = "Item_TZ_Potato";farming_vegetableconf.icons[farmingText.cabbages] = "Item_TZ_CabbageLettuce";

How do I add the line above now without having to edit the official file?

Thanks again :)! Also for your patience, I know those are probably all stupid questions which can be resolved easily...

Googled a bit but still can't figure it out... Do I have to modify the original files? Is there no other way?

Link to comment
Share on other sites

I wrote something and then realised I had misunderstood... nevermind. Without poking in the code, I'm thinhking you're just adding new functions and variables to this table: farming_vegetableconf

 

In that case, why can't this be done in a separate file... presumably farming_vegetableconf is a global variable and you'll be able to edit it from anywhere?

Link to comment
Share on other sites

Googled a bit but still can't figure it out... Do I have to modify the original files? Is there no other way?

 

Merged your posts. Please be patient and don't bump your thread.

 

As Stormy said, as it is a global table you shouldn't have any problems with editing it from an external file. You can overwrite the function in an external file and for inserting values in farming_vegetableconf.icons "table.insert" should work.

Link to comment
Share on other sites

As RoboMatt has already pointed out, you need to

 

1) require the file whose data you want to modify/override

e.g. require "TimedActions/ISBaseTimedAction" at the top of the file if you wanted to mess with ISBaseTimedAction

 

2) replace the original functions / variables / symbols with new ones either by assignment or by declaration

Link to comment
Share on other sites

I know those are probably all stupid questions

Quite the opposite, unless we are both stupid. I'm very much interested.

 

If i look into override.lua in the sleeping overhaul i find the following:

require'ISUI/ISModalDialog'; -- 1-- Old = {}; -- we'll store all overwritten functions in here. -- 2-- Old.ISModalDialog = {}; -- 3Old.ISModalDialog.new = ISModalDialog.new; -- 4function ISModalDialog:new -- 5

So basically it works like this:

1. Call the original file.

2. Make the slate

3. Call the original table.

4. Make sure the original function is overwritten.

5. Overwrite the original function.

 

What about tables? In case of farming_vegetableconf.icons the above method would suggest:

-- require 'Farming/farming_text.lua'; -- not sure... do i have to call on it again or is this automatically carried over by the original?require 'Farming/farming_vegetableconf.lua';Old = {};Old.farming_vegetableconf = {};Old.farming_vegetableconf.icons = farming_vegetableconf.iconsfarming_vegetableconf.icons[farmingText.carrots] = "Item_Carrots";-- (...)-- additions here

 

or with table.insert simply

table.insert(farming_vegetableconf.icons[farmingText.Watermelons] = "Item_Watermelon");
Link to comment
Share on other sites

Thanks to all of you :)!

 

I said I figured it out before but now with all the extra explanations you guys gave me I really figured it out and finally understand how things work in RoboMats Override.lua and the file I want to edit. From here on it shouldn't be much of a problem to add / overwrite the few lines of code I have.

 

I still have muuuuuch to learn ;)!

 

@RoboMat

 

Yeah, sorry about that. Won't happen again!

Link to comment
Share on other sites

require'ISUI/ISModalDialog'; -- 1-- Old = {}; -- we'll store all overwritten functions in here. -- 2-- Old.ISModalDialog = {}; -- 3Old.ISModalDialog.new = ISModalDialog.new; -- 4function ISModalDialog:new -- 5

No ... basically it is like this:

  1. Assure that original file is loaded before mod file (http://www.lua.org/pil/8.1.html)
  2. Create global table
  3. Create nested table in global table
  4. Store original function in nested table
  5. Overwrite original function

Basically you don't need points 2, 3 and 4 ... those are only useful if you need to call the original function for some reason ;)

 

For inserting in tables you should use table.insert.

 

 

Thanks to all of you :)!

 

I said I figured it out before but now with all the extra explanations you guys gave me I really figured it out and finally understand how things work in RoboMats Override.lua and the file I want to edit. From here on it shouldn't be much of a problem to add / overwrite the few lines of code I have.

 

I still have muuuuuch to learn ;)!

 

@RoboMat

 

Yeah, sorry about that. Won't happen again!

 

Glad to hear it works now ;)

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...