Can construct building if you have a resource without consuming it

I tried to replace gold with food, culture, science, didn't worked. Do I have to use different words or a totally different code is needed to add those yield ?
It depends on the yield but for the ones you listed yes.

Culture
Use "player:ChangeJONSCulture(int change)" because Culture is a pseudo yield that doesn't always behave like other yields. Culture granted this way will not contribute to any city's border expanding.

Food
Food can not be given on the "player level". Food only exists on the "city level". You'll need to get the city object and use "city:ChangeFood(int change)".

Science
This one is tricky since techs don't exist at the "player level" they exists at the "team level". You'll need to get the Team the player is on, than the tech their team is currently researching. Once you have that you'll use "teamTech:ChangeResearchProgress(TechType index, int change, PlayerID player)".
---------------------------------
Looking at your previous works I noticed that you create a table where modders can insert their data (so they don't need to touch the lua code).
You can certainly construct something similar, I am just unsure what structure you intend the table to have and what you intend the columns to mean.

I don't know if there are any other differences between inserting data in lua or using sql/xml (I usually prefer SQL over xml) and if there are not any major differences...
If the next paragraphs are confusing, don't worry about it.

SQL and XML are use to populate a database of "constants" (things that won't change over the course of a game). XML gets automatically parsed into SQL but there are things SQL can do that can't be done in XML. Thus, if you are more comfortable with SQL than feel free to use it 100% of the time over XML.

LUA isn't generally used to insert data. While XML and SQL are two different ways to do the same thing, LUA is something different.
 
ABOUT THE YIELDS

tried with culture, didn't worked. While I try other solutions maybe it's better if you look at what I did: SOLVED

Spoiler :
function WeaverMarketBonus(playerID)
local player = Players[playerID];
local numWeaverMarkets = 0;
local cultureBonus = 0;

-- Count how many Weaver Markets the player has
for indexCity in player:Cities() do
if(indexCity:IsHasBuilding(GameInfoTypes["BUILDING_WEAVER_MARKET"])) then
numWeaverMarkets = numWeaverMarkets + 1;
end
end

-- Calculate the bonus culture to give
if(numWeaverMarkets > 0) then
if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_SILK"], true) > 0) then
cultureBonus = cultureBonus + 1;
end
if(player:GetNumResourceAvailable(GameInfoTypes["RESOURCE_COTTON"], true) > 0) then
cultureBonus = cultureBonus + 1;
end

player:ChangeJONSCulture(int change)(numWeaverMarkets * cultureBonus);
end
end
GameEvents.PlayerDoTurn.Add(WeaverMarketBonus);

I replaced "gold" with "culture" and "player:ChangeGold" with "player:ChangeJONSCulture(int change)"


log says: SOLVED

Spoiler :
Syntax Error: C:\Users\Windows 7\Desktop\Documents\My Games\Sid Meier's Civilization 5\MODS\Angel of History (v 1)\LUA/BuildingsDifResBonus.lua:23: ')' expected near 'change'

Runtime Error: Error loading C:\Users\Windows 7\Desktop\Documents\My Games\Sid Meier's Civilization 5\MODS\Angel of History (v 1)\LUA/BuildingsDifResBonus.lua


I know that those things are obvious for you but... what exactly do I have to replace? :blush: :blush: :blush: I feel a little dumb but currently I can only suppose the meaning of what I read (like someone who speak only some words of english) and the logic, AFAIK, must be perfect and clear in order to work. I made experiments only in an empirical and "opportunistic" way (that can be a good start, but only a start)

ABOUT THE SQL/XML TABLES

Well, I can imagine something like this: CREATE TABLE Building_GlobalResourceYield with four columns (BuildingType (text), ResourceType (text), YieldType (text), Yield (integer)) I hope the terms are correct, these are what I usually find in new tables created by modders

ABOUT THE XML/SQL/LUA DIFFERENCE

I understand perfectly (I am Ignorant but no SO ignorant :)). I feel comfortably with SQL (although, as probably almost everyone, I begun with XML) especially to handle a large quantity of data, simply, the columns view is more practical (and I remeber to have red somewhere that the game translate xml in sql).

I know that they do the same thing (transalte xml in sql was infact pretty easy).
If I understood correctly:
1)XML and SQL are basically a language to insert data in a database like an excel file, with columns and rows.
2)LUA is needed to create the functions that link two or more elements of the Database to create a new rule.

I was just wandering if there is a difference, in terms of performance, between repeating the code you made for several building with their respective resources in the same lua file or do this in sql with a new table created for this purpose.
 
CULTURE WORKED!

I deleted (int change), I thoought that was only general indication.
 
FOOD WORKED!

to be sure, can you verify if what I did is ok?

I replaced "player:ChangeGold" with "city:ChangeFood"

Added below "local foodBonus = 0;" :
"local city = player:GetCityByID(cityID);"
(I thought you meant this for "get city object", I copied it from the religion prereq lua code you made first)

Question: if "city" is the city object, what is "pCity" (I found it in other lua codes before), in general, what the "p" prefix means? (player maybe? so only player cities?)
 
in general, what the "p" prefix means?
...
if "city" is the city object, what is "pCity"...
It is good programming practice to try to pick variable names that communicate the purpose of the variable. One method is Hungarian Notation. In this case of "city" vs "pCity" the "p" means "pointer". Since "city" or "pCity" are just variable names there isn't a syntactical difference between the two, just a (extremely slight) difference in naming conventions.
-------------------
With respect to the tables. I wasn't very clear in my previous post. I'm unsure what you're trying to implement. For example, are do you want to make a table that allows buildings to require that a player have a specific resource? As contrasted with the already existing tables for city-has-resource restrictions?
 
About Science: I thought about it, beside the difficulty of the code, maybe resources should not produce science (at least, not towards all the technolgies), so maybe better let go for now, your opinion?

About other Yields:

Production: I forgot Production, that shoud be essential in my opinion, can you tell me what magic words ;) I should use ? SOLVED

Happiness: I know, it's not a yield and, AFAIK, is tricky. Do you think is possible with a code like those above or it's far harder? I understand if it's too much tricky, I wait for your response.

Faith: like science I don't think resources should produce faith, I'd be glad to have your opinion also on this.
 

A very interesting matter...

About the tables: Sorry if I was unclear: the Building require the resource (not in the city radius but at National level, even if imported from other civs, like in those lua code)
So, for example, my SQL should look like this:

INSERT INTO Building_GlobalResourceYield (Buildingtype, ResourceType, Yieldtype, Yield) VALUES
BUILDING_WEAVERS_MARKET, RESOURCE_SILK, YIELD_GOLD, 1),
BUILDING_WEAVERS_MARKET, RESOURCE_SILK, YIELD_CULTURE, 1),
BUILDING_WEAVERS_MARKET, RESOURCE_COTTON, YIELD_GOLD, 1);

"Building_GlobalResourceYield" was the first thing that crossed my mind, as opposed to "local".

So, the Weavers Market require at least ONE of these resources (silk or cotton) and have a yield bonus for each one of them:
1 gold and 1 culture for having silk
1 gold for having cotton.

Obviously, there's no limit to the number of resource to insert for every building.
In the case of Weavers Market there should be also Fur, Whool, Dyes, Flax, Leather (Whool and Leather are created by me, Flax is from Horem), each one with its bonuses of Gold and/or Culture and/or Food and/or Production and/or (hopefully) Happiness. Like I said in the previous post I have doubts on Science and Faith.

(As far as I can remember this is very similar to how Corporations worked in Civ 4)

This is the purpose. I don't know if this is the best idea as table, maybe you have a better idea.

However, I tried to use together the three lua codes (gold, culture, food) and they worked. All in the same file. Just to let you know.

I hope I was more clear, tell me If I omitted something.
 
PRODUCTION WORKED !

I used the food code and replaced "food" with "production". I thought the same reasoning you did with food could be applied to production, and it worked! :)
 
About the tables: ...the Building require the resource (not in the city radius but at National level, even if imported from other civs, like in those lua code)
I would recommend focusing on just the resource requirement first. The benefit is conceptually different and should be determined elsewhere. The following should be pretty easy.

A pair of tables:
(you could just as easily call them "national" or "global")
AllowsImport would be a boolean.
Code:
Building_EmpireResourceAnds <BuildingType, ResourceType, AllowsImport>
Building_EmpireResourceOrs <BuildingType, ResourceType, AllowsImport>

The specification for these tables would be:
Building_EmpireResourceAnds
BuildingType can only be built if the player has every ResourceType. If AllowsImport is true, than imported resources can satisfy the need.

