Jump to content

Global Array Possible? Best Alternative?


Lunalle

Recommended Posts

I've written a script that has a number of variables (currently in an array) which is about 25 members long. I'd like to just slap the array in to global space so it will be retained across event firings. I really don't want to expand my code and use the UI to make 25 different global variables, but I don't see an alternative at this point. So I'm asking for some confirmation: is there a way to do this, and if not is there a different shortcut?

 

Thanks

Link to comment
Share on other sites

If you are talking about a single script. Define your array in the empty state, then redefine and fill your array as needed. Then you can use it anywhere else on your script.

 

Here is an example where I use an array to hold on/off states for a series of MCM toggle options:

Defined in the empty state

Bool[]  ComponentActive

redefined and filled in the OnConfigInit event

    ComponentActive = new bool[14]
    ComponentActive[0] = false  
    ComponentActive[1] = false  
    ComponentActive[2] = false  
    ComponentActive[3] = false  
    ComponentActive[4] = false  
    ComponentActive[5] = false  
    ComponentActive[6] = false  
    ComponentActive[7] = false  
    ComponentActive[8] = false  
    ComponentActive[9] = false  
    ComponentActive[10] = false
    ComponentActive[11] = false
    ComponentActive[12] = false
    ComponentActive[13] = false

Then I use them wherever they are needed throughout the MCM script.

Link to comment
Share on other sites

Doesn't work for me... here's my test code:

Scriptname Test2 extends ReferenceAlias

float[] damage2

event onConfigInit()
	damage2 = new float[20]
endevent

event onSpellCast(Form akSpell)
	Player.DamageActorValue("CarryWeight", 1.0)
	Damage2[0] = Damage2[0] + 1 ;not sure why Damage2[0] += 1 throws a comiler error =\
	Debug.MessageBox("Total Dmg Caused: " + Damage2[0])
endevent

In game it displays 1 every time I cast, instead of incrementing. It seems I'm missing something rather basic here. :embarrassed:

Link to comment
Share on other sites

Uhhm... okay. Still no dice here.

Scriptname Test2 extends RefrenceAlias
float[] CastCount
int i = 0

event onConfigInit()
	CastCount = new float[20]
	while i < 21
		CastCount[i] = 0
		i += 1
	endwhile
endevent

event onSpellCast(Form akSpell)
	float oldValue = CastCount[0]
	float newValue = oldValue + 1
	CastCount[0] = newValue
	Debug.MessageBox("Number of Spells Cast: " + CastCount[0])
endevent

That should be the full code for a script to count the number of times the player casts a (any) spell. You can try it out for yourself; for me the messagebox always says 1.

Link to comment
Share on other sites

1. Surprised it even compiled as OnConfigInit() is an MCM event and should not be recognized by a ReferenceAlias script

2. Why do you even need an array for this?

 

This does what you want without the array hassle

Int NumSpellsCast = 0

Event OnSpellCast(Form akSpell)
  NumSpellsCast += 1
  Debug.MessageBox("Number of spells cast: "+NumSpellsCast)
EndEvent
Link to comment
Share on other sites

1) Well... that's rather concerning. It does seem to work though.

2) No array is needed for this, it is a simplified proof of concept example.

 

Thank you for your help on this. I found a workaround! I don't understand what's happening exactly, but if I use an int type, it works as expected. When changing back to float, the value stays at (or possibly is reset to) 1.000. I'll just list this under limitations of the scripting language and move on with my life. :geek:

Link to comment
Share on other sites

Okay, I lied, I can't let it go... also, I kind of need to store floats =\

 

Please, for the love of Stendarr, someone explain this to me. It's been hours, and obviously my years of programming have not prepared me for scripting in papyrus.

 

Here's what I've found through testing:

Scriptname Test2 extends ReferenceAlias

int i = 0 ;iterator
int gf = 0
int[] CastCount ;array to keep track of different types of spells cast

event onInit()
	CastCount = new int[20] ; init
	while i < 21 
		CastCount[i] = 0 ; starting vals
		i += 1
	endwhile
	Debug.MessageBox("Reset") ; never seen
endevent

event onSpellCast(Form akSpell)
	gf += 5 ; value of CastCount[0] increases by 5  TAKING THIS LINE OUT CAUSES THE OUTPUT TO BE STATIC | what does 'gf' have to do with 'CastCount' ?!
	int oldValue = CastCount[0] 
	int newValue = oldValue + 2  ; output starts at 7, and increases by 5
	CastCount[0] = newValue ; newValue is presumably x + 2, where x = number of times script has run
	Debug.MessageBox("Number of Spells Cast: " + CastCount[0]) ; 7, 12, ...
        ; CastCount[1] shows the same values as CastCount[0]
        ; a bunch more code
endevent

Thanks in advance to anyone who can clear up this mess.

 

EDIT: My guess would be that the array is never getting its own memory allocation, and has a memory allocation that is shared with gf. Hence when updating gf, the array also updates. This also explains why when there is no gf, the array value remains static (presumably, it would work with any variable that was declared immediately adjacent to the array (sharing memory)). I suppose where that leaves me, is in need of a working function to hook when the player starts/loads my mod.

I'd think there'd be an onmodinit or some such function, but I have yet to find it. creationkit.com is useful, but not without its bugs, and certainly not with all the answers.

 

Cheers

Edited by Lunalle
Link to comment
Share on other sites

Skip the array. Create 20 global variables and put them all into a single formlist.

FormList Property GVList Auto

Event OnSpellCast(Form akSpell)
  (GVList.GetAt(0) as GlobalVariable).Mod(1.0)
  Debug.MessageBox("Number of spells cast: "+(GVList.GetAt(0) as GlobalVariable).GetValue())
EndEvent

Arrays have their uses but manipulating the values the way you want seems to be more trouble than it is worth. Global variables in a formlist could be more flexible. Plus you can access them on other scripts if need be.

 

If you really want to use an array, see if setting the empty state declaration up as a hidden property would help keep values in memory.

float[] Property CastCount Auto Hidden
Link to comment
Share on other sites

I 'think' I know why your float is not storing properly. All of the math you are doing is with Int values, not float. Change the oldvalue/newvalue to type float and use 1.0 (I have noticed that floats require a decimal in papyrus).

 

That said, I agree with Ishara, if you are actually trying to count spell casts, there are easier ways. If you are just trying to do something simple to learn about floats, try my suggestion.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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