Jump to content

Help working with structs


markyrocks

Recommended Posts

I'm attempting to save time and typing by using a struct. I'm needing to add in tons of properties on the script I'm working on. So I'm thinking if I can access the properties as elements in a struct it could save me typing and messing around in general. However I want to put the elements of the struct in an array so I could access a particular element via the index. This would also save typing and other messing around.

 

So I been kicking around a few ideas, seems like every path I think of turns up blank.

 

Basically I want

 

Struct mystruct

Element1

Element2

Element3

Endstruct

Var array[2]

array[0] = mystruct.Element1

array[1] = mystruct.Element2

And so on but I don't want to type all that out bc it defeats my purpose.

 

The only other thing that could solve my issue is if I can access the elements in the struct in a different way that returns the element. Like FindStruct() returns the index in an array that the struct is located. That does me no good.

It would be great if there was like a struct to array function. I could write my own function to do this if there was a simple way to cycle through the elements in the struct.

Link to comment
Share on other sites

I'm about to just write some helper scripts through autoit so just change the structs to strings an just output what I want. I'd be able to do something similar if papyrus allowed for dll calls.

 

Edit, incase anyone is wondering what I'm trying to do here. If I had say 20 variables declared, if I put them in a struct and added them to an array I could access them through passing one integer that pointed at an index in an array as opposed to writing a bunch of if statements to fo the same thing. I know something similar could probably be accomplished through a formlist but unless the list already exists I'd still have to make the list and manually enter in the values so it actually would add an extra layer to the whole thing. So not only would i be making many custom terminals, I'd be making formlists and then declaring them then pointing to specific elements. From a quick look at the formlist script it doesn't even seem that the functionality that I'm looking for even exists. You may ask why I wouldn't just write in the declarations ect but I'm dealing with at least a few hundred forms, might even be more than 1000. Not really sure. I'm slowly chipping away at it but its becoming apparent why this hasn't been done b4. At this point I've been relying on alot of copying and pasting but it's still alot more messing around than I want to do. The process could be alot more efficient.

Edited by markyrocks
Link to comment
Share on other sites

I'm not entirely convinced this is the right way for you to do what you want to do. I haven't wrapped my head around what you want to do yet. Anyway, your initial example of what you want is basically on the right path. The definition of a struct is pretty straight forward. The internal elements as you call it have to be defined types so

Struct TheStructBase
    String someString
    int iSomeInt
    float fSomeFloat
    ObjectReference myReference
EndStruct
 
; somewhere else in code
 
TheStructBase myStruct = new TheStructBase ; define a single instance of the struct
myStruct.iSomeInt = 12345
 
 
; or as an array
TheStructBase[] myStruct = new TheStructBase[10] ; start with 10 entities in the array.
myStruct[0] = new TheStructBase ; don't forget to initialize the entity or you get an error
myStruct[0].iSomeInt = 12345

I'm not sure if that helps you get where you want to go with your project. I hope it's of some help at least.

 

Edit: After re-reading the OP again I think I understand that you want to define a struct and somehow have it then put into an array so each member of the struct is an array entity? There's nothing that will do that for you. You'd have to write some function yourself for it. I gather you've already figured that out. If you're going to do that I don't see why you wouldn't just access the struct member directly. You say you don't want to type stuff. If you're using any decent text editor it should have the ability to be setup to auto complete names for you to save typing. Notepad++ has that ability. I believe Atom does as well. Programming is all about typing. And using ambiguous names will just make it harder for you to figure out your own code if you have an error, or anyone else to troubleshoot with you.

Edited by BigAndFlabby
Link to comment
Share on other sites

A simple example:

Struct TerminalStruct
    Terminal TermRef
    ObjectReference TerminalObj
    float someFloat
EndStruct

Event OnInit()
    TerminalStruct[] Terminals = new TerminalStruct[128]
    int itr = 0
    while (itr < Terminals.length)
        Terminals[itr] = new TerminalStruct
        Terminals[itr].someFloat = Utility.RandomFloat(1.0, 100.0)
        itr += 1
    endWhile
endEvent
Edited by Reneer
Link to comment
Share on other sites

I appreciate the input guys but I think I have the issue solved.

 

The original problem was I was manually entering in hundreds of variables through the add property feature of a script attached to a terminal.

 

I figured if I could just put them in a struct I'd be able to somehow easily and through little typing convert the struct to an array. Well that's not possible without pointing the array at individual elements ect. So alot of typing. I've since written helper scripts that copy the names of these items (objectmods) and save them to a file. Once saved in the file I wrote other functions that put them into various formats such as adding the papyrus variable declarations and spitting it back out to the file so I can just copy and paste it into my script. Once in the script the ck editor wont automatically assign it the correct ingame id but if I hit the auto fill button it does. So essentially I've solved how to bulk add variables, provided that they all follow the same format.

 

But once I have the names in a file I also wrote a function that can automatically generate the code that can add the struct elements to an array. I'm not continuing down that path bc I'm just sticking with the original format that I was originally using bc I have a different function that just writes if elseif statements and adds the variables in where they need to go and adds a unique number so they can be called with one int function arg.

 

The reason I'm doing this is due to the format of terminals and the daisy chain to a quest stage and the limited functionality that it gives based on that format. Like I click on a terminal entry that points to a quest stage. That stage number corresponds to a kmyQuest function that corresponds to a function call in my script. Ect. I almost have the whole process automated. Basically I have to add in a couple unique numbers in my script (autoit) and it will automatically fill out an entire terminal form with 50 entries. Then I go to the quest form and it will fill in 50 entries ect. It's very nice.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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