Page 3 of 3

Posted: Sun Mar 21, 2004 2:18 pm
by dcoshea
Hi bdbodger, thanks for your post. The code you posted looks perfectly fine to me. In ai_add_task, local.ai is a reference to an object of type Actor, I assume, and you create some fields called 'sequence', 'action', 'parm1', etc., that are each one-dimensional arrays.

What I would call strange is if you were treating local.ai itself as an array by doing say 'local.ai[local.ai.index]["sequence"]', since local.ai is of type Actor, not "two-dimensional array".

To confirm my theory that doing something like '$player[local.player_index][local.some_other_index]' is not possible, I used the following code:

Code: Select all

		for (local.i = 1; local.i <= $player.size; local.i++) {
			local.player = $player[local.i]

			// test 1: access a normal field of the Player object
			iprintln_noloc "Player " local.i " health is " local.player.health

			// test 2: treat the Player object as an array and try and set the value of the first element of the array
			local.player[1] = 123
			iprintln_noloc "...value of testing_array_index " local.player[1]

			// create a constant array and make local.const_array refer to it
			local.const_array = 1::2::3

			// make local.var_array refer to a variable array
			local.var_array[1] = 1
			local.var_array[2] = 2

			// test 3: treat local.const_array as an object
			iprintln_noloc "Accessing classname of const array: " local.const_array.classname

			// test 4: treat local.var_array as an object
			iprintln_noloc "Accessing classname of var array: " local.var_array.classname
Here is the output I got when that code ran:

Code: Select all

Player 1 health is 100.000
Test 1: As we'd expect, we were able to read a field from the Player object.

Code: Select all

...value of testing_array_index player
Accessing classname of const array: NIL
Accessing classname of var array: NIL
			local.player[1] = 123 (maps/obj/obj_team4.scr, 71)
			local.player^

^~^~^ Script Error: [] applied to invalid type 'listener'

			iprintln_noloc "Accessing classname of const array: " local.const_array.classname (maps/obj/obj_team4.scr, 77)
			iprintln_noloc "Accessing classname of const array: " local.const_array^

^~^~^ Script Error: Cannot cast 'const array' to listener

			iprintln_noloc "Accessing classname of var array: " local.var_array.classname (maps/obj/obj_team4.scr, 78)
			iprintln_noloc "Accessing classname of var array: " local.var_array^

^~^~^ Script Error: Cannot cast 'array' to listener
Tests 2, 3 and 4 failed. Note that the output is out-of-order - the iprintln_noloc output comes before the corresponding error messages.

Test 2: You can see that when I tried to set local.player[1] it said that [] should not be applied to a 'listener', or object, since it's meant to be applied to an array.

Test 3: Here you can see that I couldn't get the classname of the constant array because it's of type 'const array', not 'listener'.

Test 4: Similarly you can see I couldn't get the classname because it's of type 'array'.

So, I am happy to say that these results prove that MOH's scripting language has a conventional type system where a variable is of a particular type and cannot be simultaneously of two different types (i.e. an array and an object). I would have been freaked out otherwise :)

Regards,
David

Posted: Sun Mar 21, 2004 4:03 pm
by bdbodger
Well when you do this local.player = $player[local.i] local.player is = to an entity or listener and is not any array so local.player[1] = 123 is invalid because it can not cast a value to an entity . you could I think do local.player[local.i] = $player[local.i] . You lost me on the rest of it I mean variables don't have classnames . I am not exactly sure on the syntax but if you did do local.player[local.i] = $player[local.i] then you could do

local.player[local.i].action[1] = 123
local.player[local.i].action[2] = 456

but not sure if you can do local.player[local.i][action] = 123 or maybe local.player[local.i].[action] = 123 but that seems wrong . Entities are handled a bit different than normal arrays but they can store values in an array type structure .

Posted: Mon Mar 22, 2004 1:30 am
by dcoshea
bdbodger wrote:Well when you do this local.player = $player[local.i] local.player is = to an entity or listener and is not any array so local.player[1] = 123 is invalid because it can not cast a value to an entity .
Exactly.
you could I think do local.player[local.i] = $player[local.i] .
Yes, and if I did it in a loop I guess I'd have an array with exactly the same contents as $player at the point in time when it was created.
You lost me on the rest of it I mean variables don't have classnames .
I was trying to prove the point that MOH has a sensible typing system where an array is an array and not a listener, and a listener is a listener and not an array.
I am not exactly sure on the syntax but if you did do local.player[local.i] = $player[local.i] then you could do

local.player[local.i].action[1] = 123
local.player[local.i].action[2] = 456
That's not astounding stuff - creating an 'action' array attached to the Player object.
but not sure if you can do local.player[local.i][action] = 123 or maybe local.player[local.i].[action] = 123 but that seems wrong .
That's my point, it seems wrong, but you suggested this in some previous code:

Code: Select all

$player[1]["somestring"][1] = 10
Assuming $player is an array, then $player[1] is an object of type Listener, not an array, so I can't see how you could apply ["somestring"][1] to it. I think my test proved it won't work.
Entities are handled a bit different than normal arrays but they can store values in an array type structure .
What do you mean by that? Are you talking about how if you have more than one entity with the same targetname, there is an automatic array created where each element refers to an individual entity with that targetname?

Regards,
David

Posted: Mon Mar 22, 2004 5:23 am
by bdbodger
ok the syntax $player[1]["somestring"][1] = 10 is not right but

local.allies_spawner = spawn human/allied_airborne_soldier.tik origin $friendly_spawn[randomint(friendly_spawn.size)+1].origin targetname "test"

if(level.allies == 8)
thread tester
}

tester:
for(local.i=1;local.i<=$test.size;local.i++)
{
$test[local.i].something[1]= local.i
iprintln $test[local.i].something[1]
}
end

That works the entity can hold values or keys as they are called and those key can be arrays . You can create an array and have one element of that array be = 2 an entity . but you are right in saying that you can't use the enitity as an array although $player[1] etc is part of a targetname array . So the syntax should have been $player[1].somestring[1] = 10 not $player[1]["somestring"][1] = 10

Posted: Mon Mar 22, 2004 8:11 am
by dcoshea
Thanks bdbodger for putting up with my persistence in wanting to make sure we both have the same understanding of how variables work in the language :)

Regards,
David