Struct declarations.

Sometimes you need to share your knowledge. This is where you can do it. A place for POL and Script guides.
Post Reply
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Struct declarations.

Post by Yukiko »

eScript/eCompile is very loose when it comes to variable types. What I mean is you can do the following and it doesn't usually care:

Code: Select all


// Variable declared implicitly as an integer because we assigned 0 to it
var something := 0;

// Placed a string in a variable originally assigned an integer value
something := "a string of characters";
However, I discovered that when you are using a variable type struct it is important to be sure to declare that variable as a struct prior to adding members to it. I think members is the right word.
So the following produced a "Property not found" in the CProp preceeding the added members:

Code: Select all

program setdest(object)

// Variable declared but no type or value assigned 
var dest;
dest.+x := 1000;
dest.+y := 1000;
dest.+z := 10;
dest.+realm := "britannia";
SetObjProperty(object, Destination", dest);


The proper way is:

Code: Select all

program setdest(object)

// Variable expressly declared as a struct
var dest := struct;
dest.+x := 1000;
dest.+y := 1000;
dest.+z := 10;
dest.+realm := "britannia";
SetObjProperty(object, Destination", dest);
I would assume variable types of dictionary would require the same procedure.

I don't know if this is a bug. I come from a pretty strongly "typed" programming language, Pascal, so I approve of this requirement but the compiler is the place to trap this normally and I only wish it was required on all variables that their type was to be declared and rigidly enforced. I should note that eCompile didn't complain about this. It was at runtime that the behaviour surfaced. So just be aware of this when writing your programs (scripts).
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Struct declarations.

Post by Turley »

That's the big difference between assigning a value and thus also defining the type and using typespecific methods.
Since escript is not typesafe it cannot check such things. It only translates the method call into the binary format and the rest is up to the implementation.
In you case you created a variable of type uninitialized and try to access the .+ Methods. Totally fine for the ecompiler since it has no information about the methods such a type has or what at all the current type of the variable is, it only checks the syntax.

Or in short:
var a;
Is never a good idea. It first of all creates a variable of a senseless type which you later will overwrite. And if you miss the assignment of the real type you get such runtime errors.
Duttones
Apprentice Poster
Posts: 54
Joined: Tue Mar 27, 2012 8:56 pm

Re: Struct declarations.

Post by Duttones »

I use POL exactly because it is not a strongly typed language. For that, we have ServUO in C#, that is strongly typed.

My dream is to have an UO emulator in javascript, maybe using node.js, but I don't know how high level languages will handle game servers, haha.
boberski
Grandmaster Poster
Posts: 275
Joined: Tue Oct 15, 2013 12:00 pm

Re: Struct declarations.

Post by boberski »

Duttones
Apprentice Poster
Posts: 54
Joined: Tue Mar 27, 2012 8:56 pm

Re: Struct declarations.

Post by Duttones »

boberski wrote: Wed Aug 02, 2017 12:46 pm There is in PHP...
https://github.com/joaoescribano/UltimaPHP
eeeewww
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Struct declarations.

Post by Yukiko »

Turley wrote: Wed Aug 02, 2017 8:12 am That's the big difference between assigning a value and thus also defining the type and using typespecific methods.
Since escript is not typesafe it cannot check such things. It only translates the method call into the binary format and the rest is up to the implementation.
In you case you created a variable of type uninitialized and try to access the .+ Methods. Totally fine for the ecompiler since it has no information about the methods such a type has or what at all the current type of the variable is, it only checks the syntax.

Or in short:
var a;
Is never a good idea. It first of all creates a variable of a senseless type which you later will overwrite. And if you miss the assignment of the real type you get such runtime errors.
Oh I understand Turley and this wasn't a complaint or criticism. It was intended to inform new people, or old ones like me, that assume, since eScript isn't a strong typed language, that simply declaring a variable that is going to be used as a struct without defining it as a struct in the declaration will cause problems. That's why I posted in the "Guides" forum. I try to define my variables when I declare them but for some reason this time I just did a var dest without defining it.
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Struct declarations.

Post by Turley »

I know ;)
I just wanted to clarify that this is not a specific problem of a struct.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: Struct declarations.

Post by andenixa »

eScript is dynamic (with struct members;) weak typed DSL when it comes to variables. It would be less stressful to newcomers if compiler wouldn't allow blank variable declaration i.e.var smthing not followed by an immediate assignment operator.

var something := struct{};

is important because it creates Struct object and assigns it to something variable;

When you declare a variable without its assigned to value its being assigned to an uninit object.
I don't always think its useless because you can compare it value to another uninits ;)

The fastest way is like Yukiko mentioned:

Code: Select all

program setdest(object)

    var dest := struct{x:=1000, y:=1000, z:=10, realm:="Britannia"};
    object.SetProp("Destination", dest);
    
    ..

endprogram

UO server in Java script might actually work in terms of perfomance. I think JS is compiled or has JIT.
In terms of scripting it would be a hell of nightmare as JS is the worst possible choice for scripting language. If you don't believe it ask people who use Unity regarding their experience with JS :p Well they don't script in JS because its horrible (though Unity allows it).

PS: Sorry for necro threading. Won't happen again.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Struct declarations.

Post by Yukiko »

Thanks Andenixa. I have to agree with you about eCompile forcing variables to be assigned a value at least. If you need an unassigned variable perhaps something like:

Code: Select all

var a := nil;
or
var a := uninit;
could be used.
Never apologise for posting. I always find your comments informative.
Post Reply