Message Board
Message Board > Fenix / Bennu / Gemix / DIV > A little help needed...passing a structure as a parameter? |
June 21, 2008, 02:13 | |
Geuben
Whiskered 11 posts |
Hey all, i'm after a bit of help. I've written a pathfinding function that uses a stucture to hold the relevant info it needs. I've declared this stucture Globally as the pathfinding function calls a few functions which need access to this structure. This all works fine, but I want to be able to call multiple instances of the pathfinding function (so as too speed up the AI in my game). Obviously currently this wont work as each instance would try and use the Global structure. I thought about declaring the structure Privately inside the pathfinding function, but need to give access to the other functions that are called by pathfinding. So how do i pass a structure as a parameter? Thanks in advance for any help. Geuben ____________ |
# |
June 21, 2008, 02:43 | |
Rincewind
programmer 1548 posts |
Hi, It's possible to define your own data type, for example: Fenix code: Type _pathfind Struct Openlist[300] Parent; x; y; F; G; H; End Struct Closedlist[300] Parent; x; y; F; G; H; End Struct Target x; y; End End Inside the main pathfinding function you can declare all your pathfinding variables privately by declaring one variable of the data type _pathfind: Fenix code: Private _pathfind pathfinding_hooray; And if we have a sub process that will be called by the pathfinding function, like this: Fenix code: Process pathfinding_sub_process(_pathfind pathfinding) ... End ...then we can pass this process a reference to our pathfinding data like this: Fenix code: pathfinding_sub_process(pathfinding_hooray); And access it in pathfinding_sub_process(): Fenix code: pathfinding.Target.x=1; ____________ Personal website: http://www.loijson.com |
# |
June 21, 2008, 14:20 | |
Geuben
Whiskered 11 posts |
Cheers, i'll try that. I saw that page in the Fenixdocs wiki, but it was a little confusing.
____________ |
# |
June 21, 2008, 14:34 | |
Htbaa
Perl 368 posts |
Hi Geuben, welcome at Booleansoup! Rincewind, can these types contain methods as well? That would make Fenix OOP :-) ____________ blog.htbaa.com |
# |
June 21, 2008, 14:37 | |
Rincewind
programmer 1548 posts |
Hopefully my reply wasn't equally confusing then - if you have any more questions, don't hesitate to ask. Edit: Nope Htbaa. [Edited on June 21, 2008 by Rincewind] ____________ Personal website: http://www.loijson.com |
# |
June 21, 2008, 15:20 | |
Geuben
Whiskered 11 posts |
I've tried to declare the following Code: TYPE _pathfind STRUCT path_grid[max_c][max_r]; square; parent = 0; g = 0; h = 0; f = 0; END open_list[max_sqr] = 0; closed_list[max_sqr] = 0; check_square; END But get the "too many ends" error...any ideas? ____________ |
# |
June 21, 2008, 15:34 | |
Rincewind
programmer 1548 posts |
You can't use variables in the declaration of other variables (as you're doing with those max_* variables), so you could either chose values there or allocate memory later on manually. And just a side note, but you realize that with doing open_list[max_sqr] = 0; you are assigning only the first element of the array the value 0, and not the other elements? All ints are iniated with value zero though so no need for it. ____________ Personal website: http://www.loijson.com |
# |
June 21, 2008, 15:56 | |
Geuben
Whiskered 11 posts |
the max_*s are consts. I wasnt aware that all ints were declared as 0, but i actually have a loop that sets them all to 0 anyway [Edited on June 21, 2008 by Geuben] ____________ |
# |
June 21, 2008, 16:06 | |
Rincewind
programmer 1548 posts |
In that case it's probably due to the positioning of your type block, or to something else unrelated to this part of your code at all. Do you have the type block within a process or function? Because it should be on the top level, not inside of anything.
____________ Personal website: http://www.loijson.com |
# |
June 21, 2008, 16:29 | |
Geuben
Whiskered 11 posts |
This is the start of the program Code: PROGRAM ad_wars;
//INCLUDE "menu's.prg"; CONST max_units = 10; max_buildings = 6; max_c = 33; max_r = 25; max_sqr = 1000; GLOBAL TYPE _pathfind STRUCT path_grid[max_c][max_r]; square; parent = 0; g = 0; h = 0; f = 0; END open_list[max_sqr] = 0; closed_list[max_sqr] = 0; check_square; END ____________ |
# |
June 21, 2008, 16:46 | |
Rincewind
programmer 1548 posts |
That type block shouldn't really be inside there. I would place it on top of everything straight after the PROGRAM line, but since you need the constant there that won't be possible, so what you could do that is also possible is place the type block all down in the Global block. Fenix code: PROGRAM ad_wars; //INCLUDE "menu's.prg"; CONST max_units = 10; max_buildings = 6; max_c = 33; max_r = 25; max_sqr = 1000; GLOBAL <other globals> TYPE _pathfind STRUCT path_grid[max_c][max_r]; square; parent = 0; g = 0; h = 0; f = 0; END open_list[max_sqr] = 0; closed_list[max_sqr] = 0; check_square; END ____________ Personal website: http://www.loijson.com |
# |
June 21, 2008, 16:51 | |
Geuben
Whiskered 11 posts |
ah, well that seems to have worked. I didn't realise the order of declaration would have any effect.
____________ |
# |
June 21, 2008, 19:24 | |
Sandman
F3n!x0r 1194 posts |
It does if you use the statement PROGRAM: it's a worthless piece of shit statement and should be avoided because of this reason alone. For clarity you should do this: Fenix code: //INCLUDE "menu's.prg"; CONST max_units = 10; max_buildings = 6; max_c = 33; max_r = 25; max_sqr = 1000; END TYPE _pathfind STRUCT path_grid[max_c][max_r]; square; parent = 0; g = 0; h = 0; f = 0; END open_list[max_sqr] = 0; closed_list[max_sqr] = 0; check_square; END GLOBAL _pathfind myPathFind; //<other globals> END Process Main() Begin //<this process gets started first, like other cool languages: main!> End Keep in mind that you should not assume identifiers (like variables, constants, function, process, etc) are known before they are declared. This is only half implemented in Fenix (functions and processes only) and is therefore disabled by default in Bennu (for backwards compatibility it is an option). C, C++ and the like also do not support it, they use forward declarations for things like this, which Fenix and Bennu have as well for functions and processes (Declare). Java fully implements this feature, though. ____________ BennuWiki Yes, my avatar has grey borders in IE (so get a decent browser) ROOFLEZ ROOFLEZ |
# |
June 22, 2008, 12:09 | |
yonni
None 420 posts |
so what is the point of the PROGRAM statement?
____________ |
# |
June 22, 2008, 13:55 | |
Htbaa
Perl 368 posts |
Your were required to use it in DIV(2). I think Fenix took it over for the sake of compatibility?
____________ blog.htbaa.com |
# |
Message Board > Fenix / Bennu / Gemix / DIV > A little help needed...passing a structure as a parameter?