1

I have to find a way to create objects dynamically, that means the user can decide how many objects to create once the program starts. What I tried to do is:

                if count = 6 then
                create player1.player
                create player2.player 
                create player3.player
                create player4.player
                create player5.player
                create player6.player
                  elseif count > 4 then
                    create player1.player
                    create player2.player 
                    create player3.player
                    create player4.player
                    create player5.player
                      elseif count > 3 then
                        create player1.player
                        create player2.player 
                        create player3.player
                        create player4.player
                          elseif count > 2 then
                            create player1.player
                            create player2.player 
                            create player3.player
                           else
                             create player1.player
                             create player2.player 
               end

Once the user has choosen the number of players, the variable count gets updated and the feature that creates the objects gets called.

I used this kind of brute force method, instead of a loop, because I need the "names" of the objects, I have to called them again in the program.

Anyways the compiler gives me a VEVI error, variable is not properly set. Some help?

DevX10
  • 483
  • 2
  • 6
  • 16

2 Answers2

1

You have this compiler error because your player1 to player6 are declared as attached (it is the default). If a variable or attribute can be void during the execution of the program, you must declare it as detachable. For exemple:

player1: detachable PLAYER

After that, each time you want to use your variable or attribute, you have to test it's attachment (even if you know it is attached, the compiler don't) like this:

if attached player1 as la_player1 then
    ...
    do_something_with(la_player1)
    ...
end

This way, the compiler will know that you used an attached object and will compile without giving you any VEVI error.

Louis M
  • 576
  • 2
  • 4
0

If all the variables playerN are attributes of a class declared as attached (that may be the case if they are of a type like PLAYER provided that class types are attached by default, or of a type like attached PLAYER otherwise), they all are expected to be initialized at the end of a creation procedure.

Assuming that some of the attributes can be void, the corresponding type declaration should look like detachable PLAYER.

In the example there are 2 variables that are always attached to objects and 4 variables that may or may not be attached to an object. Therefore they need to be declared as

player1, player2: PLAYER
player3, player4, player5, player6: detachable PLAYER

if the sample code appears in a creation procedure and player1 and player2 are always initialized.

Then player1 and player2 can be accessed unguarded, but for other players the code similar to

if attached player5 as p then
   p.whatever
end

will be required.

If all player attributes can be void, all of them need to be declared as detachable PLAYER.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thanks a lot for the answer, but can you tell me if there is a more efficient way to do this? Is it possible with a list and a loop, instead of using if statements? – DevX10 Jan 07 '16 at 21:08
  • @Ergo, of course, you can store the values in a container, for example, in an `ARRAYED_LIST [PLAYER]`. You will create the container object and then put as many player objects into it as required. BTW, in that case `detachable` mark will not be required. (I can extend my answer with more details if needed.) – Alexander Kogtenkov Jan 07 '16 at 21:35
  • thanks and sorry for the late answer, but I would know more about the container. Why isn't detachable needed. – DevX10 Jan 14 '16 at 22:41
  • And how do you access the separate objects once stored in the container?? – DevX10 Jan 14 '16 at 22:42
  • @Ergo, there is nothing special about storing attached or separate items in a container. Because you can access only elements that have been added, it is sufficient to declare the type with the corresponding actual parameter, e.g. `ARRAYED_LIST [separate PLAYER]`. Then you can add and access the elements as usual. Because only existing elements can be accessed, no additional checks for attachment status will be required in that case. – Alexander Kogtenkov Jan 15 '16 at 07:49