Variables in Object Reference

Say I have "player.stamina"

Can I use a variable to reference that? Say I have a string variable called mname. Can I say mname.stamina to reference player.stamina? If so, how do I do it?


K.V.
msg("{player.stamina}")

...or

msg(player.stamina)

...or

msg("Stamina: "+player.stamina)

...or

msg("Stamina: {player.stamina}")

no no. I mean, what if I have a function that references several objects and I want to pass it a parameter to reference an object. So in the above examples if I wanted the parameter (we'll call the parameter mname) to be in those messages to be player:

msg (mname.stamina) where mname would be the passed parameter of player


You can do this:

mname = player
msg(mname.stamina)

Or send player as an argument to a function, and have mname as a parameter of the function, then do this:

msg(mname.stamina)

Not sure what you mean by "messages to be player" though.


What I mean, is, if the object was called wildwolf and I wanted to pass that object name to the function combat:

combat ("wildwolf")

then in the function:

<function name="combat" parameters="mname" type="string">

Then inside that function I'd like to do:

msg (mname.stamina)

But, when I try to use that in a msg to create a table. As such:

<table style="width: 75%"><tr><th>Combantant</th> <th align="center">Skill</th> <th align="center">Stamina</th> <th align="center">Luck</th></tr> <tr> <td><b><font color="blue">Player</b></font></td> <td align="center">{player.skillcurrent}</td> <td align="center">{player.staminacurrent}</td> <td align="center">{player.luckcurrent}</td> </tr> <td><b><font color="red">{mname.alias}</b></font></td> <td align="center">{mname.mskill}</td> <td align="center">{mname.mstamina}</td></tr> </table>

In the table instead of the alias, stamina and skill of the object, it is printing {mname.alias}, {mname.skill} and {mname.stamina}


You need to send the object, not its name, to the function, so:

combat (wildwolf)

When it has quotes around it, it is a string.


Well, that fixed some errors. But it is still not working right in the table. I get this:

Combantant Skill Stamina Luck
Player 8 21 9
{mname.alias} {mname.mskill} {mname.mstamina}


The other problem is that the text processor cannot use local variable, which includes parameters from functions. When you do the msg command, the bit of Quest that is handling that has no idea what mname is.

The best way around that is to insert the values into the string, something like:

"The thing is called " + mname.alias + "."

If mname is an object reference, you can use:

msg ("The thing is called " + mname.alias + ".")

as Pixie suggests.

Using a variable that contains the name of an object is generally a bad idea; but there might be some circumstances where that's what you've got (for example, the GetExit functions return the name of an exit, not an actual object). In that case, there's two ways to do it:

  1. msg ("The thing is called " + GetObject(mname).alias + ".")
  2. msg ("The thing is called {" + mname + ".alias}.")

I'm not 100% sure, but I think the second one of those will be more efficient.


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


if/when you can, using the text processor commands is very convenient/nice/great/wonderful/easier-(for more simple stuff anyways)/quicker/etc


however, you can always fall back on and use the "normal" scripting too (as well as thus also being able to use very useful concatenation):

the secret trick: break it into chunks:

"TEXT"
+ VARIABLE +

for example:

player.alias = "Joe"
player.age_int = 18
player.age_str = "adult"
player.sex = "male"
player.race = "human"
player.class = "warrior"

// output/displayment:

Joe is a 18 year old adult male human warrior.

using text processor commands:

msg ("{player.alias} is a {player.age_int} year old {player.age_str} {player.sex} {player.race} {player.class}.")

using "normal" scripting:

msg (player.alias + " is a " + player.age_int + " year old " + player.age_str + " " + player.sex + " " + player.race + " " + player.class + ".")

the chunks: 12

01. player.alias +
02. "(SPACE)is(SPACE)a(SPACE)"
03. + player.age_int +
04. "(SPACE)year(SPACE)old(SPACE)"
05. + player.age_str +
06. "(SPACE)"
07. + player.sex +
08. "(SPACE)"
09. + player.race +
10. "(SPACE)"
11. + player.class +
12. "(DOT/PERIOD)"

or, if this makes more sense:

the chunks (and CONNECTORS: +): 23

01. player.alias
02. (CONNECTOR_OF_STRING-VARIABLE): +
03. "(SPACE)is(SPACE)a(SPACE)"
04. (CONNECTOR_OF_STRING-VARIABLE): +
05. player.age_int
06. (CONNECTOR_OF_STRING-VARIABLE): +
07. "(SPACE)year(SPACE)old(SPACE)"
08. (CONNECTOR_OF_STRING-VARIABLE): +
09. player.age_str
10. (CONNECTOR_OF_STRING-VARIABLE): +
11. "(SPACE)"
12. (CONNECTOR_OF_STRING-VARIABLE): +
13. player.sex
14. (CONNECTOR_OF_STRING-VARIABLE): +
15. "(SPACE)"
16. (CONNECTOR_OF_STRING-VARIABLE): +
17. player.race
18. (CONNECTOR_OF_STRING-VARIABLE): +
19. "(SPACE)"
20. (CONNECTOR_OF_STRING-VARIABLE): +
21. player.class
22. (CONNECTOR_OF_STRING-VARIABLE): +
23. "(DOT/PERIOD)"

That made no sense. lol But Pixie's example worked. Thanks!


I was just trying to explain how to correctly do the syntax for the "normal" scripting method, as it's very confusing for non-coders or for beginners at coding.

when I first started... I had trouble with it... and the "secret trick of breaking it into chunks", made the syntax easy for me to get correct without errors.


here's the comparison of the two methods:

for an example:

player.alias = "Joe"
player.age_int = 18
player.age_str = "adult"
player.sex = "male"
player.race = "human"
player.class = "warrior"

if you want this output/displayment of:

Joe is a 18 year old adult male human warrior.

to get it coded/written/syntax'ed correctly without errors:

  1. using text processor commands:
msg ("{player.alias} is a {player.age_int} year old {player.age_str} {player.sex} {player.race} {player.class}.")

or

  1. using "normal" scripting:
msg (player.alias + " is a " + player.age_int + " year old " + player.age_str + " " + player.sex + " " + player.race + " " + player.class + ".")

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

Support

Forums