Author Topic: Passing variables / returning string value  (Read 1339 times)

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Passing variables / returning string value
« on: April 03, 2013, 10:52:31 PM »
What am I doing wrong here?

Code: [Select]
const string@ strOre = "Ore", strDamage = "Damage", strMass = "mass";
const string@ strMtl = "Metals", strMine = "MineM", strMtlGen = "MtlG";
const string@ strElc = "Electronics", strElcGen = "ElcG";
const string@ strAdv = "AdvParts", strAdvGen = "AdvG", strFoodGen = "FudGe";
const string@ strFood = "Food", strGoods = "Guds", strLuxuries = "Luxs";
const string@ strMtlBuffer = "Metal_Buffer", strElecBuffer = "Electronics_Buffer", strAdvBuffer = "AdvancedParts_Buffer", strFoodBuffer = "Food_Buffer";
const string@ strMtlEmpDemand = "Metal_Demand", strElecEmpDemand = "Electronics_Demand", strAdvEmpDemand = "AdvancedParts_Demand", strFoodEmpDemand = "Food_Demand";

...

float tradeResource(Empire@ emp, Object@ obj, State@ state, const string@ statName, float amount, float tradeEff, TradeMode mode) {

...
      string@ bufferName = statBufferName(statName);

...

}

// Take statName, return buffer string name
string@ statBufferName(const string@ statName) {

if (statName == strMtl) { return strMtlBuffer; }
if (statName == strElc) { return strElecBuffer; }
if (statName == strAdv) { return strAdvBuffer; }
if (statName == strFood) { return strFoodBuffer; }

}


The above won't excute (and the log file won't show me what the error is :/ ).
Any ideas?




Thy Reaper

  • BMS Lead Developer
  • BMS Administrator
  • Hopeless
  • *
  • Posts: 3237
  • Karma: +397/-8
    • View Profile
    • Blind Mind Studios
Re: Passing variables / returning string value
« Reply #1 on: April 03, 2013, 10:56:41 PM »
What do you mean it won't execute? The specific lines you show, or the entire function of tradeResource?

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #2 on: April 03, 2013, 11:19:12 PM »
Well if it wasn't obvious, this is inside planet.as - which I'm trying to modify - and with

Code: [Select]
string@ statBufferName(const string@ statName) { ... }

Nothing happens - I can tell because I get no output in the console, and I have many other debug statements which will output when this sucessfully executes.

If I use

Code: [Select]
string statBufferName(const string@ statName) { ... }

string bufferName = statBufferName(statName);

It works, but I have later issues trying to use emp.getStat(bufferName).

So:

1)Am I wrong in using an object reference in return for my helper function?

2) Is there some way for me to get useful error messages when working inside planet.as, particularly the tick() functions? If the code is wrong, it just fails to work as expected, I don't get any useful errors in console output.

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #3 on: April 04, 2013, 07:41:36 AM »
The first attempt didn't work because you were attempting to return a const string@ as a non const string@ and so that will have run into an error. That error is displayed in log.txt if you run in verbose mode so check there and you can find better error messages.

You could also use:

Code: [Select]
string statBufferName(const string@ statName) { ... }

string@ bufferName = statBufferName(statName);

Which will solve the const issue but might or might not resolve the second issue you are having, but then again that might be an error in your code, you'd have to post relevant snippets of where its failing when you try and use it again.
« Last Edit: April 04, 2013, 08:04:51 AM by Azalrion »
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #4 on: April 04, 2013, 12:38:16 PM »
I've got verbose mode on, I just don't get any output after the usual setup and init functions  :-\

I'm attaching my entire planet.as file for context. My issues are around line 635.

My goal here is to replace the galactic bank with a on-demand system; when a planet needs a resource it updates the relevant "Demand" variable, and if a planet with extra resources to trade notices a positive value in the demand variable, it adds to the "buffer"; when a planet with demand sees free resources in the buffer, it takes from the buffer.




Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #5 on: April 04, 2013, 01:46:46 PM »
Errors with scripts are in the setup and init functions. Just search for error because I assure you, you would have got one with the first attempt.
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #6 on: April 04, 2013, 01:51:14 PM »
Well, I did find this:


AS Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet(0).as: Compiling string statDemandName(const string@)
 line: 868, col: 1
AS Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet(0).as: Not all paths return a value
 line: 868, col: 47
AS Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet(0).as: Compiling string statBufferName(const string@)
 line: 878, col: 1
