Having a problem getting settings from a cfg file.

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Having a problem getting settings from a cfg file.

Post by Yukiko »

I have a static housing package originally written by Bishop. I recently added a seetings.cfg file to it to allow some flexibility for a server admin. So I took the settings.inc file Austin created and changed it to read my cfg file. By the way, it's a simple little include file that is very handy if I can figure out what I am doing wrong :)

The file reports the following error:

Code: Select all

syslog [pkg/systems/statichousing/textcmd/gm/staticdeed2.ecl]: Error::SH_GetSettingsCfgElem() - Unable to find elem [PriceEditable] ->Element not found
Here is the settings.inc file:

Code: Select all

//$Id: settings.inc 373 2006-06-17 18:27:33Z austinheilman $

/*===============================================================
* Current Version
* SETTINGS.INC - v1.0
* Updated 9/27/2005 2:54PM
*
* -- Revision v1.0 --
* Austin:
*  Created include file
===============================================================*/

use uo;
use os;
use cfgfile;

/*
 * SH_GetSettingsCfgFile(engine_name)
 *
 * Purpose
 * Reads in :staticHousing:config/settings.cfg
 *
 * Parameters
 *
 * Return value
 * A config file reference.
 *
 */
function SH_GetSettingsCfgFile()
	UnloadConfigFile(":staticHousing:settings");
	var cfg := ReadConfigFile(":staticHousing:settings");

	if ( cfg.errortext )
		SysLog("Error::SH_GetSettingsCfgFile() - Unable to open [:staticHousing:settings.cfg] ->"+cfg.errortext);
	endif

	return cfg;
endfunction

/*
 * SH_GetSettingsCfgElem(elem_name, cfg_file)
 *
 * Purpose
 * Retrieves an elem from a config file. 
 *
 * Parameters
 * elem_name:	A string matching the elem name to be retrieved.
 * cfg_file:	Optional parameter - reference to a config already read in by SH_GetSettingsCfgFile()
 *
 * Return value
 * A config file elem reference.
 *
 */
function SH_GetSettingsCfgElem(elem_name, byref cfg_file:=0)
	if ( !cfg_file )
		cfg_file := SH_GetSettingsCfgFile();
	endif
	
	var elem := cfg_file[elem_name];

	if ( elem.errortext )
		SysLog("Error::SH_GetSettingsCfgElem() - Unable to find elem ["+elem_name+"] ->"+elem.errortext);
	endif

	return elem;
endfunction
Here is my test line that calls the uses the GetSettingsCfgElem function call in settings.inc:

Code: Select all

SendSysMessage(who, SH_GetSettingsCfgElem("PriceEditable"));
And finally here is the settings.cfg file:

Code: Select all

# $Id: settings.cfg 620 2006-07-08 17:14:41Z Yukiko $
#
#

Elem	Settings
{
	// The following three (3) settings apply to the staticDeed script
	// PriceEditable
	// 0 - The price per square for a house is not changeable.
	// 1 - The price per square for a house is changeable.
    PriceEditable	1
	
	// SecuresEditable
	// 0 - The number of secures for a house are not changeable.
	// 1 - The number of secures for a house are changeable.
    SecuresEditable	1
	
	// LockDownsEditable
	// 0 - The number of lockdowns for a house are not changeable.
	// 1 - The number of lockdowns for a house are changeable.
    LockDownsEditable	0
}
I have placed the settings.cfg file in \pkg\systems\staticHousing\config. the script doesn't find the setting. I even tried placing the settings.cfg file in the main staticHousing directory and still no go. As an additional note I tested another package that uses the settings.inc file and it cannot read from the config file associated with that package either. Note it apparently finds the file but cannot read the elements because it doesn't report being unable to open the file .

If you can help with this I would appreciate it.
RusseL
Forum Regular
Posts: 375
Joined: Fri Feb 20, 2009 8:30 pm

Re: Having a problem getting settings from a cfg file.

Post by RusseL »

Must be:
ConfigFile -> Elem -> Entry
settings.cfg -> Settings -> PriceEditable

What you trying to read:
settings.cfg -> PriceEditable

Code: Select all

SendSysMessage(who, GetConfigInt(SH_GetSettingsCfgElem("Settings"), "PriceEditable"));
or readable format :) :

Code: Select all

var configuration := SH_GetSettingsCfgFile();
var element_of_config := SH_GetSettingsCfgElem("Settings", configuration);
var entry_inside_of_element := GetConfigInt(element_of_config, "PriceEditable");
https://docs.polserver.com/pol099/fullf ... tConfigInt
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Having a problem getting settings from a cfg file.

Post by Yukiko »

Yes. I thought there was something missing in the config file reading process but that is the way Austin created the include file and anyone who has looked at Austin's code knows he's an expert at writing code.

I'll give your suggestions a try. Thanks.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Having a problem getting settings from a cfg file.

Post by Yukiko »

I got it working.Thanks RusseL for pointing me in the right direction. This is the final settings.cfg file I ended up with. You will note I chose GetConfigString instead of GetConfigInt because I want to include the possibility of mixed data types, integer, string, real etc. in the values a settings config file can hold. Naturally that requires the calling program to do the appropriate conversion on the returned data but if you're fishing for an integer value you already know that so you know you're going to have to perform a CInt() operation on what you get back. Thanks again RusseL. I never thought it was an error in the code of the settings.inc file because it came from Austin. :) He must have been tired when he wrote it or maybe I misunderstood how it is used. It's probably my mistake.

Code: Select all

//$Id: settings.inc 373 2006-06-17 18:27:33Z austinheilman $

/*===============================================================
* Current Version
* SETTINGS.INC - v1.0
* Updated 9/27/2005 2:54PM
*
* -- Revision v1.0 --
* Austin:
*  Created include file
===============================================================*/

use uo;
use os;
use cfgfile;

/*
 * SH_GetSettingsCfgFile(engine_name)
 *
 * Purpose
 * Reads in :staticHousing:configs/settings.cfg
 *
 * Parameters
 *
 * Return value
 * A config file reference.
 *
 */
function SH_GetSettingsCfgFile()
	UnloadConfigFile(":staticHousing:settings");
	var cfg := ReadConfigFile(":staticHousing:settings");

	if ( cfg.errortext )
		SysLog("Error::SH_GetSettingsCfgFile() - Unable to open [:staticHousing:settings.cfg] ->"+cfg.errortext);
	endif

	return cfg;
endfunction

/*
 * SH_GetSettingsCfgElem(elem_name, cfg_file)
 *
 * Purpose
 * Retrieves an elem from a config file. 
 *
 * Parameters
 * elem_name:	A string matching the elem name to be retrieved.
 * cfg_file:	Optional parameter - reference to a config already read in by SH_GetSettingsCfgFile()
 *
 * Return value
 * A config file elem reference.
 *
 */
function SH_GetSettingsCfgElem(elem_name, byref cfg_file:=0)

  if ( !cfg_file )
		cfg_file := SH_GetSettingsCfgFile();
	endif
	
  var elem := FindConfigElem( cfg_file, "Settings" );
  var data := GetConfigString( elem, elem_name );

//	var data := cfg_file[elem_name];
	if ( data.errortext )
		SysLog("Error::SH_GetSettingsCfgElem() - Unable to find elem ["+elem_name+"] ->"+elem.errortext);
	endif

	return data;
endfunction
RusseL
Forum Regular
Posts: 375
Joined: Fri Feb 20, 2009 8:30 pm

Re: Having a problem getting settings from a cfg file.

Post by RusseL »

Yukiko wrote: Wed May 31, 2017 11:47 pm I never thought it was an error in the code of the settings.inc file because it came from Austin. :) He must have been tired when he wrote it or maybe I misunderstood how it is used. It's probably my mistake.
It is not an error in Austins Code. Your usage is wrong.
Function name is SH_GetSettingsCfgElem, so it does what is supposed to do - returns config elem.


After your change function getElem returns config elem entry/param, and that is wrong.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Having a problem getting settings from a cfg file.

Post by Yukiko »

Yes. You are correct. As I was getting up this morning it occurred to me exactly what I was doing wrong. Once again I should have looked at Austin's use of the function rather than assuming how it was used.

My sincere apologies to Austin.
Post Reply