Posted: 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:
Posted: 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
Posted: 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
Posted: 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?)
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.)
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 endrecord 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 .
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!(:
Posted: 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 endrecord
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
Posted: 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).
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.