Jump to content

Script: When to use "Const"


Zorkaz

Recommended Posts

The flag Const stands for constant, a constant can not be changed in run time, is a read-only value. Constants are usually used for a better optimization.

 

Well that's not 100% correct. You can modify constants. At least I know it works for global variables. If a global variable has a constant flag, you can modify it, but as soon as the player loads the game again, it resets to the constant/initial value. In other works modify constant global vars, but they don't save the value.

Link to comment
Share on other sites

Two things being mixed up for Global Variables.

 

(A) In the GlobalVariable form if the const flag is checked the value can not be changed outside of the creation kit editor. This is fact and has stopped several SKK mods from releasing as they don't change existing base game assets. Example: ServiceCostBuyHouse (the cost of the HomePlate key).

 

(B) In a script properties the Const flag means that the form that script variable is pointing to can not be changed outside of the creation kit editor, which is pointless because there is no mechanism for that anyway. The contents of that form may be able to change depending on the const flag on that form (see A).

GlobalVariable Property pServiceCostBuyHouse Auto
GlobalVariable Property pServiceCostBuyHouse Auto Const Mandatory

Take those two declarations. Neither will allow the script to change the value of ServiceCostBuyHouse as the FORM is flagged Constant.

GlobalVariable Property pSKK_CSETSettlersGetShootingPerks Auto 
GlobalVariable Property pSKK_CSETSettlersGetShootingPerks Auto Const Mandatory

For these two it really doesn't matter as the SKK_CSETSettlersGetShootingPerks FORM is not flagged constant so the script can change the value.

 

 

So, what is the POINT of having Const in a variable property ? Buggered if I know, but it does not stop the value being changed and that's a fact and should not be disputed as there are 994 of them in SKK scripts being run by over 500,000 unique users. So there.

Link to comment
Share on other sites

 

The flag Const stands for constant, a constant can not be changed in run time, is a read-only value. Constants are usually used for a better optimization.

 

Well that's not 100% correct. You can modify constants. At least I know it works for global variables. If a global variable has a constant flag, you can modify it, but as soon as the player loads the game again, it resets to the constant/initial value. In other works modify constant global vars, but they don't save the value.

 

 

Any form being pointed from a constant property can be changed, what you can not change is the form itself. So, following your example, you can change the value of the global that the property is pointing to, but you can't change the global (the form) to which this property is pointing.

To make it clear, I'll do the example with an Actor property, if you have a script with a constant actor property, and this property points to Valentine, and then you want to change this actor to Piper you can't, but you can change an actor value of the actor.

 

Even simpler to visualize: if you set a property for a constant integer, and you set this integer to 5 and, in run-time, you try to change this property to 10, you wont get it changed. Because you are trying to change the value of the property. But when the property is a form you are not changing the value, you are changing the form value.

Edited by DieFeM
Link to comment
Share on other sites

 

 

The flag Const stands for constant, a constant can not be changed in run time, is a read-only value. Constants are usually used for a better optimization.

 

Well that's not 100% correct. You can modify constants. At least I know it works for global variables. If a global variable has a constant flag, you can modify it, but as soon as the player loads the game again, it resets to the constant/initial value. In other works modify constant global vars, but they don't save the value.

 

 

Any form being pointed from a constant property can be changed, what you can not change is the form itself. So, following your example, you can change the value of the global that the property is pointing to, but you can't change the global (the form) to which this property is pointing.

To make it clear, I'll do the example with an Actor property, if you have a script with a constant actor property, and this property points to Valentine, and then you want to change this actor to Piper you can't, but you can change an actor value of the actor.

 

Even simpler to visualize: if you set a property for a constant integer, and you set this integer to 5 and, in run-time, you try to change this property to 10, you wont get it changed. Because you are trying to change the value of the property. But when the property is a form you are not changing the value, you are changing the form value.

 

 

I was not talking about scripts. That's correct. When you create or edit a global variable, you can check a constant flag in the CK. That's what I meant. You can still change the value, but it gets reverted. That was surprising to me. One would think, when you check the constant flag, you can't change it anymore. That's the only thing i wanted to point out :-)

Link to comment
Share on other sites

Using constants will make script run faster, but it's not necessary for anything from a functionality point of view. You can use constant or not in the script and it won't change what that script does. You are just telling compiler that vale/item you have declared won't change and can be treated/stored as such.

 

If you edit or add just script or two, it won't make any difference on performance, not one that player can notice, but will have effect when used on a large scale. Using constant where appropriate is sign of good programming.

 

As for it's use, you use it when you declare something that won't be changed in the course of running the script.

Edited by hruza
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...