Page 1 of 26

Dynamic Script Usage Questions

PostPosted: 10 Apr 2013, 13:46
by Ben
By request of Krom, I am starting this topic where a discussion left off in a different topic.

(continued from here)
So, just to make sure I am understanding the syntax of the code correctly, to unlock the sawmill, I'd use this code:
  Code:
OnHouseBuilt(9 //woodcutter's) Begin HouseUnlock(0 //Sawmill) End;

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 13:49
by Krom
Not exactly right. The actual code will be more like this:
  Code:
OnMissionStart; begin for I := 0 to 28 do HouseBlock(I); HouseAllow(_store); HouseAllow(_school); end; OnHouseBuilt(aHouse: Integer); begin if aHouse = _quarry then HouseAllow(9); if aHouse = 9 then HouseAllow(0); end;
where _something are IDs that I dont remember :D

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 13:51
by Ben
With the exceptions of the undercores, is this the exact code? I.e., I'd actually write "for I :=0 to 28 do"? I don't understand this line.

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 13:59
by Krom
I didn't checked that code in-game, just wrote from the top of my head.

"for I :=0 to 28 do" means that we do something (HouseBlock(I);) for every I in range from 0 to 28 - these are all houses in KaM.

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 14:25
by Lewin
Please refer to our Wiki when writing dynamic scripts:
http://code.google.com/p/castlesand/wik ... ptsDynamic

Krom's examples are not using exact syntax and he forgot about multiple players. The script should be more like this:
  Code:
procedure OnMissionStart; var I, P: Integer; begin for P := 0 to <number of locations on your map minus 1> do if States.PlayerEnabled(P) then begin for I := 0 to 28 do States.HouseAllow(P, I, False); States.HouseAllow(P, _store, True); States.HouseAllow(P, _school, True); end; end; procedure OnHouseBuilt(aHouse: Integer); begin if States.HouseType(aHouse) = _school then HouseAllow(States.HouseOwner(aHouse), _inn, True); if States.HouseType(aHouse) = _quarry then HouseAllow(States.HouseOwner(aHouse), _woodcutter, True); end;
Replace _something with the house number (same as you use in .dat scripts).

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 16:06
by Ben
Please refer to our Wiki when writing dynamic scripts:
http://code.google.com/p/castlesand/wik ... ptsDynamic
The Wiki's tutorials are not very helpful (for a novice programmer such as myself); which is why I wanted to ask here on the forum.
Krom's examples are not using exact syntax and he forgot about multiple players. The script should be more like this:
  Code:
OnMissionStart; var I, P; begin for P := 0 to <number of locations on your map minus 1> do if States.PlayerEnabled(P) then begin for I := 0 to 28 do States.HouseAllow(P, I, False); States.HouseAllow(P, _store, True); States.HouseAllow(P, _school, True); end; end; OnHouseBuilt(aHouse: Integer); begin if States.HouseType(aHouse) = _school then HouseAllow(States.HouseOwner(aHouse), _inn, True); if States.HouseType(aHouse) = _quarry then HouseAllow(States.HouseOwner(aHouse), _woodcutter, True); end;
Replace _something with the house number (same as you use in .dat scripts).
In
  Code:
I := 0 to 28
What exactly does that colon mean? This may sound like a silly question, but understanding the meaning of syntax helps me grasp it better.

Also,
  Code:
States.HouseAllow(P, I, False)
This looks like it is blocking all houses for all players, right? I is the variable that represents all houses, and P is the variable that represents all players (These are variables, right? Or are they slightly different in some way?). What if I just wanted to enable it for one player? What code do I use?

A side question: Is this language case sensitive?

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 16:54
by Gradius
Sorry for the 'OT but wanted to ask if the action will be implemented in order to eliminate the road, so that when you have two stores, it's not that the servants vadino to take stuff elsewhere .. also always asked for the reason of the two deposits, which if you could take off the street, and the servants are created from the school vault A, these will work only for their "fence" of the road, so not going to vault B in the case they serve the servants to bring such food at the inn ..
Sorry if it is not clear for google translate

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 17:12
by Ben
I understand that Gradius is using a translator, so he may not understand the layout of the forum very well, so can an administrator kindly move his post to the appropriate thread (maybe the ideas topic) and delete this post of mine? This scripting is very important to me. Thank you.

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 18:07
by Lewin
In
  Code:
I := 0 to 28
What exactly does that colon mean? This may sound like a silly question, but understanding the meaning of syntax helps me grasp it better.

Also,
  Code:
States.HouseAllow(P, I, False)
This looks like it is blocking all houses for all players, right? I is the variable that represents all houses, and P is the variable that represents all players (These are variables, right? Or are they slightly different in some way?). What if I just wanted to enable it for one player? What code do I use?

A side question: Is this language case sensitive?
In Pascal:
A:=B means "set A to equal B"
A = B means "is A equal to B (true or false)"

Also, read this:
http://en.wikipedia.org/wiki/For_loop

For loops are a basic programming structure that allow you to do something over and over again. In this case, the for loop runs 29 times. First it runs with I=0, then I=1, then.... I=28. Hence "I:=0 to 28". Yes I and P are integer variables, you can see that they're declared within the scope of that function. So yeah, within the scope of that for loop I does many "every house" if you want to think of it that way, but sometimes the order you do things in matters so you need to keep in mind how it really works underneth. The purpose of that code is to set every house to "not allowed" meaning it shows with a "?" in the build menu, but it can still be allowed later if you build the house that unlocks it (so you could change it to only unallow the school and inn if you wanted, but this way makes sure it overrides any changes made in the .dat script).

The language is not case sensitive, but it's much less readable if you don't use camel case.

Re: Dynamic Script Usage

PostPosted: 10 Apr 2013, 19:53
by Ben
Okay, thanks. That's all for now.

I'm used to not capitalizing the first word in my variables, but I like the way you are doing it here (in fact, I think that I was using your method before my professor told me to try not capitalizing the first word) so I think I will train myself to do it your way.

Re: Dynamic Script Usage

PostPosted: 11 Apr 2013, 06:09
by Krom
First word capitalization is just a matter of taste and coding style accepted in certain circles (uni, work, team, programming language, etc). Also some languages are case sensitive (e.g. JavaScript) and some are not (Delphi, Pascal). Since KaM dynamic scripting is done in Pascal you can use any case that suits you.

Re: Dynamic Script Usage

PostPosted: 11 Apr 2013, 12:40
by Ben
The list of codes in the wiki, are they actual code taken from the Remake's source? That is, for example, is OnHouseBuilt the code you use to detect when a house is done?
The reason I'm wondering is because I'm curious if the list of codes for dynamic script are limited to the ones in the wiki.

Re: Dynamic Script Usage

PostPosted: 11 Apr 2013, 13:14
by Krom
We have a layer between scripts and main code so that we can change either side without affecting the other. The list of scripts is limited to those we expose through that layer.

Re: Dynamic Script Usage

PostPosted: 11 Apr 2013, 13:15
by Lewin
The list of codes in the wiki, are they actual code taken from the Remake's source? That is, for example, is OnHouseBuilt the code you use to detect when a house is done?
Not exactly... We wrote the Remake from the ground up. There's no level lower that can provide a "OnHouseBuilt" event for us to use, we wrote everything there is about houses ourselves. In fact, the game code is that lower level. When a house is built we check "does the mission script have an OnHouseBuilt event?" If yes, we run that event.
The reason I'm wondering is because I'm curious if the list of codes for dynamic script are limited to the ones in the wiki.
No, the ones on the Wiki are the only ones available. We have to implement every command listed there, by providing a layer between the script and the game code (which is very raw, easy to break, always changing and not as simple to use as we want scripts to be). That layer is this file:
http://code.google.com/p/castlesand/sou ... ingESA.pas

When you script, you only get access to those two classes, States and Actions. If we let you access the game structures directly scripting would be as difficult as writing the game itself is, and you'd have to change your script as we change/improve/rewrite the game code. So we made States and Actions and that's all which gets exposed to the script.

Re: Dynamic Script Usage

PostPosted: 12 Apr 2013, 03:06
by Ben
The game isn't recognizing "HouseAllow" Is it a bug or have I mistyped something?
  Code:
procedure OnMissionStart; var I, P: Integer; begin for P := 0 to 5 do if States.PlayerEnabled(P) then begin for I := 0 to 28 do States.HouseAllow(P, I, False); States.HouseAllow(P, 11, True); States.HouseAllow(P, 13, True); end; end;
It's not understanding line 8. I'm, assuming lines 10 and 11 won't work either.