Variables and Object Oriented Programming (OOP)

Post Reply
User avatar
Vinnie Terranova
Posts: 1259
Joined: 09 Nov 2017 10:24

Variables and Object Oriented Programming (OOP)

#1 Post by Vinnie Terranova » 03 Aug 2020 17:33

After creating some mods there are some things that I stumbled upon. It has all to do with Object Oriented Programming (OOP).

1. Variables overwriting each other due to loading order

I created two different mods which both make use of economy_data.sii:

Mod_A:

Code: Select all

SiiNunit
{
economy_data : economy.data.storage
{
...
var1 : default value
var2 : changed value   # modded
}}
Mod_B:

Code: Select all

SiiNunit
{
economy_data : economy.data.storage
{
...
var1 : changed value   # modded
var2 : default value
}}
The problem is, that if Mod_A has a higher priority loading order than Mod_B, Mod_B won't work as Mod_B.var1 is overwritten by Mod_A.var1.
And the other way around, If Mod_A has a lower priority loading order than Mod_B, Mod_A won't work as Mod_A.var2 is overwritten by Mod_B.var2.

To solve this, I could create a Mod_C:

Mod_C:

Code: Select all

SiiNunit
{
economy_data : economy.data.storage
{
...
var1 : changed value   # modded
var2 : changed value   # modded
}}
But that's not really what I want. I create small mods, and each mod should do just one thing and one thing only.
So, is there a way that var1 and var2 will not be overwritten, regardless loading order of Mod_A and Mod_B?
I was thinking about inheritance. But that led to another problem:


2. Inheritance

I decided to create two test mods, based on Mod_A and Mod_B (see above).

Mod_A_TEST

Code: Select all

SiiNunit
{
economy_data : economy.data.storage
{
var2 : changed value   # modded
}}
Mod_B_TEST

Code: Select all

SiiNunit
{
economy_data : economy.data.storage
{
var1 : changed value   # modded
}}
And here it comes: both mods contained nothing else... I deleted all other variables with their default values. The reason I did this was that actually mods only should contain the modded variables. And nothing else. After all: all other variables with the default values are stored in def.scs anyway.
So, when the game loads, it should load all the variables with their default values that are stored in def.scs. After that the game should load Mod_A_TEST and Mod_B_TEST, which should only change the values of var1 and var2. In this case all variables are loaded with their default values, except for var1 and var2 which are loaded with their modded values.

Unfortunately this doesn't work, as the game crashed (CTD). So I believe, if Mod_A_TEST or Mod_B_TEST don't contain ALL other variables with their default values AS WELL (besides var1 and var2), those other variables are not loaded from def.scs, and the game will crash...

But is there a way that Mod_A_TEST ONLY contain the modded var2 and Mod_B_TEST ONLY contain the modded var1, without all the other variables which are stored in def.scs anyway? That would make life much, much easier.


3. Scope of variables

I've created a mod that alters some values of a variable (in my case: kerb_weight[]) in each truck chassis (for instance: long2.sii). The problem is: I had to change that value in each of the 84(!) chassis files (7 trucks, each with 12 chassis files). In this case I had to edit 84 files. No big deal, but shouldn't there be another, easier way of editing 84 files in this repeating and boring way?

One possible(?) solution has to do with the scope of variables. I'm not sure, but I think that each chassis files starts with a so-called 'namespace' (for instance: accessory_chassis_data : long2.kenworth.t680.chassis).
Within that namespace there is this kerb_weight[] variable that I want to edit. The scope of kerb_weight[] is within that namespace. Is there a way to give kerb_weight[] a wider scope? Is there a way to change this local variable to a more global variable?
If there is, I could create a mod, that just contains kerb_weight[] and nothing else, and If I want to change the value of kerb_weight[] again, I could do this by editing just one file, instead of editing 84 files.


4. Editing values

Mostly, if I want to change a value of some variable, I can do that by just changing the default value into my modded value. But there are times that I want to be more flexible: sometimes I want to calculate the new value. So instead of default:

price: 28950 (default) -> price: 57900 (modded)

I want to do this by calculating:

price: 28950 (default) -> price = price*2 (modded)

Is this possible? This would also make life much easier.

User avatar
xXCARL1992Xx
Posts: 11356
Joined: 17 Aug 2016 12:18
Contact:

Re: Variables and Object Oriented Programming (OOP)

#2 Post by xXCARL1992Xx » 03 Aug 2020 17:49

you cant inject new "code" into the game, modding in ETS/ATS only works by replacing/adding new code, that is how it works in every game, you add something new or you replace something that already exists with your modifications in it

the way you want it means the game has to open the mod, look what is in it and then replace it on the fly in the original file with your value and this is impossible for all i know and no game does it in that way

basically, the game overwrites the whole file with the same name, it cant combine them or partially replace them

---

making the kerb weight a global value is also impossible because the value differs from chassis to chassis, it isnt a constant that is applicable for everything, it varies and is even over simplified in the games, normally tires, wheels, engines, gearboxes etc. should also change the weight of the truck but it doesnt here

you can try to do it via @include: kerbweight_kenworth.sui and include that in every Kenworth chassis with the values in it but this will just add confusion if you have to much include files

---

mathematical formulas inside def are also not doable to my knowledge because this is just plain text and not code, the game just sees "ah here is variable name: with the value Kenworth so replace name: in GUI to Kenworth"
Image
| !!!NO SUPPORT OR REQUESTS OF ANY SORT VIA PM!!! | Screenshot Thread | Steam Workshop | World of Trucks Profil |

Post Reply

Return to “Mods”

Who is online

Users browsing this forum: No registered users and 2 guests