Newbie question: data into and out of scenes

Forums for educators and researchers to discuss educational aspects of Algodoo.

Newbie question: data into and out of scenes

Postby stephane.savanah » Tue Feb 11, 2014 8:42 am

How does one get data into and out of an Algodoo scene?
e.g. 1: suppose I have created a scene which is a game and I want to to automatically extract the score for use in an external application.
e.g. 2: suppose I have created a scene that requires some automated set up - I want to send parameters into the scene.
Thanks!
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby electronicboy » Tue Feb 11, 2014 12:38 pm

I know they there is a function called "writetofile", I think it's system.writetofile(string, filename), however you would need to check the syntax using the console.

Sending data into Algodoo is a little more complex. There is a file inside the Algodoo document folder, I've forgotten the exact name of the file, however I can find out later on tonight, but any commands wrote to the file will be executed in Algodoo's thyme engine.
If you open Algodoo and open a scene, the file name should appear in the log file. (the file will be deleted after it's executed.)
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Wed Feb 12, 2014 7:15 am

Thanks electronicboy. I'll have a play around. Very grateful if you do discover and send me more details. Cheers...
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby Kilinich » Wed Feb 12, 2014 11:26 am

Here some examples of scoreboard stored in file:
Rating: rated 6.1
Filesize: 388.59 kB
Comments: 9
Ratings: 4
download

Rating: rated 6.1
Filesize: 159.5 kB
Comments: 11
Ratings: 3
download
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Fri Feb 14, 2014 5:51 am

Hi Kilinich,
Your stuff is totally awesome. I feel more like playing your games than building my own!

Anyway, thanks for the examples. I was able to learn some stuff but I'm more of a newbie than you think! So I have some basic questions that I hope won't take long to answer...

Where exactly do you write the code for allocating a file and storing a file? is it in the console? (I did not see any such code in your examples). How does one control where the file is stored?

I guess the bottom line is I need to see example code, but I could not locate it in your scenes. I looked inside some objects (e.g. the score box) so I can see some code to execute a function - but I can't see the function code anywhere.

Hope this all makes sense - again very grateful for your help...
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby Kilinich » Fri Feb 14, 2014 10:00 am

I prefer to write code outside algodoo. I use FAR manager for umpack .PHZ and edit .PHN or write some function in text files and copy-paste it in console.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Mon Feb 17, 2014 3:56 am

Thanks again Kilinich - but again, this is a little beyond my understanding. Any chance you can send me some examples of text-file code you pasted into the console? Thanks in advance...
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby Kilinich » Mon Feb 17, 2014 11:19 am

Copy that and paste in console (~ key).
Code: Select all
scene.my.xFor = (n1, n2, code) => {
    n2 > n1 ? {
        m := (n1 + n2) / 2;
        scene.my.xFor(n1, m, code);
        scene.my.xFor(m + 1, n2, code)
    } : {code(n1)}
};

Now you can use this function in your scripts:
For exmple scene.my.xfor(0,5,(i)=>{print (i)}) will print 0..5
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Tue Feb 18, 2014 6:03 am

Thanks Kilinich! I'm getting there.
Hopefully last few questions: how to use WriteToFile? When you use this function where exactly does it store the file (doesn't seem to be the logfile)? Do you need to define a filename first? or is there a default filename?

Here is my console code:

scene.my.WRITESCORE = (SCORE) => {System.WriteToFile(SCORE);}

Here is my code in the object:
onCollide = (e)=>{
SCORE = SCORE + 1
}
onDie = (e)=>{
WRITESCORE(SCORE)
}

I want to export the score to an external application...
Thanks again...
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby Kilinich » Tue Feb 18, 2014 9:15 am

> System.WriteToFile
Append some text to a file - usage: WriteToFile("foo.txt", "some text"). Returns 'true' on success.

I use it like this (just an exalmple):

Global:
Code: Select all
Scene.my.readBest := {
    s := System.ReadWholeFile("segwayx2.sav");
    geval(s)
};
Scene.my.writeBest := (j)=>{
    System.WriteToFile("segwayx2.sav", j + ";\n")
};


in events:
Code: Select all
onSpawn := (e)=>{
        best := Scene.my.readBest;
        best > e.this._best ? {e.this._best = best} : {}};


Code: Select all
onCollide := (e)=>{
        e.this._jump = e.this.pos(0) - e.pos(0);
        e.this._jump > e.this._best ? {
            e.this._best = e.this._jump;
            Scene.my.writeBest(e.this._best)
        } : {}};
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Wed Feb 19, 2014 8:02 am

Fantastic - I've made a lot of progress. Can now write to a text file!
Still much to learn though - can you recommend a good Thyme manual or Tutorial? I need answers to (probably basic questions) like:
> How do I edit previously entered global code? I press "~" to bring down the console but I cannot seem to access previously entered code.
> Why do local variables not get saved with scenes? E.g. I define an allocate a variable in a geometry and save the scene, but when I later open this scene the variable definition code has disappeared.
Thanks...Stef
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby electronicboy » Wed Feb 19, 2014 3:51 pm

All code should be saved in the scene.my. variable namespace, and any variables on a geometry should start with a underscore _.

And, the most up-to date tutorials we have are in the thyme section (see the one created by GradyFitz), although they haven't
exactly been maintained well, algodoo hasn't really changed much on the thyme side since these other than a few new functions which a quick search may help you with.

Also, if you type in the function variable where you saved the code, it should list the code. (however, it is best practice to keep such code inside a document inside a text editor where it is easier to copy from).


Code: Select all
   25380 ms: Inter-process communication file detected...
   25381 ms: Permanently erasing file "Algodoo/communication.cfg"...
   25381 ms: Running code: ""


The above file is relative from the users home folder, and will evaluate any thyme code in there. I'd suggest using a queue, and pulling infomation either one by one, or a few at a time (one by one is easier, however chucking a few at a time will help a lot for performance if you have a lot of data to pass). You will have to check that the file is deleted before writing to it, otherwise the code will probably not be evaluated properly.
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Thu Feb 20, 2014 5:41 am

Thanks e-boy. I'm sure it is something obvious but can you please provide a bit more info on scene.my.variable namespace? I do not know how to 'save code to this area'. I know that I can enter code by bringing down the console with "~" but I do not know where this code gets saved and I do not know how to retrieve it for editing. Thanks...Stef
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am

Re: Newbie question: data into and out of scenes

Postby electronicboy » Thu Feb 20, 2014 9:27 am

As you may have noticed so far, any "global code", which I will now refer to as functions are stored into variables. years ago when thyme was just a little child, a way to transport variables in saves was needed, and this was the birth of the scene.my namespace.

Anything saved into this space, is saved into file save files which you can create, which allows you to save functions and data variables between sessions and to other people in the community. If your variable name starts with scene.my. then it will be saved into this area and will be saved into the scene.

Slightly more advanced, however not very, is the entity space, which is found on every object. Now, any variable on a geometry starting with a _, will be exported along with the save. for example '_data', would be saved, while 'data' would not. This is why when you're looking at scenes on algobox, a lot of code running on geometries will have extra variables with an '_' at the beginning of them.

I believe the best way to look at the use of these two different spaces of storing infomation as a variant of PHP or similar languages, if the variable needs to be accessed by several parts of the scene, stick it into scene.my. While, if you only need it for running code from one object, then use a entity variable.

> Scene.my.writeBest := (j)=>{
> System.WriteToFile("segwayx2.sav", j + ";\n")
> };
(j)=>{
System.WriteToFile("segwayx2.sav", j + ";\n")
}
> Scene.my.writeBest
(j)=>{
System.WriteToFile("segwayx2.sav", j + ";\n")
}


as for your last question, see the above box, if you type in the name of a variable into the console, it will output the contents of the data that is in the variable, whether it be a string, a float or a function, algodoo doesn't care and will print it for you. Now, the only way to edit a variable is to overwrite it, as I think I stated in one of my posts above, install a good text editor, (Notepad++ is usually a good choice for a windows user) and store snippets of code in there. as your code get's more complex, having it inside a text file which you can keep properly formatted is always nice!
When asking for help, READ THE STICKIES!
electronicboy
 
Posts: 1694
Joined: Mon Aug 31, 2009 6:18 pm

Re: Newbie question: data into and out of scenes

Postby stephane.savanah » Fri Feb 21, 2014 1:35 am

Thanks e-boy and Kilinich. I finally get it. Probably I was thinking too much in a traditional mode. I now know how to do what I want to do. Cheers...
stephane.savanah
 
Posts: 11
Joined: Tue Feb 11, 2014 8:20 am


Return to Algodoo for educators

Who is online

Users browsing this forum: No registered users and 1 guest

cron