Jump to content

papyrus beginner - how to add to global variable?!


Recommended Posts

Hi, I've only started learning Papyrus this week, so I don't know if this is a problem with a very obvious solution, sorry, but I haven't found anything that works yet!

 

I've made a global variable (Marirel) and I'm trying to add to it when a line of dialogue is played. My current script in a fragment, which I've tried to put together through seeing other people's scripts (and the creation kit website), is currently:

 

GlobalVariable Property MarirelProperty auto
Float GetValue()
Float Mod(Float 2)
According to the Creation Kit, there is 'no viable alternative at input GlobalVariable' and 'at input MarirelProperty', and a loop isn't matching anything at an input. I'll admit I find coding incredibly hard, so I'm just not sure how to fix this. Any suggestions would help me so much!
Edited by nakarisaune
Link to comment
Share on other sites

Remove the current script from the fragment and start over.

 

The script has to exist before you can add a property to it. To do this simply enter "; " and compile. This will create the fragment block and create the fragment script. Click on the properties button and add your new property through the interface rather than typing it in. Unlike full scripts, script fragments are functions on a larger script. Property declarations cannot exist inside of a function. Once your property has been added, remove the ";" and replace with the following:

NameOfGlobalVariable.Mod(2.0)

Of course replace "NameOfGlobalVariable" with whatever name you used.

Link to comment
Share on other sites

Remove all the code, except for a ; (semicolon), which indicates a comment. Compile the script. After that, close the window with OK and open again.

 

Now you can add the property by clicking the 'add property' button. Type: GlobalVariable, Name: MarirelProperty

 

If the property is created sucessfully, paste this code into the box, compile.

 

MarirelProperty.SetValue(MarirelProperty.GetValue() + 1)

Don't forget to actually fill the property and always test with a clean save.

 

edit: gnaah, too late...

Edited by Ghaunadaur
Link to comment
Share on other sites

Remove the current script from the fragment and start over.

 

The script has to exist before you can add a property to it. To do this simply enter "; " and compile. This will create the fragment block and create the fragment script. Click on the properties button and add your new property through the interface rather than typing it in. Unlike full scripts, script fragments are functions on a larger script. Property declarations cannot exist inside of a function. Once your property has been added, remove the ";" and replace with the following:

NameOfGlobalVariable.Mod(2.0)

Of course replace "NameOfGlobalVariable" with whatever name you used.

Ahh, thank you so much! I didn't quite understand what people were doing talking about properties, or the difference between fragments and scripts, so that will be really useful, thank you. I've tested and now this part of my mod is finally working!!

Link to comment
Share on other sites

MarirelProperty.SetValue(MarirelProperty.GetValue() + 1)

And don't ever do that with a global variable. If you're changing by a fixed amount (+1, -1, +50, etc.) use the Mod function instead.

MarirelProperty.Mod(1)

Using Mod will run faster and be thread-safe too.

Link to comment
Share on other sites

"Don't ever" is a bit strong. Either way we are talking about faster than human perception, so speed isn't really a consideration in most circumstances, and thread-safety is only a concern if more than one script may be modifying the variable at the same time. CDCooley is correct that using Mod() is the best practice, but there's no need to spread panic.

Link to comment
Share on other sites

Why not? Will it break the game?

No, it's just slower. Mod is best practice but either of them work in the end.

Actually it might break the quest involved which is exactly why using Mod is more than just "best practice".

 

"Don't ever" is a bit strong. Either way we are talking about faster than human perception, so speed isn't really a consideration in most circumstances, and thread-safety is only a concern if more than one script may be modifying the variable at the same time. CDCooley is correct that using Mod() is the best practice, but there's no need to spread panic.

Faster than human perception has nothing to do with it. The fact that Mod is faster (and less to type) is just a bonus.

 

The important part is thread safety. Most people don't know what that is and can remain happily ignorant if they just use Mod. Panic is what happens when people's mods don't work for reasons they don't understand.

 

No published example should follow what is known to be bad practice. That just gets people into trouble. That's why I responded in this case.

 

The GetValue/SetValue scheme will probably work fine 99% of the time but Mod works 100%. And when problems do appear it's generally extremely hard to identify the cause so I stick by my original statement that you should always use Mod and never use the GetValue/SetValue method.

Link to comment
Share on other sites

From a compiled code point of view there is little to no difference.

When you call mod() it is doing exactly what the other call set up is "Property.SetValue(Property.GetValue() + 1)".

(with a few more micro fast reg calls to the cpu) In the end one is a bit more tidier.

Edited by NexusComa
Link to comment
Share on other sites

  • Recently Browsing   0 members

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