Jump to content

"type mismatch" Issue


dagobaking

Recommended Posts

Hello. I'm trying to get this piece of Papyrus code to compile in FO4:
ObjectReference[] kActorArray = Game.GetPlayer().FindAllReferencesWithKeyword(ActorTypeNPC, 1000.0)


Var [] sendData = new Var[1]
sendData[0] = kActorArray

The compiler tells me that the last line is a type mismatch problem.

 

I have programming experience. But, am not familiar with the quirks of Papyrus.

 

Can someone help solve how to write that last line so that it compiles with the ObjectReference array data added to the sendData Var array?

Link to comment
Share on other sites

The simple answer is you're trying to make a multidimensional array, ie set a singular object of an array to a reference of an entire array. You can't point to an entire array when it expects a singular object. Hence the type mismatch.

 

From that code snippet you're basically making a multidimensional array. Since papyrus doesn't support multidimensional arrays that cannot be done. It has to be a singular object. So doing sendData[0] = kActorArray[0] would work. You could probably do something like sendData = kActorArray to have it reference the whole array. I've never tried assigning a var array to another array reference so I'm not sure of what qwerks that might have. It does not copy the array with that. However explicitly casting it on the assignment I believe does make the engine copy the array since it has to return entities of a specified type so it ends up duplicating them for the return value. Maybe this is a desired behavior. It would need testing to verify.

 

Just another point of note. You also cannot use arrays inside structs. However you can have an array of a struct. Or create dummy objects in the world with a script attached to them to work as a secondary array. Then have an array of those objects. Though this quickly becomes a memory management nightmare in that it has large potential for causing save bloat and eventually save corruption if it gets too heavy. Not to mention issues relating to persistence. I personally have decided this method is just generally a bad concept so I've never tried to actually use it.

Link to comment
Share on other sites

The simple answer is you're trying to make a multidimensional array, ie set a singular object of an array to a reference of an entire array. You can't point to an entire array when it expects a singular object. Hence the type mismatch.

 

From that code snippet you're basically making a multidimensional array. Since papyrus doesn't support multidimensional arrays that cannot be done. It has to be a singular object. So doing sendData[0] = kActorArray[0] would work. You could probably do something like sendData = kActorArray to have it reference the whole array. I've never tried assigning a var array to another array reference so I'm not sure of what qwerks that might have. It does not copy the array with that. However explicitly casting it on the assignment I believe does make the engine copy the array since it has to return entities of a specified type so it ends up duplicating them for the return value. Maybe this is a desired behavior. It would need testing to verify.

 

Just another point of note. You also cannot use arrays inside structs. However you can have an array of a struct. Or create dummy objects in the world with a script attached to them to work as a secondary array. Then have an array of those objects. Though this quickly becomes a memory management nightmare in that it has large potential for causing save bloat and eventually save corruption if it gets too heavy. Not to mention issues relating to persistence. I personally have decided this method is just generally a bad concept so I've never tried to actually use it.

 

Thank you for the insight.

 

Through F4SE, there is a workaround that allows multi-dimensional arrays: https://www.reddit.com/r/f4se/comments/6cwtkd/f4se_040_released_for_fallout_4_194_runtime/

 

But, I couldn't get that to work either due to the different type of Array.

 

I am sending the "sendData" array through another F4SE command that requires a Var Array. I tried just plugging in kActorArray instead and got a similar type error.

 

I also tried casting the Array as a Var. That allowed the script to compile. But, it seemed to lose the data. Maybe I didn't do the cast correctly...?

 

Hoping someone has an idea.

Link to comment
Share on other sites

If you're the one consuming the data on the other side, your best bet may be to simulate your own multidimensional array in a regular one. So if you had:

 

Array1a, Array1b

Array2a, Array2b, Array2c

 

You could convert it to something like:

 

2, Array1a, Array1b, 3, Array2a, Array2b, Array2c

 

And then pull it apart at the other side. If they're fixed length you won't need any counts, if they're not fixed but all the same length you could get away with a single count at the start.

Link to comment
Share on other sites

Thank you. It is data going through an F4SE command that can handle multi-dimensional arrays using VartoVarArray. So, that part is ok.

 

I solved the issue.

 

I was able to get it working by simply casting it as a Var[] like this:

kActorArray as Var[]

I had actually tried that before. I thought it was broken because it returned 0 results. But, the problem was that my FindAllReferencesWithKeyword line didn't work as expected. Once I changed the keyword it started working.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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