Unlock door

Iron Mike
09 Mar 2013, 15:44
I have tried to include the need to enter a code to unlock a door, following the instructions in the 'How To' guide, but when I run the game I get this message:
> use keypad
Please enter the security code
Error running script: Error compiling expression 'code=game.code': CompareElement: Operation 'Equal' is not defined for types 'String' and 'Int32'
Can anyone tell me where I'm going wrong? I've checked and double checked my input, comparing it to the guide, but can't see what to change.
Thank you

TriangleGames
09 Mar 2013, 16:07
Based on the error message, it sounds like you are comparing a string (letters) to an integer(numeric ONLY). You could try changing the value of "code" to a string, or converting the player input to an integer with the function "ToInt".

EDIT: This is the wiki entry for ToInt. It also points out that if the player input is not actually a number, ToInt will result in an error. You can check the input first with "IsInt."

MORE: I've just noticed that the GetInput() function used in the guide is not supported in Quest 5.4.
If you're using 5.4 beta, that might be part of problem. There is a new script command for Get Input. Using the new Get Input script command will look like this:
Code with Get Input script command.png

levicki
09 Mar 2013, 19:24
Iron Mike wrote:I have tried to include the need to enter a code to unlock a door, following the instructions in the 'How To' guide, but when I run the game I get this message:
> use keypad
Please enter the security code
Error running script: Error compiling expression 'code=game.code': CompareElement: Operation 'Equal' is not defined for types 'String' and 'Int32'
Can anyone tell me where I'm going wrong? I've checked and double checked my input, comparing it to the guide, but can't see what to change.
Thank you


If your script is using "Get Input" then the input will be a string while you defined your game.code as an integer.

You can either use

if (ToInt(result) = game.code) {
// TODO: it is the same
}
else {
// TODO: not the right code
}

Or you can change your game.code to be of type string if you want to use direct comparison and avoid unnecessary conversions.

sonic102
09 Mar 2013, 19:28
Could we see your game file/code please? That may help.

You should note that player input is always considered to be a string, so it would work if game.code was a string. Have yountried that?

HegemonKhan
09 Mar 2013, 22:15
Mike's new, so he's not familar with using the Code View or working with Coding, yet.

I think a simple solution would be if you:

go to your game attribute "code", and (you might need to delete it and make a new one) change it's type to a string (strings are merely a bunch or letters and~or numbers, for example, these would be strings: 1234 or redblue or 1red2blue).

so:

Game -> Attributes (Tab) -> your created "code" attribute (scroll to find it, and then click on it so it is highlighted) -> (delete it)

Game -> Attributes (Tab) -> Add ->

name: code
type: string (I presume you had choosen integer here for your old game attribute, "code")
value: (whatever your correct code number sequence is, lol)

-- if you can simply change the type, then you don't need to delete your "code" attribute and make a new "code" attribute, lol. Only if you can't change the type, do you then need to delete it and make a new one. I can't remember off the top of my head.

----------------

I just found~learned this out myself recently, lol

when you use an equal sign, you can't have one side be a string type and the other side be an integer type:

string = integer (doesn't work, "string and int32" error)

string = string (works)

integer = integer (works)

the game engine doesn't care about the values, it cares about the type you set for the attribute:

so even though your "code" attribute's value is (let's say): "1234", your type (presumably) was set to integer (int), while your "get input" script (code) is (by the game engine) set to be a string, and thus string = integer (error).

when you want to use numbers for your attribute's value, you can either use the types: integer or string

and in this instance, you need to set the type to string, and not to integer

TriangleGames
10 Mar 2013, 02:43
The way it is set up in the wiki's How To guide, one way the code variable would end up as an integer instead of a string, would be if you left out the quotation marks where the code is defined. In an expression, those quote marks tell the computer that what's in them is text, i.e. a string.
From Security Code wiki page.png

HegemonKhan
10 Mar 2013, 04:31
good spot Triangle! Quotes matter, whether you're suppose to use them or not, makes a difference in whether your code~stuff works or not. Just get the quotes wrong in an expression and you got an error with, lol ;) (HK still hates syntax with writing expressions, I still have trouble getting it right, hehe)

or when you forgot to type them in for scripting.. lol

