Jump to content


Photo

Loading Objects from a txt file


  • Please log in to reply
10 replies to this topic

#1 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 02 April 2012 - 03:53 PM

Dear GMC,

I am trying to make a level editor. After a good bit of trouble with the saving I attempted the code for the loading. Needless to say it didn't work. I can't figure out why. I tried putting it inside execute_string (I thought that might work because I made a game a little while ago that used execute_string to change sprites).
Here is my code, if you need to see my saving code just ask.
///Press L event
file = get_open_filename('text files|*.txt','data');
if (file != '')
    {
    with (obj_player) {instance_destroy();}
    with (obj_block) {instance_destroy();}
    with (obj_button) {instance_destroy();}
    fname = file_text_open_read(file);
    if (!file_text_eof(fname))
        {
        xx = file_text_read_real(fname);
        file_text_readln(fname);
        yy = file_text_read_real(fname);
        file_text_readln(fname);
        obj = file_text_read_string(fname);
        file_text_readln(fname);
        instance_create(xx,yy,obj);
        }
    if (file_text_eof(fname))
        {
        file_text_close(fname);
        }
    }
Any assistance would be appreciated! Thank you.

M

Edited by MetroidMan347, 02 April 2012 - 03:54 PM.

  • 0

#2 RocketLauncher3G

RocketLauncher3G

    Int. Game Programmer

  • GMC Member
  • 627 posts

Posted 02 April 2012 - 03:57 PM

Could you let us see the file your program created?

That way it's easier to where the problem lies ;)
  • 0

#3 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 02 April 2012 - 03:59 PM

I suppose that would be helpful wouldn't it. :)

96
160
obj_player
256
32
obj_block
384
64
obj_block
416
128
obj_block
384
256
obj_block
352
288
obj_block
128
320
obj_block
128
224
obj_block
160
64
obj_block
256
192
obj_button


  • 0

#4 RocketLauncher3G

RocketLauncher3G

    Int. Game Programmer

  • GMC Member
  • 627 posts

Posted 02 April 2012 - 04:24 PM

Okay,

What could solve this thing is that instead of the obj_name you get the id (the_instance_you_want_to_add.object_index) of the object and save that one, as object names are variables which hold a certain value.

what definitely is a problem is the if...

file = get_open_filename('text files|*.txt','data');
if (file != '')
    {
    with (obj_player) {instance_destroy();}
    with (obj_block) {instance_destroy();}
    with (obj_button) {instance_destroy();}
    fname = file_text_open_read(file);
    if (!file_text_eof(fname))
        {
        xx = file_text_read_real(fname);
        file_text_readln(fname);
        yy = file_text_read_real(fname);
        file_text_readln(fname);
        obj = file_text_read_string(fname);
        file_text_readln(fname);
        instance_create(xx,yy,obj);
        }
    if (file_text_eof(fname))
        {
        file_text_close(fname);
        }
    }

