Skip to content

Tutorial Variable Creator

Kamran Wali edited this page Dec 2, 2024 · 6 revisions

Tutorial Variable Creator

I have also added a feature that allows to share/use data in a performant way by extending the Resource script. For now there are 4 categories of data share and each have their own different data types.

Fixed Vars

In this category different type of data types are shared, example bool, float, int, string etc. You only need to create one fixed var and share it with multiple objects, example - If five objects needs an int value of 1 then create a fixed var of type int that has the value 1 and share that. In that way only one int value of 1 is created instead of five which saves some memory. Like the name suggests the values are fixed and can/should NOT be updated. Otherwise it defeats the purpose of its function. Only call the get_value() function to get the value and do NOT change the property _value through script.

Managers

This category shares different type of managers instead of data types. Managers are scripts that have a bit of complex logic to it and these manager resources helps to share the references to those managers in a decoupled way. In this case unfortunately Variable Creator will only create managers that are created in CodeOptPro but if you want to create your own custom manager then you can use the manager_helper_template which is under the Resources while creating a script. The manager that is going to be referenced MUST be the only one that calls the method void set_manager(manager) and provides self as reference. Other scripts using this manager helper resource reference MUST only call the method get_manager() and then use the manager's methods from there. You can check out the script cop_update_manager_global_helper to see how the manager resource script is coded.

  • COP_PoolHelper - This manager's reference type stores and uses any script that is a child of base_pool. For now the scripts pool_global and pool_local can be stored and used by this reference helper script.
  • COP_UpdateManagerGlobalHelper - This manager's reference type stores and uses any script that is a child of base_update_manager. For now the scripts physics_process_manager_local, physics_process_manager_global, process_manager_local and process_manager_global can be stored and used by this reference helper script.
  • manager_helper_template - For creating a new COP_Helper type you can simply use the script templates that are already present in the addon. Go to the folder addons -> kamran_wali -> code_opt_pro and then copy the folder script_templates. Paste the copied folder to the root folder res://. Now you can use the script templates to create a new COP_Helper. Just create a new script and make sure to Inherit from Resource. Then in the Template section select Resource: Manager Helper Template. Give the script any name you want and finally create it. Now in the script make sure to give it a class name if you want to which has been commented out. For the _manager, change it to any script type you want. For the get_manager() method make sure to give it a return type as well which may help with performance a bit. Finally for the set_manager(manager) method make sure the parameter has a type as well which may also help with performance.

Observers

This category shares different type of observers like bool, float, int and String. The variables in this category uses the observer pattern to send signals to the observer. Basically when the value in the observer changes then it will emit that change to the observers. The emit is done using Godot's Signals. The observers are similar to the Vars but the only difference being that the observers can emit signals. Read the Vars to understand how the values are stored.

Vars

Just like Fixed Vars this category shares different type of data types as well, example bool, float, int, string etc. The only difference is that you can NOT set any values here like Fixed Vars and the values may change through custom scripts. Vars basically share values that are constantly changing. For example - You have 5 objects that wants to know the player's position. Then just create a COP_Vector3Var and make the player script constantly update the newly created COP_Vector3Var. Then add the newly created COP_Vector3Var to the other 5 objects. Now all of those 5 objects have access to the player's position without the need of player script reference. Also use the functions get_value() and set_value(value) for getting and setting the value. Do NOT get or set the property _value directly through script as this may result in error later on. Below are all the types.

Variable Creator

For now the only way to create a new variable is to use the Variable Creator plugin. You can open the Variable Creator window by going to the menu Project -> Project Settings then select the Plugins tab and finally enable the Variable Creator. This will open the Variable Creator by docking it at the bottom right side. You can dock it how ever you wish. Below I will explain the highlighted parts of the Variable Creator.

Variable-Creator1.png
Variable Creator
  • a. Name - This is where you give the name of the variable you want to create. If name given already exists in the path then the newly created variable will replace the old one and it does not matter what type it was.
  • b. Path - This is the path or folder location where the new variable will be created. You can update this path as well. Follow the instructions in c. to see how to update the path.
  • c. Update Path - If you want to update the path where the new variable will be created then right click the folder where the variable should be created and select Copy Path. Paste the copied path in the path field, b.. Finally press the Update Path button and the path will be updated. This will only update the path for 1 variable type, in this case FixedBoolVar types. This way the Variable Creator will allow you to have different paths for different variable types. The default path is res://addons/kamran_wali/code_opt_pro/variables/.
  • d. Category - This is where you get to select from which category the variable will be created. For now there are 4 categories which are FixedVars, Managers, Observers and Vars.
  • e. Variable Type - This is where you get to select which type of variable to create. Each category have different type of variable types.
  • f. Create Variable - This button will create the new variable type. Remember to give a name to the variable otherwise this button will NOT be visible. Also the name of the button Create Variable will change with the variable type selected so that you will know what type you are creating.

Clone this wiki locally