<function name=attack_function>
<defending type=boolean>true</defending>

though my biggest bad habit is forgetting the ">" at the end, lol (it can take a while to find it, when you got a lot of code, well at least before I was using the note++ anyways, lol)

<function name="attack_function"
yada yada yada code
</function>

Iron Mike
10 Mar 2013, 21:11
Thank you all for your advice. I'm sorry I haven't replied earlier - I've been trying all of your suggestions, but without success. I've set a variable - game.code - to be a random number between 1000 and 9999. This can be read by the player if he looks. Later he needs to enter the number into a keypad to unlock a door. It seems to be the comparison of the player's input with the variable game.code that's causing the problem. I don't know how to use code, so in the GUI I entered

Keypad - Use/Give
set variable code = player input
if expression code = game.code
then
unlock the exit .... and so on

Sorry, I don't know how to give you a screen shot.

I guess code and game.code need to both be integers, but I don't know how to do it. I can only work in the GUI so one word answers would be much appreciated!! Thank you

levicki
10 Mar 2013, 22:46
Iron Mike wrote:Thank you all for your advice. I'm sorry I haven't replied earlier - I've been trying all of your suggestions, but without success. I've set a variable - game.code - to be a random number between 1000 and 9999. This can be read by the player if he looks. Later he needs to enter the number into a keypad to unlock a door. It seems to be the comparison of the player's input with the variable game.code that's causing the problem. I don't know how to use code, so in the GUI I entered

Keypad - Use/Give
set variable code = player input
if expression code = game.code
then
unlock the exit .... and so on

Sorry, I don't know how to give you a screen shot.

I guess code and game.code need to both be integers, but I don't know how to do it. I can only work in the GUI so one word answers would be much appreciated!! Thank you


In the place where you assign game.code to random number:

set game.code = GetRandomInt(1000, 1999)

Change expression to read:

set game.code = ToString(GetRandomInt(1000, 1999))

Also, if you created game.code as integer, please go to game attributes and change it to string and then it should work.

HegemonKhan
11 Mar 2013, 10:00
following the guide, you should have this:

Game -> Script (Tab) -> Start Script -> Add A Script ->

Variables -> Set A Variable or Attribute -> Set Variable game.code = [EXPRESSION] ToString (GetRandomInt (1000,9999))

------------------------

otherwise:

you must be making some kind of mistake, it's probably a minor~small mistake, as it should be working from~by using our examples. Did you switch the Print A Message to [EXPRESSION] from [MESSAGE] where you are suppose to, for example (this is a common thing to forget and~or miss doing) ???

it's time for a slight intro into the coding, hehe:

in the GUI, at the top, there should be a button that looks like a notepaper, between the play and ? (help) buttons.

this notepaper button is a toggle to go back and forth from the GUI mode and Code View mode.

so press this notepaper button, so you are now in Code View mod.

highlight all of it, copy it, and then paste it here, inside of this:

[code.](paste your game file code here)[/code.]

-- but without the dots in the two [codes]

it'll look like this:

(your pasted game file code)


this enables us to examine your game code, to see what you got wrong.

we can then provide your with the corrected game code, via the same way:

(our corrected code for you)


which you can highlight (by pressing the: select all), copy, and then paste into your game file's code (delete your entire old game file code first) or you can paste it into a new game too.

----------

another way to view the game code is to simply open up your game file, with note, note++, or word pad.

Iron Mike
11 Mar 2013, 14:06
Hallelujah!! Thank you all for the advice - some of which I nearly understood. The last advice was exactly what I needed. Thank you HegemonKhan - it now works perfectly. Thank you all

sharky3008
27 May 2013, 15:41
I have made my door and used ToInt, I have everything working perfectly except if the input is a string instead of integer, then it comes up with this:- Error running script: Error evaluating expression 'ToInt(result) = game.code': Input string was not in a correct format.. I think there is a way round this using IsInt, but I can't work out how or where to use it. Can anybody help please?

Liam315
27 May 2013, 15:50
Your first if... script should be something like
if (IsInt(result)) {
[rest of the script goes here]
}
else {
msg ("That's not a number")
}

sharky3008
27 May 2013, 20:03
Thanks Liam, works great now :-) Sooner or later I will get the hang of this.