Array/data structures library/plugin?

Information and Discussion Relating to NWN2 Scripting and Coding

Moderators: Moderator, Developer, DM

User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Array/data structures library/plugin?

Unread post by gedweyignasia »

Does anyone know of some library or plugin for the toolset that adds arrays/data structures to NWScript? Maybe someone out there made a DLL for it or something?

Edit: Found array support.
http://sourceforge.net/projects/stdnws/files/
Last edited by gedweyignasia on Sun Feb 08, 2015 6:50 pm, edited 1 time in total.
NegInfinity
Posts: 2449
Joined: Wed Feb 05, 2014 11:24 am

Re: Array/data structures library/plugin?

Unread post by NegInfinity »

AFAIK, it supports simple structures.

Code: Select all

struct strTint
{
	int iRed;
	int iGreen;
	int iBlue;
	int iAlpha;
};
There's also SetLocalArrayInt/SetLocalArrayString line of functions.

It is unlikely that there is a library that adds proper arrays into core language, cause you need to hook the parser for that, which is gonna be very hard.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

I'm looking for something that adds 2-dimensional arrays and hopefully some other data structures like linked lists, tree structures, etc. into the language.

It doesn't have to be "proper" arrays, I just have to know the size of it and be able to access. You don't need any fancy hooks to change the syntax, just give me a way to create 2-dimensional arrays.

To make a faction system pretty, it'd be nice to have an array of objects or an array of creatures.
NegInfinity
Posts: 2449
Joined: Wed Feb 05, 2014 11:24 am

Re: Array/data structures library/plugin?

Unread post by NegInfinity »

I suggest to try jamming whatever it is you're trying to do into MySQL database. nwnx4 server has sql support via either mysql or sqlite. This will save you from working with arrays in language that doesn't have them.
gedweyignasia wrote:I'm looking for something that adds 2-dimensional arrays and hopefully some other data structures like linked lists, tree structures, etc. into the language.

It doesn't have to be "proper" arrays, I just have to know the size of it and be able to access. You don't need any fancy hooks to change the syntax, just give me a way to create 2-dimensional arrays.

To make a faction system pretty, it'd be nice to have an array of objects or an array of creatures.
I suggest to take a look at 2da files. Get2DAString. You put all data you need into text file *.2da, and then you can access that from within script.

Emulating arrays via plugin will be difficult, because it is a scripting language. You COULD write a dll that provides you arrays, but you'll need to explicitly manage lifetimes of those arrays, otherwise you'll get massive memory leaks, fast, and manually managing lifetime of anything in nwnscript is gonna be painful.

You have GetLocalArrayInt/String line of functions. Which kinda gives you 1d array. When you have 1d array, you can emulate array of any size with it.

To emulate 2d array (numRows, numColumns) using 1d array, you need 1d array with size numRows*numColumns. Offset of individual element will be column + row*numColumns.

However...

in nwn script there is no debugger. Which means that if you try to do heavy-duty programming there, it is gonna be painful.

I suggest to look at sql-based solution.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

Wow, completely spaced on how to fake 2d-arrays. Thaaaat's a good solution, just have to make functions to access it so there isn't ugly math everywhere.

Still need an object array, unless there's a way to uniquely identify objects that may or may note exist using integers or strings. (It's that or have every object store its own information, which turns into a gross conversation.)
Luna
Retired Admin
Posts: 7945
Joined: Sat May 16, 2009 3:00 pm

Re: Array/data structures library/plugin?

Unread post by Luna »

If you have any luck on array support, let me know.

I wanted one to fix some issue's with Kemo Scry.
Think of like putting everyone's name in array and then sorting it.
I don't remember if I couldn't figure it out or just didn't have time to look into arrays further.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

Luna wrote:If you have any luck on array support, let me know.

