Halstrom Posted February 19, 2018 Share Posted February 19, 2018 Any idea how to set the value of a global variable or float property from a script? It keeps telling me they are read only. Link to comment Share on other sites More sharing options...
SKKmods Posted February 19, 2018 Share Posted February 19, 2018 1. Check the Global variable form itself doesn't have the CONSTANT checkbox checked 2. If it doesn't, when you define it as a property for the script do not include CONST GlobalVariable Property pSKK_Debug Auto Const Mandatory ; read onlyGlobalVariable Property pSKK_Debug Auto Mandatory ; read/write Link to comment Share on other sites More sharing options...
Halstrom Posted February 19, 2018 Author Share Posted February 19, 2018 1. Check the Global variable form itself doesn't have the CONSTANT checkbox checked 2. If it doesn't, when you define it as a property for the script do not include CONST GlobalVariable Property pSKK_Debug Auto Const Mandatory ; read onlyGlobalVariable Property pSKK_Debug Auto Mandatory ; read/writeCool, thanks, I thought it would be something simple like that :) Link to comment Share on other sites More sharing options...
isathar Posted February 19, 2018 Share Posted February 19, 2018 The const flag on GlobalVariables actually has nothing to do with the script-side flag. The flag for GlobalVariables sets its value to be constant, so any changes to them can't be saved. I'm not sure if its purpose is memory optimization or just locking the value to its default. It's possible to create an override for the globalvariable to disable the const flag, but I'm not sure if there are any long-term negative effects since we don't know how they're handled by the game engine. The const flag in scripts is mostly for memory optimization, and will prevent changes to the variable or forms used as properties. If the GlobalVariable property will only be used for its default form and you're not planning to set it to a different GlobalVariable in the script, use const in its definition. It won't prevent you from changing the globalvariable's value in the script. Link to comment Share on other sites More sharing options...
SKKmods Posted February 20, 2018 Share Posted February 20, 2018 So here is the thing, I just this minute set the const checkbox on a GlobalVariable Form and Const in the properties and: [02/20/2018 - 12:28:50AM] error: Cannot set the value of a constant GlobalVariable Link to comment Share on other sites More sharing options...
isathar Posted February 20, 2018 Share Posted February 20, 2018 (edited) Yep, that's what the globalvariable form's const flag does. The script-side flag is unrelated to this - it just marks the property/variable as a constant so that its contents (the globalvariable form the property points to) can't be changed dynamically (or swapped out for another globalvariable) . It's possible to create an override of the globalvariable's form in a separate plugin and clear the const flag on the override to allow editing it - but, as mentioned, no clue if there are any negative effects to doing this. Edited February 20, 2018 by isathar Link to comment Share on other sites More sharing options...
SKKmods Posted February 20, 2018 Share Posted February 20, 2018 To produce elegant and maintainable code it seems sensible to align the const property to use and form state, no ? Link to comment Share on other sites More sharing options...
Halstrom Posted February 20, 2018 Author Share Posted February 20, 2018 Thanks guys, that sorted that issue, the next issue I have is I'm trying to copy the global variable to a float: fBatteryLevel = gBatteryLevel as float and getting the error "cannot cast a globalvariable to a float, types are incompatible" Link to comment Share on other sites More sharing options...
isathar Posted February 20, 2018 Share Posted February 20, 2018 You'll need to call the GlobalVariable function GetValue() for that. fBatteryLevel = gBatteryLevel.GetValue() should work. If you plan to edit the GlobalVariable's value, use SetValue(YourNewBatteryLevel). A description of the functions you can use on GlobalVariables can be found here. SKK50: I'm not sure what you mean. As I mentioned, the two flags are unrelated, so it's not about elegance but functionality.Here's a description of the GlobalVariable form's const flag, and here's the same for Papyrus - explained in a much better way than I can pull off. Link to comment Share on other sites More sharing options...
Halstrom Posted February 21, 2018 Author Share Posted February 21, 2018 (edited) Next question :smile: Is there an equivalent to IsMoving, I want to detect if the player is moving. I have IsRunning and IsSprinting, but there's no IsWalking. Edited February 21, 2018 by Halstrom Link to comment Share on other sites More sharing options...
Recommended Posts