Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Importing Modules
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aange10




PostPosted: Sun Sep 18, 2011 11:58 pm   Post subject: Importing Modules

What is it you are trying to achieve?
I've made a module with a procedure in it to make a main menu. I want to import that module into my game, and run the procedure.


What is the problem you are having?
I'm getting a syntax error at Import.

Describe what you have tried to solve this problem
I've read http://compsci.ca/v3/viewtopic.php?t=4303 fully, and followed everything there and there is still a syntax error.
I've searched the forums for others with the same problems (am I alone?? hah)
I've look at the turing help reference (F10), but their explaination of:

Quote:

import [ howImport ] importItem
{, [howImport] importItem }

is not useful at all. Their description is also totally confusing.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Here is the folder I'm working with (Note I have the module saved two different ways, it's just my attempts at getting it right.)


Please specify what version of Turing you are using
4.0 (I think, it's the latest version with the generate standalone working)

ALL MY TESTING IS IN GAME-TESTING.t, you will NOT find anything under game.t (unless you want to play it.. heh)



Game.rar
 Description:
My game...

Download
 Filename:  Game.rar
 Filesize:  5.82 KB
 Downloaded:  62 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Mon Sep 19, 2011 12:03 am   Post subject: RE:Importing Modules

you simply type import module'sFileNameWithoutExtension as the first line of your code
Aange10




PostPosted: Mon Sep 19, 2011 12:05 am   Post subject: RE:Importing Modules

Ahh so the problem was it HAS to be the FIRST line of code... I didn't know that D:. Thankyou very much!
Aange10




PostPosted: Mon Sep 19, 2011 12:11 am   Post subject: Re: Importing Modules

Though, is there a way I could make the variables that change in the procedure change for the main program too? (Instead of JUST locally within the proc?)



NOTE: Updated file:



Game.rar
 Description:
Updated file

Download
 Filename:  Game.rar
 Filesize:  5.2 KB
 Downloaded:  64 Time(s)

Raknarg




PostPosted: Mon Sep 19, 2011 4:35 pm   Post subject: RE:Importing Modules

I believed thats achieved with either exporting or pointers or something
Aange10




PostPosted: Mon Sep 19, 2011 4:39 pm   Post subject: RE:Importing Modules

Hmm, could make a function that does the procedure, and use the result (i think is the name) command to get the varriables?
Aange10




PostPosted: Mon Sep 19, 2011 6:01 pm   Post subject: Re: Importing Modules

I read Pointers and I've read Functions & Procedures to try and solve this problem, and so I think I've found something close to my answer (Yay!).

The pointers thread didn't help me too much. Really I think it's just because it didn't really give a good example of its use.

However, the Functions and Procedures thread did help. So, I'm posting a reply here to ask a few questions.

1.) Is it possible to make a function that will run the procedure, and give me all my variables as real numbers (and then i can convert my needed variable to int)?

2.) Cervantes said

Quote:

Nice functions are ones that compute a value based soley on the parameters they take.


is there a way that my procedure will compute the values (If you run through the main menu, or look at the coding, you see how depending on which character you pick the variables change.) and my function will give me the result??