AS Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet(0).as: Not all paths return a value
 line: 878, col: 47

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #7 on: April 04, 2013, 01:57:05 PM »
Oh, heh, I turned on versioning in my editor and it isn't suffixing the files with .bak

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #8 on: April 04, 2013, 01:59:37 PM »
Yes that error will be because you don't return anything if the passed in string doesn't match one of the options.  You need to return a value if there is no match like so:

Code: [Select]
// Take statName, return demand string name
string statDemandName(const string@ statName) {

if (statName == strMtl) {
return strMtlEmpDemand;
} else if (statName == strElc) {
return strElecEmpDemand;
} else if (statName == strAdv) {
return strAdvEmpDemand;
} else if (statName == strFood) {
return strFoodEmpDemand;
}

return null;
}

// Take statName, return buffer string name
string statBufferName(const string@ statName) {

if (statName == strMtl) {
return strMtlBuffer;
} else if (statName == strElc) {
return strElecBuffer;
} else if (statName == strAdv) {
return strAdvBuffer;
}

return null;
}

« Last Edit: April 04, 2013, 02:02:18 PM by Azalrion »
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #9 on: April 04, 2013, 02:02:30 PM »
Actually return null gave me an error about conversion from null to a string. I used return "no_value" and it went away.

Interestingly, now I get this:

Note: Diplomacy strings will still have gaps - fillDiplomacyStringGaps() not implemented
Loading effects: Game Data/Effects/effects.txt
Loading effects: Game Data/Effects/remnant_effects.txt
Loading effects: Game Data/Effects/struct_effects.txt
Game Data/Effects/struct_effects.txt(122) error E007: No function with matching parameter list (Event@)
 Function: "planet::popEcoInit"
Game Data/Effects/struct_effects.txt(125) error E007: No function with matching parameter list (Event@)
 Function: "planet::empEcoInit"
Loading effects: Game Data/Effects/system_effects.txt

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #10 on: April 04, 2013, 02:03:26 PM »
I have to go to work, but I should note I have entries in both structures.txt and structure_effects.txt for those - and the first one is a built-in from SR.

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #11 on: April 04, 2013, 02:03:57 PM »
There will be other errors in the log file, that will appear when planet.as cannot be compiled because there is a code or syntax error.
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #12 on: April 04, 2013, 10:47:32 PM »
Turns out it didn't like

Code: [Select]
if (foodBuffer is null) { print ("foodBuffer is null"); }

Because of:

Code: [Select]
S Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet.as: No conversion from '<null handle>' to 'float' available.
 line: 639, col: 21

Tho I'm not sure now how you'd check that a float has any value...

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #13 on: April 05, 2013, 06:25:40 AM »
You initialize it before using it. Set it to say 0.0f, so you can check it that way. A float can never be null and I wouldn't be surprised if that function was returning 0.0f if the stat hasn't been used before.
« Last Edit: April 05, 2013, 08:36:48 AM by Azalrion »
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #14 on: April 05, 2013, 08:56:31 PM »
Well, if you declare a float without setting an initial value, and then try to use it (for example, f_to_s() ), this happens:

Code: [Select]
S Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet.as: Compiling float tradeResource(Empire@, Object@, State@, const string@, float, float, TradeMode)
 line: 600, col: 1
AS Server Error in C:\Program Files (x86)\Star Ruler\Game Data\scripts\server\planet.as: 'foobar' is not initialized.
 line: 603, col: 25

I didn't really need to use it other than debugging, so I've moved on, but it still would be nice to know. In most other languages I work with, checking for null value is easy enough.

Thanks for all your help Azalrion! I may have more questions in the future =)

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #15 on: April 06, 2013, 04:42:44 AM »
Most c-style languages don't allow you to evaluate primitives to being null. Not being initialized is not the same as being null as null itself is roughly a value.
« Last Edit: April 06, 2013, 04:51:04 AM by Azalrion »
GA - Mod Team
GA Forums

ibanix

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: Passing variables / returning string value
« Reply #16 on: April 06, 2013, 02:42:06 PM »
Right, but I don't seem to have an ifdefined() option in angelscript. Or do I?

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: Passing variables / returning string value
« Reply #17 on: April 06, 2013, 02:50:27 PM »
Nope you don't, you have to initialize them. Its no different to say float f = 0.0f as it is to use isdefined(). If you don't initialize something you are just leaving a chance of errors cropping up later which can be avoided.
GA - Mod Team
GA Forums