# Is it possible to check if variable declared?

This is a nerd question, but for the sake of making something ‘pluggable’ it seems useful to create a pattern like:

``````*if not(exists(character_strength))
temp character_strength 50
``````

This seems particularly useful in the context of gosub_scene…

2 Likes
``````*if strength != 50
*set strength 50
``````

Perhaps something like this? Just because you *create a variable at the beginning of the game doesn’t mean you can’t have one version of the variable be nothing at all… such as

*create strength 0

But this might not be what you mean… can you explain a bit more what you mean by ‘pluggable’?

I see what you’re saying, but I’m more imagining 'it’s temporarily this unless it’s already been set to something else somewhere"

You mean that strength becomes something else, rather than the number? Like if strength turns into dexterity, keeping the 50?

I think that, in the stat chart, you could do a

``````*if flag00X = 1
*percent strength strength
*if flag00X = 2
*percent strength dexterity
*if flag00X = 3
*percent strength constitution
``````

and so on, setting a *set flag00X condition each time it should change from one to another. Or…

``````*if ((flag00X = 1) and (strength != 50))
*percent strength strength
*if ((flag00X = 2) and (strength != 50))
*percent strength dexterity
*if ((flag00X = 3) and (strength != 50))
*percent strength constitution
``````

And then when putting *set flag00X after an *if strength != 50 statement, or creating a *set strength 40 if you do want it to change, then changing it back after it has, perhaps under another *if conditional.

Perhaps I’m complicating things- someone more proficient might be able to come up with a better solution… but maybe this helps?

Okay, let me try and break this up a bit.

Right now, as far as I can tell, a variable must exist globally and be defined in startup, or it must be a temp variable. Right?
Additionally you can’t ‘pass’ variables to other scenes. So, if you are going to contextualize one scene based upon a previous scene you have to have a variable to hold the context of the first scene and it has to be declared in startup, and it has to be referenced in the following scene.
If you want to focus on just the scene you are writing, without worrying that a non-linear path has set that variable to something else, you also have to set the variable.

Finally, imaging ‘hair_color’ which we’ll say is set to ‘blue’ at the beginning as a default, but now we want to randomly pick a color unless the character has dies their hair. Now we need another variable for ‘hair_has_been_died’. Right?

In javascript you can pass variables into a function (which is like a gosub), so the control can come from the calling scene. In PHP you can call isset and determin if it’s been set.

But I apologize. In the time sine I asked this question, I looked through the source and I don’t think you can. I might suggest a patch on the github page.

Thank you.

@Sheana You can do this easily with string variables. Set it to, say “unknown”. Then use *if (variable) !=“unknown” to tell if it’s ever been set.

You can do something similar with non-string variables. Say, if a variable always starts false, or always starts at 0, and then can change to something else, you can use >0 or if (variable) to see if it’s ever been set.

Whether it’s ever been called since that? I think you would have to use a second variable, set variable_called true the first time it could be called in that sequence.

Something like this should work in theory:

``````*temp is_declared false
*script temps.is_declared = (typeof stats.myvar != "undefined");
*if (is_declared)
Variable exists
``````

But you shouldn’t really be in a position where you need to check if a variable is declared, not in Choice script. Pre-define everything, actually design your game before you code it and you won’t have that problem.

1 Like

Maybe this is just me… but wouldn’t it just be a lot easier on everyone if Choicescript carried variables over from one scene to the next?

1 Like

It does, any *create variables carry over, it’s only *temp ones that do not.

So noted- I haven’t begun work on a second scene yet- I was scared for a moment there. ^_^’