Thanks to anybody who takes the time to read this! (I've spent about two hours toying with these arcane methods, and I appreciate all the help I can get.)
Insectoid




PostPosted: Mon Sep 19, 2011 7:36 pm   Post subject: RE:Importing Modules

You could use a function, and abandon the procedure altogether. A function is just a procedure that returns a value.
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Mon Sep 19, 2011 8:37 pm   Post subject: RE:Importing Modules

Sounds good to me! Only one problem with that. How do i make a function give more than one result?
Tony




PostPosted: Mon Sep 19, 2011 9:30 pm   Post subject: RE:Importing Modules

return a multi-value type, such as a record
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Aange10




PostPosted: Mon Sep 19, 2011 10:59 pm   Post subject: Re: Importing Modules

Alright, so I've spent the last 2 hours trying to make my functions output a record.
Here are some of the things I've tried doing to make it work (in probably every combination...)
Firstly I made something that looked like this:
Turing:

var xpos,ypos,xpos_width,xpos_height,color1 : int := 1
var jump_speed,run_speed : real := 10
type PlayerData:
    record
        xpos: int
        ypos: int
        xpos_width: int
        xpos_height: int
        color1: int
        jump_speed: real
        run_speed: real
    end record
var a,b : PlayerData
function MainMenuF
    ypos += 1
    xpos_width += 1
    xpos_height += 1
    color1 += 1
    jump_speed += 2.5
    run_speed += 2.5
result PlayerData
end MainMenuF

put "PlayerData:",PlayerData


From there I started refining the coding. And these are the things I've spent two hours changing:

1. I've made the record exclusively local to the function. I've also tried making the record both global and local.

2. I've tried different parameters for function MainMenuF: .. I've tried type,record, and PlayerData

3. I've tried different parameters for result PlayerData: .. I've tried type, var, record, PlayerData

4. I've ALSO tried to use the "var a : PlayerData" technique, and have substitute it for PlayerData in (i think) every combination i've tried.

5. I've fully read the Pointer, Functions and Procedures, Records (quite pointlessly...) The Dir Module, and functional programming in Turing [skimmed], tutorials.

6. I've searched the forums for the key words 'function record' and browsed the posts.


-- So, is there a way I could maybe see a function that does what I'm trying to achieve? I want my module (posted a few up) to give me the variables it changes in the procedure. If I need to change the procedure to a function and it be that simple, then awesome. But I'd absolutely love whomever could show me a function that takes in global variables and outputs a record (or however to make it work)


... I just want to make it clear my intentions are not to be bothersome or a waste you guys' time; I'm wasting more of my time than I'd like as is. My goal is to learn this and complete the task I'm trying to do, not decimate whatever free time I have Crying or Very sad .

EDIT: Haha see what I get for trying to clean up my coding? 2 days of researching and fiddling! But at least once I learn it, I know it!(:
Tony




PostPosted: Mon Sep 19, 2011 11:37 pm   Post subject: RE:Importing Modules

code:

var new_data, old_data : PlayerData

% init all values
old_data.xpos = 0
old_data.ypos = 0
% ... etc.

function do_stuff_to(local_data : PlayerData)
    local_data.ypos += 1
    local_data.xpos_width += 1
    % etc...
    result local_data
end do_stuff_to

new_data = do_stuff_to(old_data)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Zren




PostPosted: Tue Sep 20, 2011 1:53 am   Post subject: RE:Importing Modules

You can't modify variables passed as parameters in Turing Tony. Not too big a deal. Rather inefficient (but alas, it is Turing). You just have to copy it into a new variable first. It's still rather annoying.
Turing:

type Player :
    record
        x, y : int
    end record

fcn newPlayer (x, y : int) : Player
    var p : Player
    p.x := x
    p.y := y
    result p
end newPlayer

fcn blarg (_p : Player) : Player
    var p := _p
    p.x += 1
    result p
end blarg

var p := newPlayer (0, 0)
p := blarg (p)
DemonWasp




PostPosted: Tue Sep 20, 2011 7:32 am   Post subject: Re: RE:Importing Modules

Zren @ Tue Sep 20, 2011 1:53 am wrote:
You can't modify variables passed as parameters in Turing


Lies. The following:

Turing:

var a : int := 3

proc foo ( var i : int )  % note the 'var' here...
  i += 2
end foo

foo ( a )
put a


will output 5.

Also, Tony's code doesn't modify local_data itself, it modifies the contents of local_data (different, and most languages allow the latter).
Tony




PostPosted: Tue Sep 20, 2011 12:38 pm   Post subject: Re: RE:Importing Modules

Ops. Hard to keep up with all the subtle differences between languages. Zren has the right idea (if there are multiple functions that perform operations, the copy of a record can be factored out into its own function).

DemonWasp's pass-by-reference also works, but I find such code to be less intuitive. You'd still need to do the whole copy-record code if you want the original to not be modified (e.g. calculate new position to check if it's a valid move). I suppose that depends on the use cases.

DemonWasp @ Tue Sep 20, 2011 7:32 am wrote:
Tony's code doesn't modify local_data itself, it modifies the contents of local_data (different, and most languages allow the latter).

That would have been the case for objects, but I think records are flat data-structures.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 27 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: