[GS] How to create a new variable used as a value?

Supremax67

Chieftain
Joined
Mar 14, 2019
Messages
10
I simply want to create within an SQL the option to check the difference between 2 variables and save that as a new Variable. (It is probably easy to do, but checking with the community first)


The idea is for me to be able to detect if a variable was changed when affected by another mod, and I simply want to use a known value (WHERE gamespeedtype meets criteria) and compared to a variable in game. I want to be able to use this new variable for my mod calculations.
 
Last edited:
Not exactly sure what your trying to do but operators work in sql.
Eg. SELECT * FROM GameSpeeds WHERE CostMultiplier > '100'
 
Your example doesn't show a new variable being introduced.
CostMultiplier is a variable
MonthIncrement is a variable
TurnsPerIncrement is a variable
... however I want to create a new variable that is not existing in the game. Not a table, just a variable I can re-use.
 
You can use CREATE TABLE to do this.

In the example below we create a table to hold variable data. Then we add a variable to that table with an INSERT. Then we access the variable in a subquery during an UPDATE assignment.

Code:
CREATE TABLE tblMyVariables
(
   xId             string PRIMARY KEY,
   xValue       string
);

INSERT INTO tblMyVariables
           (xId, xValue)
VALUES('custom_Cost_Multiplier', '1.5');


UPDATE Units SET Cost = Cost * (SELECT xValue FROM tblMyVariables WHERE xId= 'custom_Cost_Multiplier');
 
Last edited:
I am not looking into creating a table, I just want to keep it simple and limited to only 1 new variable.

How would that be done? I am sure the process is quite simple, I just need a modder to understand my original request and keep it simple! Please and thank you.
 
I am not looking into creating a table, I just want to keep it simple and limited to only 1 new variable.

How would that be done? I am sure the process is quite simple, I just need a modder to understand my original request and keep it simple! Please and thank you.


You need to create a table to store the variable. I'm not aware of any other way to work with variables in SQLite. If there's a way to do it, I have not seen it.
 
I don't really see any logical way to proceed except to hard-code the original known base-game values for all rows within a given table. Only then can you reliably know for sure whether some other mod has already alterred the data for that table.

If some other mod removes a row from the table or alters multiple columns within each row for multiple rows within the table, then I am not sure that hard-coding the original structure of the entire table will even work. Updates with WHERE clauses will probably be broken in such a case, as probably will SELECT FROM WHERE statements, simply because matching values to the "WHERE" requirements might no longer even exist.

If you simply pull the data for a Column within a Row of a Table you cannot know whether it has already been alterred from the original base-game data. Your code will pull whatever is currently set as an argument for that column for that row and will execute upon that value, but your code cannot know whether it was the original value or not without some form of hard-code of the original value stored somewhere either within a new game-table or within a new column you add to an existing table. If it were me, I'd do as suggested by @isau, and create an entirely new table to store these "hard-coded" original values.

If another mod runs its code on the database after your mod runs its code on the database, you will need an SQL trigger to detect this and take action as needed. I have never verified that SQLite triggers for "AFTER UPDATE" actually work in Civ6, however. They do for "AFTER INSERT" and "AFTER DELETE" since Firaxis themselves use these types of triggers.
 
If I understand the question, the basic way to accomplish this is some variation of this:


CREATE TABLE tblMyVariables
(
xId string PRIMARY KEY,
xValue string
);

INSERT INTO tblMyVariables
(xId, xValue)
SELECT 'omeVarName', someField
FROM someTable;

UPDATE someOtherTable SET someOtherField = (SELECT xValue FROM tblMyVariables WHERE xId= 'varName');



You don't strictly have to create a new table for this. You could re-use an existing one. However I personally discourage this. Create a table with a name that very clearly identifies it as your custom table. This helps prevent mod conflicts. Plus it means you can open up SQLite Studio and spot check your mod by writing SELECT statements against your custom table.



This isn't like in C++ or wherever where you can create a variable like:

int nIndex;


To store a value you need to identify what table it will be stored in. That is unless there is some hidden variable feature in SQLite I'm not aware of (I use it strictly in a hobbiest way.)
 
Here is a specific example from my Combined Tweaks mod. This setting sets how many turns it various diplomatic actions will last.


Setting up a table to hold variables:

Code:
-- Create a table that is used to store game options
CREATE TABLE tblQuoOptions
(
 OptionID text PRIMARY KEY,
 Value text,
 OptionClass text
);


INSERT INTO tblQuoOptions
    (OptionID,             Value,     OptionClass)
VALUES        ('QUO_OPTION_DIPLOMACY_TIMER_DURATION',    '-99', 'QUO'), 
    ('QUO_OPTION_ROCKETBOOTS',    '-99', 'QUO'),
    ('QUO_OPTION_EXTRA_SIGHT',    '-99', 'QUO');


In another file: Providing a place for people to set the variable. You wouldn't do this. You'd grab and set the variable with a SELECT statement instead.

Code:
-- ================
-- DIPLOMACY TIMERS
/* Set Value to the duration that diplomatic actions, such as denouncements, friend pacts, defense pacts, etc should last.
This setting does not affect the duration of trade agreements or open borders, and does not take game speed into account.
The unmodded value is 30. Recommended value for standard speed as of the Rise and Fall expansion is 40. */
UPDATE tblQuoOptions
   SET Value = 40
 WHERE OptionID = 'QUO_OPTION_DIPLOMACY_TIMER_DURATION' ;



This is how the variable is actually applied:

Code:
UPDATE GlobalParameters SET Value=(SELECT tblQuoOptions.Value FROM tblQuoOptions WHERE OptionID='QUO_OPTION_DIPLOMACY_TIMER_DURATION') WHERE Name='LEVY_MILITARY_TURN_DURATION' ;
UPDATE GlobalParameters SET Value=(SELECT tblQuoOptions.Value FROM tblQuoOptions WHERE OptionID='QUO_OPTION_DIPLOMACY_TIMER_DURATION') WHERE Name='DIPLOMACY_DECLARED_FRIENDSHIP_TIME_LIMIT' ;
UPDATE GlobalParameters SET Value=(SELECT tblQuoOptions.Value FROM tblQuoOptions WHERE OptionID='QUO_OPTION_DIPLOMACY_TIMER_DURATION') WHERE Name='DIPLOMACY_DEFENSIVE_PACT_TIME_LIMIT' ;
UPDATE GlobalParameters SET Value=(SELECT tblQuoOptions.Value FROM tblQuoOptions WHERE OptionID='QUO_OPTION_DIPLOMACY_TIMER_DURATION') WHERE Name='DIPLOMACY_DENOUNCE_TIME_LIMIT' ;
UPDATE GlobalParameters SET Value=(SELECT tblQuoOptions.Value FROM tblQuoOptions WHERE OptionID='QUO_OPTION_DIPLOMACY_TIMER_DURATION') WHERE Name='DIPLOMACY_ALLIANCE_TIME_LIMIT' ;
 
Top Bottom