where you say if(!file_text_eof(fname) it should say while (!file_text_eof(fname)), so it becomes:
file = get_open_filename('text files|*.txt','data');
if (file != '')
    {
    with (obj_player) {instance_destroy();}
    with (obj_block) {instance_destroy();}
    with (obj_button) {instance_destroy();}
    fname = file_text_open_read(file);
    while (!file_text_eof(fname))
        {
        xx = file_text_read_real(fname);
        file_text_readln(fname);
        yy = file_text_read_real(fname);
        file_text_readln(fname);
        obj = file_text_read_real(fname); // if you decide to store the object_index into the file, and you should definitely do this, because execute_string is terribly slow... 
        file_text_readln(fname);
        instance_create(xx,yy,obj);
        }
    file_text_close(fname); // as you've already reached the end of the file, otherwise the while loop is still running you can get rid of this if-statement
    }

Edited by RocketLauncher3G, 02 April 2012 - 04:25 PM.

  • 0

#5 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 02 April 2012 - 04:39 PM

<snip>

It didn't work, all it did was make the alpha go away on my cursor. Can't other things go on happening while a loop is going? That is what this would indicate to me.
  • 0

#6 RocketLauncher3G

RocketLauncher3G

    Int. Game Programmer

  • GMC Member
  • 627 posts

Posted 02 April 2012 - 04:48 PM


<snip>

It didn't work, all it did was make the alpha go away on my cursor. Can't other things go on happening while a loop is going? That is what this would indicate to me.


When a loop is going it won't do anything else, as it is busy with that loop.

Have you considered saving the object_indexes instead of the object_names, as it's much faster to load them that way... Either way, I think that
execute_string("instance_create(" + string(xx) + ", " + string(yy) + ", " + string(obj) + ");")
will do the trick...

However this can be slow, so therefore I'd consider saving the object indexes instead of the object names...
  • 0

#7 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 02 April 2012 - 04:57 PM

Ok how can I change my saving code to do that?

Here it is:
var file,i;

/* Define a list of objects to save */
object[0] = obj_player;
object[1] = obj_block;
object[2] = obj_button;
object[3] = obj_key;
objects = 4;

file = file_text_open_write("level.txt");
for(i = 0;i < objects;i += 1) {
    with(object[i]) {
        file_text_write_string(file,string(x));
        file_text_writeln(file);
        file_text_write_string(file,string(y));
        file_text_writeln(file);
        file_text_write_string(file,object_get_name(object[i].object_index));
        file_text_writeln(file);
    }
}
file_text_close(file);

Thanks for help.

M

EDIT: It's wrapping down but the lines are preserved in my actual code.
EDIT2: Oh wait derp all I have to do is get rid of object_get_name right? Derpity-Derp.
EDIT3: IT WORKS!!! AFTER ALL THIS TIME IT FINALLY WORKS!!! THANKS SO MUCH EVERYBODY!!!

Edited by MetroidMan347, 02 April 2012 - 05:09 PM.

  • 0

#8 Desert Dog

Desert Dog

    GMC Member

  • GMC Member
  • 6409 posts
  • Version:Unknown

Posted 02 April 2012 - 10:12 PM

Oh, lol, you again. :tongue:

Yeah, you typically save the object_index in save files. You don't really want to be using execute_string if you can help it.

I thought you wanted to save the string, because you'd want to save your levels as an actual script, like this:
instance_create(x,y,obj_name)

which you could then copy&paste in-game.. no external levels. :tongue:
  • 0

#9 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 04 April 2012 - 05:14 AM

I was under the impression that it was typically a good idea to stay away from execute_file as the end user can just put whatever code they want into the file and the game will execute it. I also believe that, like execute_string, it's painfully slow. Am I wrong in thinking this because if I am that would be a pretty good thing to know.

Thank you all for your help!

M
  • 0

#10 Desert Dog

Desert Dog

    GMC Member

  • GMC Member
  • 6409 posts
  • Version:Unknown

Posted 04 April 2012 - 05:52 AM

I was under the impression that it was typically a good idea to stay away from execute_file as the end user can just put whatever code they want into the file and the game will execute it. I also believe that, like execute_string, it's painfully slow. Am I wrong in thinking this because if I am that would be a pretty good thing to know.

Thank you all for your help!

M


You wouldn't use execute_file on it, you'd just copy&paste in-game as a script. Then create your level with that script.

so you get the benefit of using your own custom level editor to build your game, while keeping the levels internal.
  • 0

#11 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 836 posts
  • Version:GM:Studio

Posted 04 April 2012 - 05:57 AM


<snip>


You wouldn't use execute_file on it, you'd just copy&paste in-game as a script. Then create your level with that script.

so you get the benefit of using your own custom level editor to build your game, while keeping the levels internal.


Oh, I see! No, I really like GM's editor, especially now that I've upgraded to 8.1 from 8.0. I want to be able to release DLC and let users make their own levels. That's always appealed to me it really makes content practically infinite. As long as there are people that play the game there will be levels available. I really can't thank all of you who replied to this topic and my other one enough!!!

M
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users