problems with arrays

Post » Wed Jan 05, 2011 4:31 am

This is my first attempt at OBSE arrays. I read the info about this on the wikki and I gotta say is rather poor. I'm still uncertain about 85% of the stuff below. Hopefully you guys can see what is wrong with my code.


scn  MyScriptarray_var tScaleshort counterfloat timeRateshort currentScalefloat newScalebegin function { }	let tScale:= ar_construct array	set counter to 30	set timeRate to 12	set currentScale to myQuest.myQuestVar	while (counter > 0)				let tScale[counter] := timeRate               		set counter to counter - 1		set timeRate to timeRate -0.4	loop			if (currentScale <= 30)		let newScale := tScale[currentScale]    ; I suspect this statement is incorrect	endif		printc " newScale is: %.2f", newScaleEnd


The script compiles, but it spits a bunch of errors on the console once in game :(

basically what I'm trying to do is store a series of values between 0 and 12 in an array.
then, I want to access any of the stored values and store any of them in a float var.
User avatar
lauren cleaves
 
Posts: 3307
Joined: Tue Aug 15, 2006 8:35 am

Post » Wed Jan 05, 2011 6:13 am

Your script looks good, except for one fatal error. In your initialization loop, you try to assign the array elements starting with element 30, then downwards to element 1, which is illegal.

Here's the description of Arrays from the OBSE documentation:
An Array behaves like arrays in most programming languages: the key is an unsigned integer starting at zero, and there are no gaps between elements. (In other words, if an element exists at indexes 1 and 3 then an element necessarily exists at 0 and 2). Attempting to access an element using a key which is greater than the highest key in the array results in an error. The only exception to this rule is during assignment: it is okay to assign a value to the key which is one greater than the highest key in the array.


What this translates to, is that after construction of the array, the only element you are allowed to assign a value to, is element 0 - and when you have done that, so that element 0 exists, you can assign to element 1, etc. Trying to assign a value to element 30, means that you try to make a gap in the array (no elements 0-29 yet). So change your initialization loop to something like:
        set counter to 0        set timeRate to 0        set currentScale to myQuest.myQuestVar        while (counter < 30)                                let tScale[counter] := timeRate                               set counter to counter + 1                set timeRate to timeRate +0.4        loop

Note that this will make the array go from elements 0-29 and not 1-30, so your check must reflect that as well.

If you need gaps in the array, make it of type Map instead of type Array, but that is a much less effective array type, and not needed here.
User avatar
KRistina Karlsson
 
Posts: 3383
Joined: Tue Jun 20, 2006 9:22 pm

Post » Tue Jan 04, 2011 5:12 pm

Well, that did the trick alright :celebration:

You know for being made for a very high-level programming language, this particular OBSE addition has a fairly high degree of complexity. :eek:

I think that : int myArray[30] is a hell of a lot easier, but then again some folks say that set myVar to myVar + myVar is easier than myVar ++ but hey, at least we have arrays right? :intergalactic::flamethrower:

lol, Thank you !!
User avatar
Jani Eayon
 
Posts: 3435
Joined: Sun Mar 25, 2007 12:19 pm


Return to IV - Oblivion