Jump to content

Photo

papyrus beginner - how to add to global variable?!

papyrus global variable

  • Please log in to reply
14 replies to this topic

#1
nakarisaune

nakarisaune

    Stranger

  • Members
  • Pip
  • 5 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, 22 August 2016 - 05:31 PM.


#2
IsharaMeradin

IsharaMeradin

    The Pale Redguard

  • Members
  • PipPipPipPipPip
  • 10,549 posts

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.



#3
Ghaunadaur

Ghaunadaur

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,222 posts
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, 22 August 2016 - 05:45 PM.


#4
nakarisaune

nakarisaune

    Stranger

  • Members
  • Pip
  • 5 posts

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!!



#5
cdcooley

cdcooley

    Resident poster

  • Premium Member
  • 4,820 posts
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.



#6
lofgren

lofgren

    Resident poster

  • Supporter
  • PipPipPipPipPip
  • 3,408 posts

"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.



#7
Ghaunadaur

Ghaunadaur

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,222 posts

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.


Why not? Will it break the game?

#8
FrankFamily

FrankFamily

    The Evil Blacksmith

  • Supporter
  • PipPipPipPipPip
  • 7,090 posts

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



#9
cdcooley

cdcooley

    Resident poster

  • Premium Member
  • 4,820 posts

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.



#10
NexusComa

NexusComa

    Faithful poster

  • Members
  • PipPipPipPip
  • 1,670 posts

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, 24 August 2016 - 10:59 AM.






Also tagged with one or more of these keywords: papyrus, global variable

IPB skins by Skinbox
Page loaded in: 1.125 seconds