Setting a name (Gamebook)

Hi guys. I'm a total newbie with this stuff.

Im working on a game were I would like the player to have the option to set a name for something.

For example, the main character might have a pet dog. When someone plays the game, they are prompted to type a name for the dog. Throughout the game the dog is then named what ever was input by the player.

I'd really appreciate any advice on how this can be achieved.


Greetings and welcome to Quest! I'm no expert, but I think I can help.

Here is some code that will do this. It's script code, so you will have to change your page to "Text + Script" or "Script." Find the "code view" button in the new section dedicated to script and paste in the code. It works by asking the player for input (in this case the name for a dog), and it will save what you put in as the name {player.DogName}. I don't understand it too well myself, but as far as I can tell it basically sets an attribute to the player. By using {player.DogName}, the attribute is displayed.

msg ("What is your dog's name?")
GetInput() {
  player.DogName = result
  msg ("Your dog's name is {player.DogName}.<br/>")
}

I hope this is helpful, and if you want a better explanation, see this forum post: https://textadventures.co.uk/forum/quest/topic/4708/adding-player-name-in-game

Don't be afraid to search around the forums, there are a surprising amount of questions answered on here! If you have any other questions you'd like to ask me, feel free to do so!


(filler for getting my edited post, updated/posted)


Ninjagorulz has it right, just a little more into understanding Attributes (the 'Attribute' VARIABLE)....


hopefully, you guys/girls know algebra mathematics (VARIABLE usage), as that's basically what are Attributes

x = 10
x + 30 = ?
[10] + 30 = 40
40 = 40

or, as usually, as you got to find the value of the variable:

x = ?
x + 30 = 40
x = 10
[10] + 30 = 40
40 = 40

however, with (Object-Oriented) programming/coding, we got Objects, which can have/store their own Attributes, which store/hold Values, just like how in algebra mathematics, we have variables holding/storing Amount (integers/non-decimal-numbers or double/floats/floating-points/decimal-numbers) Values, but with programming, we can also hold/store Non-Amount Values: Strings, Booleans, Object References/Pointers, Scripts, Lists, Dictionaries, and etc

this is just like the real world, for an example:

Object: john

A (String) Attribute of the 'john' Object: hair_color

The (String) Value stored/held within/by the 'hair_color' String Attribute of/within/by the 'john' Object: "black"

john.hair_color = "black"

Object: jeff

A (String) Attribute of the 'jeff' Object: hair_color

The (String) Value stored/held within/by the 'hair_color' String Attribute of/within/by the 'jeff' Object: "brown"

jeff.hair_color = "brown"

which is the exact same as with algebra mathematics:

notice how instead of using the 'x,y,z,a,b,c,n,etc' for a VARIABLE (storing/holding a Value), we're using:

x = 10 ---> john.hair_color = "black"
y = 49 ---> jeff.hair_color = "brown"

x ---> john.hair_color
y ---> jeff.hair_color

10 ---> "black"
49 ---> "brown"

hopefully, you notice the difference between using an 'x' Variable VARIABLE and using an Attribute VARIABLE

there can be one and only one 'x' Variable VARIABLE:

x = 10
x = 20 // instead of storing '10', the same 'x' variable is now storing '20'

however, there can be lots of Attribute VARIABLES, as they're tied/associated/attached/connected to (held/stored within) Objects:

john.hair_color = "black"
jeff.hair_color = "brown"
jill.hair_color = "yellow"
jim.hair_color = "grey"
joe.hair_color = "white"

as you can see, we can have 5 'hair_color' String Attributes, as they're tied/associated/attached/connected to (held/stored within) various different Objects:

john
jeff
jill
jim
joe

john.hair_color = "brown"
// is completely different/separate from:
jeff.hair_color = "black"
// is completely different/separate from:
jill.hair_color = "yellow"
// is completely different/separate from:
jim.hair_color = "grey"
// is completely different/separate from:
joe.hair_color = "white"

just like in real life, in how:

john has black hair
jeff has brown hair
jill has yellow hair
jim has grey hair
joe has white hair

but you can't have multiple 'x' variables, each holding/storing different values:

x = 10
x = 20
x = 30
x = 40
x = 50

(instead of having 5 different 'x' variables, we just have a single 'x' variable, which is changing/setting the Value that it is holding/storing, from: 10 to 20 to 30 to 40 to 50)


so, you can hopefully see how useful an 'Object-Oriented' structure is, allowing for multiple Objects, each with their own Attributes and stored/held Values:

john.hair_color = "black"
john.age = 30
john.age_type = "adult"
john.dead = false
john.weight 200.36

jeff.hair_color = "brown"
john.age = 90
john.age_type = "adult"
john.dead = true
john.weight = 100.295879

jill.hair_color = "yellow"
jill.age = 10
jill.age_type = "child"
jill.dead = false
jill.weight = 93.7

jim.hair_color = "grey"
jim.age = 1
jim.age_type = "baby"
jim.dead = false
jim.weight 999.21435

joe.hair_color = "white"
joe.age = 15
joe.age_type = "teen"
joe.dead = true
joe.weight 0.36

and more of the same (we can use 'x' as an Attribute, in this example, the x's are Integer Attributes, as they're storing Integer Values, notice that these ARE four separate 'x' Attributes, as they're attached to Objects, unlike in algebra mathematics, which just have Variable VARIABLES, and not Attribute VARIABLES)

ball_1.x = 10
ball_2.x = 30
ball_3.x = 50
ball_4.x = 90

msg (ball_1.x)
msg (ball_2.x)
msg (ball_3.x)
msg (ball_4.x)

// output/displayment:
10
30
50
90

VS

x = 10
x = 20
x = 30
x = 40

msg (x)
msg (x)
msg (x)
msg (x)

// output/displayment:
40
40
40
40


lastly...

VERY IMPORTANT:

in programming, there's also the 'Assignment' Operation:

x = 10 // the '10' Integer Value is stored into the 'x' Integer Variable VARIABLE

player.strength = 100 // the '100' Integer Value is stored into the 'strength' Integer Attribute VARIABLE, which the 'strength' Integer Attribute VARIABLE, is stored within the 'player' Object

player.alias = "HK" // the "HK" String Value is stored into the 'alias' String Attribute VARIABLE, which the 'alias' String Attribute VARIABLE, is (also, as Objects can store multiple Attributes) stored within the 'player' Object

you can NOT switch/reverse an expression of an Assignment Operation around!

this is because the programming is programmed/parsed to understand it only in a certain syntax order/placement:

the Final Value on the right side of the '=' Assignment Operator is stored within the VARIABLE on the left side of the '=' Assignment Operator

for example:

x = 10 + 20 + 30 // the Final (Integer) Value of '60' is stored within the 'x' Variable VARIABLE

more examples:

player.alias = "HK" // NO error
"HK" = player.alias // ERROR!

player.strength = 100 / NO error
100 = player.strength // ERROR!

x = 10 // NO error
10 = x// ERROR!

whereas in algebra mathematics, unfortunately, they never explain that you're actually doing a 'Comparison' Operation, which is why you can switch/reverse the expression around:

in algebra math:

x = 10 // NO error
10 = x // NO error

x + 40 = 50 // NO error
50 = x + 40 // NO error

in programming, to do a Comparison Operation, you use (as its done within) the 'if' Script/Function (and there's some other Scripts/Functions that also do Comparison Operations):

if (x = 10) { /* scripting */ } // NO error
if (10 = x) { /* scripting */ } // NO error

if (player.strength = 100) { /* scripting */ } // NO error
if (100 = player.strength) { /* scripting */ } // NO error

if (player.alias = "HK") { /* scripting */ } // NO error
if ("HK" = player.alias) { /* scripting */ } // NO error

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums