• Home
  • Design worlds
  • Download
  • Sources
  • Documentation

SourceForge.net Logo

Start - Back - Forward


Entering your name

As stated before, in the first map the user should have to enter his/her name. We are going to implement this behaviour in WissenSchaf(f)t?.

The apropriate code will be placed into Name.txt. Now, you can include that file in the driver:

#Name.txt

But since this file is still empty, it will not work, though. So we have to code the new map...

Making it work

The first draft of this file is:

karten:

name = 5 10
w	w	w	w	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=start	w
w	w	w	w	w

This code tells the system to create the map name 5 fields width and 10 fields heigh. In the following those fields are specified referencing their prototypes (which have been defined in the driver).

The =start tells WissenSchaf(f)t? to give this coordinate the name start, which is referenced in the driver as our starting position.

Now you should already have a fully functional world, which can be viewed using WissenSchaf(f)t? by opening index.txt. Make sure it works as expected.

The door

Having this working so far, we can create the door; it will be locked until the user enters his/her name, but we'll care about that feature later. First, we need a new prototype placed right before the map defining the door field:

/* Prototypes needed for this map */
prototypen:

// Door which lets you out, but only when allowed
n_d =	Tuer("Images/Teleporter.png", "Images/Bricks.png", "Images/Bricks.png")

The class Tuer (in net.daniel_kraft.wissenSchafft.standard) is the prototype of a field which teleports the user on entrance to a chosen destination. Additionally, it can be locked via its API. The first two parameters give the images for unlocked/locked state, the third one is optional, and gives the background.

Having this prototype now, we can update our map:

karten:

name = 5 10
w	w	w	n_d(j, shift_name)	w
w	e	e	e=name_shift	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=shift_name	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=start	w
w	w	w	w	w

Notice the new coordinate names: name_shift will be the point where we'll come back from the next map, shift_name vice-versa is the entry point of that map; as it currently does not exist, I gave that name temporarly a freely chosen field of our current map.

The first parameter of the door instantiation is to be either j (for yes) or n (no), telling whether the door is unlocked (accessible) at the beginning; the second one is the target of the teleport (i. e., the position which is "the other side" of the door).

Try the new version, you should be able to beam yourself.

The funny guy

Now, as the door will be locked, we need a way to unlock it. Therefore we need a person to talk to, which will ask for the user's name, and then will grantfully open the door. Time for a new prototype:

// Here's the funny person, which asks for your name to open the door.
n_g =	DialogPerson("Images/SmileyN.png", "Images/SmileyE.png",
                     "Images/SmileyS.png", "Images/SmileyW.png", "Images/Empty.png")

As you can imagine, DialogPerson is a class of net.daniel_kraft.wissenSchafft.standard; it implements a person (which can be talked to, and which always looks at you) which is controlled by a DialogSkript. The first four parameters give the images for the person looking in any main direction, the fifth, optional one, is the background to composite the other image against.

Having this done, one can write a new map:

karten:

name = 5 10
w	w	w	n_d(n, shift_name)	w
w	e	e	e=name_shift	w
w	e	e	e	w
w	e	e	e	w
w	n_g(O, <n_gname>, <n_gscript>)	e	e=shift_name	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=start	w
w	w	w	w	w

The instantiation works as follows: The initial direction the person looks (N, O, S or W), the name of the person, here a reference to a (yet undefinied) string resource in angle brackets, and the script which controlls the person, also as yet undefinied string resource reference. Note also that the door state defaults to locked now.

Now, we are to define the two strings somewhere before the map:

/* Some strings for our friend */
strings:

n_gname =	"Funny Guy"
n_gscript =	@"Scripts/Name.ds"

The name is set explicitly, the script is set to the content of the file Scripts/Name.ds (as a script is usually a longer string); now you may try to run the world, but WissenSchaf(f)t? will claim it isn't able to find that file (as it doesn't exist).

DialogSkript

So here's the script (file Scripts/Name.ds):

/* Some imports for convenience */
import net.daniel_kraft.wissenSchafft.basis;
import net.daniel_kraft.wissenSchafft.standard;
import java.io;




/* Variables */

// The name we've chosen
var String name;

// Already chosen one?
var Boolean istGesetzt;

// Link to API
var Objekt@Welt w;




/* Methods */

// name has been set (either entered or loaded), refresh stuff based on it.
void nameSet()
{

 // Set string resource, so anyone in the world knows our name.
 w:getUserIStrListe():setString('name', name);

 // Refresh flag.
 set istGesetzt=true;

}

// Initialization, here we get our API reference.
void init(Objekt@Welt aW)
{
 set w=aW;
 set istGesetzt=false;
}

// Called whenever the person is spoken to. Ask for the name, if we don't already know.
void main()
{

 // First time? If so, ask for the name
 if(!istGesetzt)
 {
  out w:getUserIStrListe():getString('name_enter');	// Ask for the name
  in name;	// Prompt for it
  nameSet();	// Refresh other stuff
  w:getKarten():getKoordinate('name_door')
   :getFeld():getSpeziell()?Objekt@Sperrbar:setBetretbar(true);	// Open the door
 }

 // In any case, print out some message.
 out w:getUserIStrListe():getString('name_msg');

}

// Load & save the name when loading & saving the game. Door state is saved by itself.
void speichern(Objekt@DataOutputStream stream)
{
 stream:writeUTF(name);
}
void laden(Objekt@DataInputStream stream)
{
 set name=stream:readUTF();
 nameSet();
}

The API of WissenSchaf(f)t? or DialogSkript is beyond the scope of this tutorial (see the documentation for a full reference), but if you know a C-style programming language, you should be able to understand what is meant. Just look at the highlighted part: It queries for the coordinate named name_door and then unlocks it. So we have to name our door:

karten:

name = 5 10
w	w	w	n_d(n, shift_name)=name_door	w
w	e	e	e=name_shift	w
w	e	e	e	w
w	e	e	e	w
w	n_g(O, <n_gname>, <n_gscript>)	e	e=shift_name	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=start	w
w	w	w	w	w

When adding some new internationalized strings to Lang/en.txt, which are referred from the script, it should work:

/* **************************************************************** */
// Name map

name_enter =	"What's your name?"
name_msg =	"%name%, glad to meet you!"

The part between the percentage characters (%) is substituted the resource with that name, in this case the dynamically set user name.

Finally...

For the final version of this map, just remove the wrong name:

karten:

name = 5 10
w	w	w	n_d(n, shift_name)=name_door	w
w	e	e	e=name_shift	w
w	e	e	e	w
w	e	e	e	w
w	n_g(O, <n_gname>, <n_gscript>)	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e	w
w	e	e	e=start	w
w	w	w	w	w

Start - Back - Forward