Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Olleh19 on Wed 30/10/2019 17:16:27

Title: Does Scanf have an equivalent in AGS
Post by: Olleh19 on Wed 30/10/2019 17:16:27
Is it in AGS but by another name perhaps? I have been following some C tutorials as per recommendation by one of the ags forum members whose exact username i cannot remember right now (laugh)
however the tutorials always comeback to using Scanf and/or printf. As for printf typing display("");, seems to work, but i don't know what to do for scanf.
(roll)

Title: Re: Does Scanf have an equivalent in AGS
Post by: Khris on Wed 30/10/2019 17:29:28
AGS isn't exactly a console application but you can use
String name = Game.InputBox("!What is your name?");
to halt the game and grab text input from the user.

Use String.AsFloat and String.AsInt to get numeric values.
Title: Re: Does Scanf have an equivalent in AGS
Post by: Olleh19 on Wed 30/10/2019 22:44:34
Thanks Khris, however the beginner in me cannot get the "scanf" to function properly. Because i can't get where i should put the %d or &age or whatever "syntax" is needed.

Code (ags) Select
function room_AfterFadeIn()
{



String ageAsString = Game.InputBox("What is your age?"); //i tried adding %d, &age just get's errors.

int age;
ageAsString ="*insert something"; //is this where the scanf d% should go in maybe? I'm confused.
age = ageAsString.AsInt;



if (age > 18)

{
     Display("The age is greater then 18");
}
if (age == 18)

{
     Display("the age is equal to 18");
}
if (age < 18 )

{
     Display("the age is less then 18");

}
}
Title: Re: Does Scanf have an equivalent in AGS
Post by: eri0o on Wed 30/10/2019 23:00:52
You are attributing an empty string to text1. It's better to call text1 something like ageAsString, instead, to give proper semantic meaning to it.

You also have one more opening "{" then needed (and an extra opposing one at the end
Title: Re: Does Scanf have an equivalent in AGS
Post by: Olleh19 on Wed 30/10/2019 23:13:43
Quote from: eri0o on Wed 30/10/2019 23:00:52
You are attributing an empty string to text1. It's better to call text1 something like ageAsString, instead, to give proper semantic meaning to it.

You also have one more opening "{" then needed (and an extra opposing one at the end

True thanks! It was because i copy pasted from a notepad i had saved on my desktop right into the ags function. I saved some of the tutorial things i went over from tutorials in a textfile called "needs to learn for ags later", that later is now!  (laugh)

What i would like to see happen (in the future) is perhaps when using a gui, that if pushing one button on the gui it would use the "scanf" function, to get the result of something else then a "typed in number" with the game.box input, perhaps a combination for lock, or a apartment call. Where if 4 digits are typed one scanf happen, four other leads to another, etc. But these things are still way above my head. I'm just happy i got my head around if/if else (not in this case tho!) but in general. And bools.  :) but for practice it would be cool to see how it scans thru this age example. Edit: I updated with your advice (i think).
Title: Re: Does Scanf have an equivalent in AGS
Post by: Khris on Thu 31/10/2019 11:05:18
To be clear: there is no scanf equivalent where you can put  %d  and the like inside a string.
Looking at the code you posted, just remove line 9 entirely and you should be good to go:

  String ageString = Game.InputBox("What is your age?");
  int age = ageString.AsInt;


For stuff like combination locks you can use the  Chars[] array to look at a string's individual characters.

Best look at the String section of the manual to see what AGS supports; AGScript was built based on C but imo trying to apply C beginner's lessons to it is doomed to fail (as this thread demonstrates ;))
Title: Re: Does Scanf have an equivalent in AGS
Post by: Olleh19 on Thu 31/10/2019 11:18:37
Quote from: Khris on Thu 31/10/2019 11:05:18
To be clear: there is no scanf equivalent where you can put  %d  and the like inside a string.
Looking at the code you posted, just remove line 9 entirely and you should be good to go:

  String ageString = Game.InputBox("What is your age?");
  int age = ageString.AsInt;


For stuff like combination locks you can use the  Chars[] array to look at a string's individual characters.

Best look at the String section of the manual to see what AGS supports; AGScript was built based on C but imo trying to apply C beginner's lessons to it is doomed to fail (as this thread demonstrates ;))

AHA!, interesting, ofc. Makes sense now, how lovely! :grin: Thanks ever so much Khris!  :)