static/instance class visibility

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

static/instance class visibility

Post by krinn »

I could find a better title for my problem, but it's related to that : http://wiki.openttd.org/AI:SQ_pitfalls

I get it that calling statically or calling the instance change the context of your code visiblity, but it's getting me really mad there !

So i'm asking for a clearer answer here's what i'm seeing on my AI (i took great care of call them static with var.classfunc instead of classfunc but it doesn't work when neast call are done)

let's say i init the var ATEST in main.nut so i could get/change ATEST value from another class by init the class with a "main" variable, this way i will always grab my previous instance to keep its context and so, keep seeing my ATEST var from the class.

Code: Select all

in dummy.nut
class cDummy { main = null; constructor(main) { this.main=main; }}
function cDummy::print
{ AILog(ATEST); }
in main.nut (i don't show the full code, just for you to get the picture)
ATEST = null; dummy = null;
ATEST = "value"; dummy=cDummy(this);
Ok, so because of context, ATEST doens't exist in cDummy so when doing
dummy.print();
openttd will trigger the "ATEST doesn't exist"

So to solve that, i set the

Code: Select all

function cDummy::print
{ AILog(main.ATEST); }
great, now dummy can access and see ATEST within my class context. Pfffff


But here's what happen, in neastest context, i can't use that trick anymore and i'm stuck, because context keep changing and i can't fix my code to keep seeing ATEST
again, i create another class, cDummy2, exactly a dup of cDummy with the same function (but cDummy2::print of course)
add dummy2 var in main.nut...

So:
dummy2.print and dummy.print work because they use main.ATEST to access the var

Now what drives me mad is that if i call dummy2 from dummy, context is again change and this time ATEST is not visible: here's come again the "ATEST doesn't exist" error...
I know how to see ATEST again (in that context ATEST is in main.main.ATEST)

But if i change let's say: cDummy2 code to use main.main.ATEST instead of main.ATEST i will not be able to use dummy2.print from main.nut (or other instance) as from that context ATEST is main.ATEST...

It's a pain because i use a class that handle my banking routines, and that class could be use from any other class and from main, so context is nearly unpredictable (it's not, but i have better things to do than looking who call who).

I suppose there's a solution or something i didn't catch there, else all AI would use main.nut for everything to avoid the problem and make one big main.nut file to not take care of that problem.

Anyone can me give hint or directions ?
Last edited by krinn on 29 Dec 2010 22:11, edited 1 time in total.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: static/instance class visibility

Post by Zuu »

Can you make a minimal example AI? This is not an issue I've experienced, though I haven't used static member variables that much. In SuperLib I had to make some workarounds due to the fact that libraries does not have a scope of their own and have no idea what name the main class will be given by the library user. There I have some __private_varName variables for caching some cargo related results.


Also next time you post code, please indent the code and use the [ code ] tags to so the indenting gets rendered correctly by the forum + browsers:

Code: Select all

function MyFunc() 
{
    AILog.Info("Hello World");
}
If you have problems figuring out how to use the code-tags, quote my post and you'll get the source BB-code of this post.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: static/instance class visibility

Post by krinn »

hmmmm, my bad it works :/
seems i register just to get a last shame before the year end.

it was that i drop a comment at a start line and gedit always color code TODO, so i thinking the line was comment out and just miss out that a class call was on that line. (and i have seek so many times for that error and i was always ignoring that line.
/* TODO*/ cBank.update();
cBank.update refer to the class cBank instead of the instance bank=cBank();

and of course the error was trigger far after in another class that doesn't really love the mixing calls.

this confirm : http://wiki.openttd.org/AI:SQ_pitfalls was a good addition to the wiki :/

that's not a good start for a 1st post :(
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: static/instance class visibility

Post by Yexo »

It's not clear to me whether ATEST is a global variable or not. If it's not: squirrel doesn't really support static class variables, it only supports them with a constant value. If it's a global variable you can access it from every scope by putting two colons in front of the variable name.

Edit: ok, so it was something else. Good you solved it :). And hey, we all make mistakes, just be glad you were able to solve it :)
krinn
Transport Coordinator
Transport Coordinator
Posts: 342
Joined: 29 Dec 2010 19:36

Re: static/instance class visibility

Post by krinn »

i have edit first my first post with code tag, but really green on black give an old touch (this remember me my old screen) but i don't find it really nice to read :)

Thank you guys for your answers
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: static/instance class visibility

Post by Yexo »

You'll have to indent your code yourself, the forum software doesn't do that for you.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 9 guests