Building_EmpireResourceOrs
BuildingType can only be built if the player has any ResourceType. If AllowsImport is true, than imported resources can satisfy the need.

The LUA:
The previous WeaverMarketBonus function was hardcoded to work only for one building and required silk or cotton. Do you feel like you know enough to generalize it to implement Building_EmpireResourceOrs or Building_EmpireResourceAnds?
 
Even looking at your previous work, I find really hard understand how to link the lua to the xml table. Let me be more clear:

What I know:

1) How to copy / past the lua for other buildings,
for example: Fish Market require at least one of Fish (turned into luxury), Crab, Whale, Clam (made by me) and those resource give similar bonus as silk or cotton did
This "copy / paste" should happen in the same lua file, withou using xml/sql

I suppose it will work anyway, though, without the benefits of a comfortable sql ordered list.

So I suppose I can generalize it (repeating the lua code every time, changing building name, resources names and/or adding more resources) BUT IN LUA not using XML or SQL

2) I understand the purpose of the xml/sql table you described.

What I don't know:

What modifications have to be done on the lua codes described in this thread in order to make the table you described work

In other words: How do you know that the sql table will do what that lua code do?
It's completly unknown territory for me, I cannot do it alone.

By the way, how do you implement the AllowsImport function?
 
Here you go.

The LUA code you would need to implement the "Building_EmpireResourceAnds" and "Building_EmpireResourceOrs" tables is:
Code:
function Building_EmpireReourceAnds_Check(playerID, buildingID)
	local player = Players[playerID];

	local condition = "BuildingType = '" .. GameInfo.Buildings[buildingID].Type .. "'";
	for row in GameInfo.Building_EmpireResourceAnds(condition) do
		if(player:GetNumResourceAvailable(GameInfoTypes[row.ResourceType], row.AllowsImport) <= 0) then
			return false; -- Found a required resource the player doesn't have
		end
	end

	return true;
end
--GameEvents.PlayerCanConstruct.Add(Building_EmpireReourceAnds_Check);

function Building_EmpireReourceOrs_Check(playerID, buildingID)
	local player = Players[playerID];
	local buildingNotInTable = true;
	
	local condition = "BuildingType = '" .. GameInfo.Buildings[buildingID].Type .. "'";
	for row in GameInfo.Building_EmpireResourceOrs(condition) do
		buildingNotInTable = false; -- This building will require a resource to build
			
		if(player:GetNumResourceAvailable(GameInfoTypes[row.ResourceType], row.AllowsImport) > 0) then
			return true; -- Found a required resource the player has
		end
	end

	return buildingNotInTable;
end
--GameEvents.PlayerCanConstruct.Add(Building_EmpireReourceOrs_Check);
 
Just one question: do you plan to do resources yields table too ? just to know how organize my mod
(if yes I wait to build my "resouce-building system" and focus on other things , if not I start creating it)
 
There is already a Building_ResourceYieldModifiers table (and one for culture and faith). Are you trying to do something different?
 
I'm refering to the yield for having a resource (not in city radius).

The codes in this thread do that.
They give to a building, for example, 1 gold for having silk.
If you lose silk, lose 1 gold.

I was wandering if you plan to do a xml/sql table also for those.
I checked your mod on steam and there is the table you described here (amazing work) but no yield as far as I can see (I suppose you are referring to the vanilla tables).

Not that I'm complaining, I just wanted to know if you plan to do a table also for those.
 
Actually, now that you make me notice it, I never used "Building_ResourceYieldModifiers".
Does it do what I wrote in the previous post ? are you sure ? It's never used in the vanilla game.
Often an unused table does nothing, simply there's nothing behind in the dll (as far as I can understand). I'll test it.
 
I tested "Building_ResourceYieldModifiers".
Do not work, at least not as the lua codes on this thread.

A resouce gave 1% production boost ONLY in the local city, and this is the only effect I noticed.
Gold and Culture don't seem to work.

So, returning to the first question, do you plan to do that table ?
 
I misspoke when I said "Building_ResourceYieldModifiers". I intended to say "Building_ResourceYieldChanges" which is used a bunch. However, it only boosts resources that city is working.

Right now it is less likely that I would implement the entirety of the bonuses table for you because:
1) I am unsure what the specification is (as in what structure the table should have and what effect it should have).
2) Depending exactly on what you want it to do, it could be a fair bit more work than the very simple empire resource tables.
3) My goal is to get you comfortable with LUA such that you're able to write it on your own. Since if you want to make a mod with the scope of your vision than you'll need to write most of it yourself.

