AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Text & Files (https://forums.alliedmods.net/showthread.php?t=22166)

Cheap_Suit 12-20-2005 11:10

Text & Files
 
I want to check a line from a file to see if the map name matches it and return the string from it. This current code has been crashing me. Can you spot the problem?

Code:
check(Map[], xFile[], xlen) {         new BaseDir[64], File[64]     get_basedir(BaseDir, 63)     format(File, 63, "%s/configs/check.cfg", BaseDir)         if(file_exists(File))     {         new Text[64], Line, len = 0         while(read_file(File, Line, Text, 63, len))         {             if((Text[0] == ';') || !len)                 continue                 parse(Text, 63, xFile, xlen)             if(equali(Text, Map, 63))             {                   break             } else {                 format(xFile, len, "")             }         }     }     return xFile }

Brad 12-20-2005 11:18

I don't think you can return a string. Actually, you don't need to return it anyway since that particular variable was passed by reference. Whatever changes to it you make in your function will be available to the calling function.

Cheap_Suit 12-20-2005 11:20

You can return a bool, you can return a float.. Why not a string?

Code:
public test(id) {     new Map[64]     get_mapname(Map, 63)         new File[64]     check(Map, File, 63)         client_print(id, print_chat, "%s", File) }


Edit: Hmmm, a global variable. Ahh a workaround! Thanks.

Brad 12-20-2005 12:27

You know how you grab the map name by doing something like this:

Code:
new mapName[32]; get_mapname(mapName, 31);
You're passing the local variable "mapName" by reference. The same thing applies with your own functions. It's not a global variable at all.

Oh, you can't return a string because it's composed of multiple cells whereas everything else you mentioned is only a single cell.

Xanimos 12-20-2005 12:38

Quote:

Originally Posted by Cheap_Suit
You can return a bool, you can return a float.. Why not a string?


Edit: Hmmm, a global variable. Ahh a workaround! Thanks.

Cheap_Suit I'm dissappointed in you. you dont need a global variable.

when you make a function with a string it will change it.

Example:
Code:
//In main function     new szString[46] , len     len = TestStringFunc( szString , 45 ) //Your Func public TestStringFunc( p_szStr , p_iLen ) {     new iLen = format(p_szStr , p_iLen , "Testing String")     return iLen }

And then in the main function szString will be "Testing String" and len will be the string length of it.

Twilight Suzuka 12-20-2005 12:42

In PAWN, you can return arrays (and thus strings, as strings in PAWN are ^0 delimited arrays) through some process, but I forget it.

Cheap_Suit 12-20-2005 14:13

Heres what I have right now but still it crashes my cs.
Code:
check_cfgfile(Map[], rFile[], rLen) {       new BaseDir[64]     get_basedir(BaseDir, 63)         new File[64]     format(File, 63, "%s/configs/xxxx.cfg", BaseDir)         new ReturnS     if(file_exists(File))     {         new Text[64], TgaFile[64], Line, len = 0         while(read_file(File, Line, Text, 63, len))         {             if((Text[0] == ';') || !len)                 continue                             parse(Text, 63, TgaFile, 63)             if(equali(Text, Map, 63))             {                 ReturnS = format(rFile , rLen , TgaFile)                   break             }             else             {                 ReturnS = format(rFile , rLen , "")             }         }     }     return ReturnS }

The function that calls it
Code:
public test(id) {     new Map[64]     get_mapname(Map, 63)         new TgaFile[64], File     File = check_precache(Map, TgaFile, 63)     client_print(id, print_chat, "%s", File) }

What I have in the cfg file
Code:

de_dust test_
de_dust2 test2_


Cheap_Suit 12-21-2005 03:04

Okay. I just need info on this:

Crashes my listen server
Code:
while(read_file(File, Line, Text, 63, len))

This doesn't
Code:
for(Line = 0; read_file(File, Line, Text, 32, len); Line++)

XxAvalanchexX 12-21-2005 19:52

The first one will continually read the same line over and over again, because the read_file function returns the next line (or 0 if end of file), but you never fetch its return value.

Xanimos 12-21-2005 22:29

Code:
while(read_file( File , Line++ , Text, 63, len))

Also
With your code
Code:
 public test(id) {     new Map[64]     get_mapname(Map, 63)           new TgaFile[64], File     File = check_precache(Map, TgaFile, 63)     client_print(id, print_chat, "%s", File) }

it should be
Code:
 public test(id) {     new Map[64]     get_mapname(Map, 63)           new TgaFile[64], File     File = check_precache(Map, TgaFile, 63)     client_print(id, print_chat, "%s", TgaFile) }


All times are GMT -4. The time now is 16:13.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.