2 registered members (TipmyPip, 1 invisible),
18,758
guests, and 8
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Having problem with STRINGs being a parameter of function
#394699
02/15/12 20:02
02/15/12 20:02
|
Joined: Feb 2012
Posts: 2
darthsmurf
OP
Guest
|
OP
Guest
Joined: Feb 2012
Posts: 2
|
I created a function that takes three strings and puts them on screen using TEXT. Everything else in my project works except the function that has the strings as parameters. I'm not sure what I'm doing wrong. Help would be appreciated.
I'm doing a choose your own adventure type popup screen and I need three different strings to be used in the function every time its called because it is better to create the panel once and put different messages in it each time it is called then to do a hundred bazillion panels that look the same with different messages in them.
example:
function win_choice(STRING* s1, STRING* s2, STRING* s3) { .... TEXT* Text1 = { ... string (s1); ... } TEXT* Text2 = { ... string (s2); ... } //and another text is used for the third string }
I keep getting errors related to this I do not understand.
"< string ^ s1);> TEST.C 5:2 (): String -s1"
TEST.C is the name of my lite-c file. I am not being shown the line number the error is on. What's in the quotes is all the error info I'm getting. I'm not sure what the engine is trying to tell me with that little bit of info except that something is wrong with the way the strings are being used.
I've used other variable types in functions as parameters before and I figure either I'm using the STRING incorrectly as a parameter or something else in the function is messed up.
I have tried doing the function header differently after trying to find tutorials or examples of strings being used as parameters in a function and couldn't find anything.
function win_choice(STRING s1, STRING s2, STRING s3)
function win_choice(STRING *s1, STRING *s2, STRING *s3)
But shouldn't it be the way I have it: function win_choice(STRING* s1, STRING* s2, STRING* s3)
Thanks for any help.
edit: When I have called the function it has been like this,
win_choice(a1,a2,a3);
the strings called a1,a2,a3 have already been declared like so STRING* a1 = "blah blah blah";
all my data types have been declared at the top of the script before doing any of the functions and panels and everything else.
Last edited by darthsmurf; 02/15/12 20:12.
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: darthsmurf]
#394714
02/16/12 00:38
02/16/12 00:38
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
You're not allowed to define text objects inside a function. You should instead:
1. Define a text object outside with three strings. 2. Write your function win_choice and in this function copy the strings you got as an argument to the text. You can use the str_cpy command for that.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: Uhrwerk]
#394722
02/16/12 05:43
02/16/12 05:43
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Yes, defining TEXTs (or any game-object0 inside a function is bad, and the compiler STILL hasnt been modded to generate errors on this... So, if you need three 'separate' TEXTs to display your data, you just create all three TEXTs as global text's (outside the function), and copy the strings into them from inside the function. eg:
TEXT* Text1 = { strings=1; }
TEXT* Text2 = { strings=1; }
TEXT* Text3 = { strings=1; }
function win_choice(STRING* s1, STRING* s2, STRING* s3)
{
if(s1!=0)
{
str_cpy((Text1.pstring)[0], s1);
set(Text1, SHOW);
}
else
{
reset(Text1, SHOW);
}
//
if(s2!=0)
{
str_cpy((Text2.pstring)[0], s2);
set(Text2, SHOW);
}
else
{
reset(Text2, SHOW);
}
//
if(s3!=0)
{
str_cpy((Text3.pstring)[0], s1);
set(Text3, SHOW);
}
else
{
reset(Text3, SHOW);
}
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: EvilSOB]
#394723
02/16/12 05:52
02/16/12 05:52
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
OR, if you are desperate to keep the TEXTs as local objects...
function win_choice(STRING* s1, STRING* s2, STRING* s3)
{
TEXT* Text1=NULL:
TEXT* Text2=NULL:
TEXT* Text3=NULL:
//
if(s1!=0)
{
Text1 = txt_create(1, 100);
str_cpy((Text1.pstring)[0], s1);
set(Text1, SHOW);
}
//
if(s2!=0)
{
Text2 = txt_create(1, 100);
str_cpy((Text2.pstring)[0], s2);
set(Text2, SHOW);
}
//
if(s3!=0)
{
Text3 = txt_create(1, 100);
str_cpy((Text3.pstring)[0], s3);
set(Text3, SHOW);
}
//
...
other stuff
...
//////////////////////////////////////////////
wait(1);
//
if(Text1)
{
if((Text1.pstring)[0]!=0)
{
str_remove((Text1.pstring)[0]);
}
ptr_remove(Text1);
}
\\
if(Text2)
{
if((Text2.pstring)[0]!=0)
{
str_remove((Text2.pstring)[0]);
}
ptr_remove(Text2);
}
\\
if(Text3)
{
if((Text3.pstring)[0]!=0)
{
str_remove((Text3.pstring)[0]);
}
ptr_remove(Text3);
}
\\
}
But as you can see, things are starting to get ugly pretty quickly. This is NOT a good way to go...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: EvilSOB]
#394755
02/16/12 11:35
02/16/12 11:35
|
Joined: Mar 2011
Posts: 3,150 Budapest
sivan
Expert
|
Expert
Joined: Mar 2011
Posts: 3,150
Budapest
|
I've never put strings=1 after global text definitions in case of created texts, like : TEXT* Text1 = { strings=1; } , maybe it causes my freeing error of strings on shut downs... but str_remove is still valid? I think ptr_remove should be used.
Last edited by sivan; 02/16/12 11:44.
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: sivan]
#394768
02/16/12 12:34
02/16/12 12:34
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Thats what I always use when working with TEXTs. Normally I will set strings to be more than one, but thats just due to my needs. It has never (to my knowledge) cause me crashes on exit... (NEVER EVER set strings to be ZERO, it WILL cause a bug with TEXTs to appear)
And telling the definition "strings = xxx;" is not really appropriate IF you use the "string = ( "xxx", "yyy", ...);" directive in that same TEXT...
But I cant say I use it "a lot", cause I just dont like to use TEXTs much. I tend to create my own STRING arrays.
And both str_remove AND ptr_remove are both valid for pstring elements of a TEXT. I just prefer to use type-specific xxx_remove functions when I can...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: sivan]
#394779
02/16/12 14:31
02/16/12 14:31
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Thats pretty much the only time I use them any more... If you feel like melting your brain, this is a function I created at a 'replacement' for txt_for_dir.
//=========================================================================================
//txt_for_dir_plus( TEXT** targ, STRING* pathname, STRING* wildcards): var
//Reads filenames that match the given wildcard/s into separate strings of a text object.
//This instruction can be used to scan a folder for files matching MULTIPLE wildcards.
//-----------------------------------------------------------------------------------------
//Parameters:
//targ Pointer to the target TEXT* pointer for the TEXT object to be filled.
// If the target TEXT* pointer is NULL, a new TEXT object is created there.
// If the target TEXT* pointer contains a TEXT object, then the filenames
// found by this function are ADDED to the END of this existing text object.
//
//pathname Absolute or Relative Pathname of folder to be searched
//
//wildcards File names (C-style wildcards) to be matched, '|' separates wildcards.
// eg: "*.MDL|*.HMP|-*_open.*" This will include all MDL and HMP files,
// and from that list it will then exclude all "*_open.*" files.
//-----------------------------------------------------------------------------------------
//Returns:
//Number of strings ADDED to 'targ' TEXT object.
//=========================================================================================
var txt_for_dir_plus( TEXT** targ, STRING* pathname, STRING* wildcards)
{ if((!targ)||(!pathname)||(!wildcards)) return(0); //un-resolvable parameter set
//--------------------------------------------------------------------------------------
TEXT *targ_txt, *tmp_txt=NULL,*sub_txt=NULL; STRING* tmp_str = str_create("");
var f_count,t_cnt, wilds, i,ii,sub;
//--------------------------------------------------------------------------------------
//Extract / separate wildcards
str_cpy(tmp_str, wildcards); wilds = 0;
while(str_stri(tmp_str,"|")) { str_clip(tmp_str,str_stri(tmp_str,"|")); wilds++; }
STRING* *all_wilds = (STRING**)sys_malloc(sizeof(STRING*) * (wilds++));
memset(all_wilds, 0, sizeof(STRING*) * wilds); str_cpy(tmp_str, wildcards);
for(i=0; i<wilds; i++)
{ all_wilds[i] = str_create(tmp_str);
if(str_stri(tmp_str,"|")) { str_clip(tmp_str,str_stri(tmp_str,"|"));
str_trunc(all_wilds[i],str_len(tmp_str)+1); } }
//--------------------------------------------------------------------------------------
//ADD files to temporary TEXT for each wildcard that DOESNT start with a '-' sign
tmp_txt=txt_create(0,0); tmp_txt.strings=0; sub_txt=txt_create(16,0);
for(t_cnt=0; t_cnt<wilds; t_cnt++)
{ if(str_to_asc(all_wilds[t_cnt])!='-')
{ str_cpy(tmp_str, pathname);
if(((tmp_str.chars)[tmp_str.length-2]!='\\')&&(str_len(tmp_str)))
{ str_cat(tmp_str, "\\"); } str_cat(tmp_str, all_wilds[t_cnt]);
while((sub=txt_for_dir(sub_txt,tmp_str))>=sub_txt.strings)
{ for(i=0; i<sub; i++) { txt_addstring(sub_txt, ""); } }
for(i=0; i<sub_txt.strings; i++)
{ if(str_len((sub_txt.pstring)[i])) txt_addstring(tmp_txt,(sub_txt.pstring)[i]);
str_cpy((sub_txt.pstring)[i], ""); } } }
//--------------------------------------------------------------------------------------
//REMOVE files from temporary TEXT for each wildcard that DOES start with a '-' sign
for(t_cnt=0; t_cnt<wilds; t_cnt++)
{ if(str_to_asc(all_wilds[t_cnt])=='-')
{ str_cpy(tmp_str, pathname);
if(((tmp_str.chars)[tmp_str.length-2]!='\\')&&(str_len(tmp_str)))
{ str_cat(tmp_str, "\\"); } str_cat(tmp_str, str_clip(all_wilds[t_cnt],1));
while((sub=txt_for_dir(sub_txt,tmp_str))>=sub_txt.strings)
{ for(i=0; i<sub; i++) { txt_addstring(sub_txt, ""); } }
for(i=0; i<sub_txt.strings; i++)
{ if(str_len((sub_txt.pstring)[i]))
for(ii=0; ii<tmp_txt.strings; ii++)
if(str_cmp((tmp_txt.pstring)[ii],(sub_txt.pstring)[i])==1)
{ str_cpy((tmp_txt.pstring)[ii], ""); }
str_cpy((sub_txt.pstring)[i], ""); } } }
//--------------------------------------------------------------------------------------
//TRANSFER from temporary TEXT to target TEXT, after creating target if it needed to be
targ_txt=*targ; if(!targ_txt)
{ *targ = targ_txt = txt_create(0,0); reset(targ_txt, SHOW); targ_txt.strings=0; }
f_count=0; for(i=0; i<tmp_txt.strings; i++)
{ if(str_len((tmp_txt.pstring)[i]))
{ txt_addstring(targ_txt, (tmp_txt.pstring)[i]); f_count++; } }
//--------------------------------------------------------------------------------------
//Cleanup Workspace
for(t_cnt=0; t_cnt<wilds; t_cnt++) { str_remove(all_wilds[t_cnt]); }
sys_free(all_wilds); str_remove(tmp_str);
if(tmp_txt) { for(i=0; i<tmp_txt.strings; i++)
if((tmp_txt.pstring)[i]) str_remove((tmp_txt.pstring)[i]); txt_remove(tmp_txt); }
if(sub_txt) { for(i=0; i<sub_txt.strings; i++)
if((sub_txt.pstring)[i]) str_remove((sub_txt.pstring)[i]); txt_remove(sub_txt); }
//--------------------------------------------------------------------------------------
return(f_count); }
//=========================================================================================
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Having problem with STRINGs being a parameter of function
[Re: sivan]
#394791
02/16/12 16:24
02/16/12 16:24
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Another useful thing about that function is that if you do supply it with a TEXT, and it doesnt contain enough strings to contain all the filenames, then it automatically extends the size of the TEXT so there is enough space to fit all the filenames into it...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|