raw(in/set/get)

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
User avatar
Sir Bob
Traffic Manager
Traffic Manager
Posts: 214
Joined: 07 Oct 2006 09:08
Location: Melbourne, Australia

raw(in/set/get)

Post by Sir Bob »

I've noticed that a number of people are using rawin, rawset and rawget in their AI's, and as far as I know, not using them hasn't restricted me in any way. I was just wondering what they did and if using them would help my AI in some way?

Sir Bob
Witty Signature!
Image
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: raw(in/set/get)

Post by Zutty »

The "raw" accessors ignore delegation; what you might think of as inheritance in general OOP. I think most people use tables as simple data structures though, so I doubt most people are actually making use of this functionality.

Its probably a combination of personal preference or perhaps the lack of documentation for squirrel combined with a bit of cargo-cult programming (i.e. where a programmer copies an entire snippet of another's code verbatim, without fully understanding it!).

TBH I did it myself... at first I thought that rawin etc... was the only way to work with tables, and now I just can't be bothered to change it!! ;)
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: raw(in/set/get)

Post by Yexo »

I think you are right there Zutty.
Zutty wrote:TBH I did it myself... at first I thought that rawin etc... was the only way to work with tables, and now I just can't be bothered to change it!! ;)
Have you found an alternative to rawin? Rawget isn't really needed (table.rawget("foobar") is equal to table["foobar"] as long as you don't use inheritance). For rawset I'm not really sure. I think table["foobar"] = 3; doesn't work if table doesn't have an index foobar yet. Rawset creates a new index if it doesn't exist yet.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: raw(in/set/get)

Post by Zutty »

Yexo wrote:I think you are right there Zutty.
Zutty wrote:TBH I did it myself... at first I thought that rawin etc... was the only way to work with tables, and now I just can't be bothered to change it!! ;)
Have you found an alternative to rawin? Rawget isn't really needed (table.rawget("foobar") is equal to table["foobar"] as long as you don't use inheritance). For rawset I'm not really sure. I think table["foobar"] = 3; doesn't work if table doesn't have an index foobar yet. Rawset creates a new index if it doesn't exist yet.
To create a slot you use the <- operator. E.g. table["foobar"] <- 3;

I am fairly sure that rawin has an equivalent "in" operator, which is used like so: if ("foobar" in table) {}. Dont quote me on this though!
PathZilla - A networking AI - Now with tram support.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests