Code won't work...

Non-platform specific questions.

Code won't work...

Postby ResourceDragon » Thu Feb 23, 2012 5:14 am

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
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby Nykos » Thu Feb 23, 2012 5:42 am

i think the good syntax would be: game_name = name_input.textNumber;
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Thu Feb 23, 2012 6:01 am

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"
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Thu Feb 23, 2012 6:50 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Thu Feb 23, 2012 8:13 am

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;
Attachments
File Loader.zip
(464.75 KiB) Downloaded 121 times
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Thu Feb 23, 2012 10:39 pm

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);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Fri Feb 24, 2012 3:37 am

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)
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Fri Feb 24, 2012 9:14 am

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).
Attachments
file_loader.ged
(1.76 KiB) Downloaded 113 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Fri Feb 24, 2012 5:18 pm

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..?
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Fri Feb 24, 2012 7:53 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Sat Feb 25, 2012 12:43 am

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?
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Sat Feb 25, 2012 5:00 am

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
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Sat Feb 25, 2012 6:19 am

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...
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Re: Code won't work...

Postby skydereign » Sat Feb 25, 2012 7:05 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Code won't work...

Postby ResourceDragon » Sat Feb 25, 2012 8:29 pm

And you sir, are my hero. +1 a million times over. :D

although, the equivalent of "puts", "p" or, "print" in Ruby is what in C?

(It's used like this; puts "Hello world." )
ResourceDragon
 
Posts: 39
Joined: Thu Feb 16, 2012 2:17 am
Score: 0 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest