Page 1 of 2
Code won't work...
Posted:
Thu Feb 23, 2012 5:14 am
by ResourceDragon
I'm sure it's a relatively simple fix, but for some GE doesn't like my code;
This explains what i'm trying to accomplish, but GE doesn't like it. Variables in blue, actors in red.
game_name = name_input.text;
folder_name = game_folder.text;
extension_name = game_extension.text;
final_name = folder_name + game_name + extension_name;
LoadGame(final_name);
if(strcmp(text,"quit")==0)
{
ExitGame();
}
Please help, lol. XD
Re: Code won't work...
Posted:
Thu Feb 23, 2012 5:42 am
by Nykos
i think the good syntax would be: game_name = name_input.textNumber;
Re: Code won't work...
Posted:
Thu Feb 23, 2012 6:01 am
by ResourceDragon
Oh yea. Duh. //HeadDesk
the .textNumber fixes the syntx problem, but now it doesn't do anything when I press enter. :/
The purpose of the code is to load a game file by what the user inputs into the file name.
User inputted text is in green, fixed text is in red;
game_name = text;
folder_name = game_folder.textNumber;
extension_name = game_extension.textNumber;
final_name = folder_name + game_name + extension_name;
LoadGame(final_name);
if(strcmp(text,"quit")==0)
{
ExitGame();
}
In the end, I want it to load the file "Game Folder/game_name.ged"
Re: Code won't work...
Posted:
Thu Feb 23, 2012 6:50 am
by skydereign
You can't add strings like that (not in C at least). You have to use string functions like strcat, strcpy, or in this case sprintf (this goes for setting strings equal to each other as well).
- Code: Select all
sprintf(temp_string, "%s/%s.%s", folder_name, game_name, extension_name);
LoadGame(temp_string);
Of course you can't specify directories with gE. The file must be in the game's current directory.
Re: Code won't work...
Posted:
Thu Feb 23, 2012 8:13 am
by ResourceDragon
Of course you can't specify directories with gE.
Actually, you can. Doing,
LoadGame(Folder/Game.ged);
Does work, as long as the folder and the file exist.
EDIT: Wait, never mind, I see what you meant.
I'm still not getting it to work. Here's my program;
Re: Code won't work...
Posted:
Thu Feb 23, 2012 10:39 pm
by skydereign
The reason it isn't working is because you can't set strings equal to each other like that. You have to use strcpy or sprintf.
- Code: Select all
// so wherever you have this (or similar)
text_var = text;
// you have to change it to this
strcpy(text_var, text);
Re: Code won't work...
Posted:
Fri Feb 24, 2012 3:37 am
by ResourceDragon
Now it's showing even less of the string... =.=...
I changed it to this, and each corresponding actor has this in their draw actor/script; strcpy(text2, text); or strcpy(text3, text);
strcpy(game_name, text);
strcpy(folder_name, text2);
strcpy(extension_name, text3);
sprintf(temp_string, "%s/%s.%s", folder_name, game_name, extension_name);
LoadGame(temp_string);
All it ends up trying to load is the period. I'm sorry I sound like such an idiot, i'm a ruby coder, but learning the basics of C.
(In ruby you can just set strings like that. XD)
Re: Code won't work...
Posted:
Fri Feb 24, 2012 9:14 am
by skydereign
You are creating a lot of strings that are unnecessary. Because of this I think you're mixing up what needs to be copied. Take a look at this, it removes the need for any global string (it uses the actor's text string).
Re: Code won't work...
Posted:
Fri Feb 24, 2012 5:18 pm
by ResourceDragon
Ah, thank you so much. I see why it wasn't working now. I learn much faster by reading working code, then trying to figure it out myself. It's how I learned Ruby. XD
Just one more question though. This line;
char temp[30];
Does it mean you can input up to 30 characters into the prompt?
EDIT: Well, it works when I playtest it in the editor, but when it's exported and I go to try and load the file, it just closes the game..?
Re: Code won't work...
Posted:
Fri Feb 24, 2012 7:53 pm
by skydereign
I assume it's because you are still trying to load a .ged file. If you are running an executable version of a game it cannot load the ged project.
Re: Code won't work...
Posted:
Sat Feb 25, 2012 12:43 am
by ResourceDragon
Ah, I see. I managed to do what I wanted it to do now.
Thank you very much! I'm sorry i've caused you so much trouble. XD
EDIT: eh... One more eensy-weensy question...
It's no problem if I don't need to know. When I input a file that doesn't exist, the window just flashes and resets the prompt, which is ok with me.
But is there a way to do it so instead of resetting, it just brings up an error message(Saying so and so doesn't exist) I can define?
Re: Code won't work...
Posted:
Sat Feb 25, 2012 5:00 am
by skydereign
You can check if the file exists by opening it with read permission (it's NULL if the file doesn't exist).
- Code: Select all
FILE* file = fopen(game_path, "r");
if(file != NULL) // file exists
{
fclose(file);
LoadGame(game_path);
}
else
{
// display the error
}
Re: Code won't work...
Posted:
Sat Feb 25, 2012 6:19 am
by ResourceDragon
K, I switched it around, so it's like this now;
- Code: Select all
char temp[30];
sprintf(temp, "%s/%s.%s", game_folder.text, text, game_extension.text);
FILE* file = fopen(temp, "r");
if(file != NULL) // file exists
{
fclose(file);
LoadGame(temp);
}
else
{
// display the error
}
And it's giving me this error;
- Code: Select all
Error Line 5; Unexpected declaration sprintf
The code seems fine to me... The file open code is similar to Ruby, but...
Re: Code won't work...
Posted:
Sat Feb 25, 2012 7:05 am
by skydereign
Yup, you definitely came from ruby. You can't declare variables wherever you want in C. You have to declare them at the start of a code block (namely at the top of the script). You can create a code block so you can do that though.
- Code: Select all
char temp[30];
sprintf(temp, "%s/%s.%s", game_folder.text, text, game_extension.text);
{
FILE* file = fopen(temp, "r");
if(file != NULL) // file exists
{
fclose(file);
LoadGame(temp);
}
else
{
// display the error
}
}
The extra set of brackets creates a new code block (so an if statement also has a new block) and the variables declared within exist only inside that block.
Re: Code won't work...
Posted:
Sat Feb 25, 2012 8:29 pm
by ResourceDragon
And you sir, are my hero. +1 a million times over.
although, the equivalent of "puts", "p" or, "print" in Ruby is what in C?
(It's used like this; puts "Hello world." )