I wanted one to fix some issue's with Kemo Scry.
Think of like putting everyone's name in array and then sorting it.
I don't remember if I couldn't figure it out or just didn't have time to look into arrays further.
Oh, that's a thought... You'd want to sort by various columns? (Each row is a struct, I'd imagine, you'd want to be able to populate an array with a given field, sort by that, then rearrange the structs to display in that order?)
Luna
Retired Admin
Posts: 7945
Joined: Sat May 16, 2009 3:00 pm

Re: Array/data structures library/plugin?

Unread post by Luna »

Nah, just sort it alphabetically.
Throw everyone's name into the array and sort it.
Which from a programming language point of view is a pretty dang simple thing to do.

But I don't know how to do arrays in nwn2. :x
NegInfinity
Posts: 2449
Joined: Wed Feb 05, 2014 11:24 am

Re: Array/data structures library/plugin?

Unread post by NegInfinity »

gedweyignasia wrote:Wow, completely spaced on how to fake 2d-arrays. Thaaaat's a good solution, just have to make functions to access it so there isn't ugly math everywhere.

Still need an object array, unless there's a way to uniquely identify objects that may or may note exist using integers or strings. (It's that or have every object store its own information, which turns into a gross conversation.)
I still highly recommend to look into databases. Because for faction it is a natural way to represent things.

tables:
factions (faction name, faction id, faction description)
members (faction id, member id)
relations (faction id, faction id, relation type)

This kind of thing. Much more natural than arrays. Will persist across resets easily, too.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

I thought database access/modification was a slow/memory-intensive operation in NWN? If not, it'd be ideal for doing a faction system; otherwise you'd basically be writing a crummy database in NWScript.
NegInfinity
Posts: 2449
Joined: Wed Feb 05, 2014 11:24 am

Re: Array/data structures library/plugin?

Unread post by NegInfinity »

gedweyignasia wrote:I thought database access/modification was a slow/memory-intensive operation in NWN? If not, it'd be ideal for doing a faction system; otherwise you'd basically be writing a crummy database in NWScript.
I'm talking about SQL database (sqlite/mysql), not NWN database.

Take a look at nwnx4 demo modules. They have sql examples.
User avatar
Rasael
Retired Staff
Posts: 8096
Joined: Wed Jun 30, 2010 6:52 am
Location: Leiden, Netherlands

Re: Array/data structures library/plugin?

Unread post by Rasael »

A database read / write is always more expensive in NWN2 than a local read write of a variable to an item.

It gets more expensive also if you try to store objects. Ints, strings and floats are pretty optimized. But objects require more memory. Both in SQL SCOR/CO as with the campaign database. As Ivan wrote BG uses SQL for object storage.

A faction system could probably best be done by storing it on the kemo scry item. And then the first time it is used by a PC / NPC relationship you copy those variables onto the PC himself. To avoid searching the inventory for the kemo item all the time. On logout you can just transcribe it back onto the kemo. Or on-rest. (or both, even better)

The real issue lies in the NPC reactions. Because players can't actually belong to another faction than the PC faction. And the faction related functions don't all work, or work in unexpected ways. (not as they should). That's what caused Ivan much grief when he wrote his code for it. It doesn't work like in NWN1.

I've been meaning to take a look at his code and tinker with it. To see if I can get it working.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

Couldn't you manage how much you wanted a PC to be liked/disliked, and use Personal Reputation instead of faction reputation to make a PC disliked when necessary?
User avatar
Rasael
Retired Staff
Posts: 8096
Joined: Wed Jun 30, 2010 6:52 am
Location: Leiden, Netherlands

Re: Array/data structures library/plugin?

Unread post by Rasael »

I think ivan found weird behaviour with that. It is my choice of approach. But i think i remember him saying it didnt work on all creatures.
User avatar
gedweyignasia
Custom Content
Posts: 1353
Joined: Sat Nov 23, 2013 11:27 pm
Location: EST/UTC-4

Re: Array/data structures library/plugin?

Unread post by gedweyignasia »

Return to “NWN2 Scripting”