I don't intend any of that to come off as harsh or condescending. My current limitation is that I simply don't know what bonus you are trying to parameterize.
 
My goal is to get you comfortable with LUA

I don't know if you can. Please, let me explain:

When I started I didn't even know what sql, xml, lua, boolean, integr and a lot of other terms were.
Once I realized the meaning of the "words" (thanks also to what I red in this site) I become able to "translate" what I saw in English, and "write" what I wanted.

For Example:
SQL: INSERT INTO (Buildings) (column 1, 2, 3...)
SELECT "Building" "Value 1, 2, 3..."

English translation: The game datas are stored in a database excel-like.
So, insert into the Building list the name of the building you want to create and fill the columns with the values you want.
If you have doubts, watch the xml table which tells you for every column which kind of values are supported and what is the default value:

text: a name picked from another table (techs, resources..)
boolean: true or false (0 or 1 in SQL)
Integr: a number

Knowing the meaning of what I saw, and the logic behind it, it was only a matter of time before being able to "write" everything I wanted.

Although the purpose of LUA is completly different the problem I have is the same:
the meaning of the words

Let's take for example the code in the post #31:
LUA: "local"
English: I know only that usually the main terms used in the lua code are quoted before with "local"

LUA: local condition = "BuildingType = '" .. GameInfo.Buildings[buildingID].Type ..
English: I thing you're stating a condition but I may be wrong
buildingtype: should be database element (building)
= '" .. : ???????????
GameInfo.Buildings[buildingID].Type .. : this should be a database element (building) but, than, what was buildingtype ?

All those things are obvious for you, obscure for me (and for most users)

Now I understand that this can be boring for you but this is the point:

If your goal is to make modding easier for modders that aren't good with lua, you are doing an amazing job.
If your goal is really make an user comfortable with lua, you have to start from the meaning of words.
It's the basic of every language, lua is no exception.

As I said, I understand this can be boring to do and, in fact, no one did such tutorial.
But sadly, if you want to explain lua to someone who don't know it, you have to explain the meaning of every word (or sign) you use.

I know, "did you really expect I explain every single word?!"
I don't expect you to do such a probably boring task, I cannot ask for this.

Clearly learn lua would be far better than wait someone else do the job.
In other words being able to fishing is better than ask for fish.

I'm not lazy, although the time is limited, I just didn't find a proper "dictionary".
I found only very general explanations while, in my opinion, what is needed is an empirical and practical "translation": "this word / sign means that"
In other words: If you want to teach something unknown, you have to compare it to something known.

In conclusion:
If you want to teach lua to others, you should consider starting a thread for it and I surely will be the first to follow it, but I don't know if you have time / will for this hard job.

Otherwise, people like me will ask for help, trying to do small modification to codes already written but nothing more
(fore example, deleting a line who make a trait avalaible only for a single civ in order to make it avalaible for all, or replacing "BuildingX" with "BuildingY"), and hoping that someone able to write in lua find your idea interesting enough to realize it.

If you still want, I can explain better how I imagine a table based on the codes on this thread about yield for having a resource (and no yield for losing it) but I'm not currently able to do it myself (although I really wish), unless someone provides us the "dictionary" I talked above.

Sadly, without a proper "translation", the lua remains like latin:
something seems familiar, but you don't really understand it.
 
I started in the same footing as you are right now. Lua (capital "L") was a beast to me, I couldn't understand anything and worse yet, I went on to read the most convoluted of all Lua files (those from Fireaxis). After a while you get to understand (just as with any language, at least for me) making comparisons, the fact that Lua is in English is even better (like searching for the root of words to make a relation - and best yet, there is no false friends!), and I'm not even English native speaker.

If you don't mind me intervening, you should look at modder's first Lua files AND more experienced modders, since those sometimes comment on their codes ("--" before a line means a comment in Lua just as anything between "<!--" and "-->" in XML).

Another excellent source is Lua's website. I learned a LOT reading their manuals, it may take a bit, an hour or two each weekend and you'll jump from 0-50 in no time, from that moment on, the learning halts a bit - as is expected -, but since you are able to make your codes your moment won't.

Good luck! :)
 
Back
Top Bottom