Using custom variables

Started by sentient, Sat 22/05/2004 15:00:53

Previous topic - Next topic

sentient

I'm having troubles with creating my own variables. I want to have a variable which stores my position in the game. When I move from one section of the map to another I will change the variable to suit.

For instance I am using

int location;
location=2;

This works when I place it in the Global script under Game Start. Well I say it works, let's just say it doesn't give a parser error. I haven't had chance to see if it works yet.

If I put in a line say,

if (location != 1)
blah... blah...

I get a parser error saying unexpected"location". Which I would think means it doesn't recognise my variable. I have tried using export loaction; at the end of the script like I have seen in an FAQ but that brings an error message also. Likewise using import location; just in case I need that. I am stuck yet again :(

strazer

If you use

int location;
location=2;

within a function, the variable is only declared for this function and is destroyed when this function ends (="local variable").

Put

int location=2;

at the top of the global script, outside of any functions. This way, the variable is accessible throughout the entire global script.

To make it accessible in room scripts as well:

End of global script: export location;

Script header: import int location;

It didn't work for you before because the variable was not declared correctly.

sentient

OK, that has got that a bit further. When I try to run this however I get a further error message.

// main global script file
int location=2;

function dialog_request (int locationselect){
if (location != 1)
if (locationselect == 1)
if (character[GetPlayerCharacter()].inv[1] > 0)
SetPlayerCharacter(HANSON);
else
DisplayMessage (500);
RunDialog(1);

I have several other options as well as this hence I haven't ended it with }. But this is the first one I have tried to add this option to. In basic I would need an endif statement for each if statement. Do I need to do something similar in this? I didn't need any before I put in the if (location !=1) option. I thought this would check if location DOESN'T =1 and then carry on to the next line. I have a feeling it is something to do with the else command I have put in to give a response if I don't have the inv item.

The error I am getting is error:newroom:two newroom/rundialog/stopdialog requests within dialog

Oh and as a side note can I get the message that comes up to be voiced by the character I am using eg HANSON: (500);. Instead of the standard message I get? I know the above doesn't work, but I'm sure there is a way.

strazer

#3
If you have 2 or more commands after a conditional, you have to enclose them in brackets, like this:

function dialog_request (int locationselect) {
  if (location != 1) {
    if (locationselect == 1) {
      if (character[GetPlayerCharacter()].inv[1] > 0) // no brackets needed since there's only 1 command following
        SetPlayerCharacter(HANSON);
    }
    else {
      DisplayMessage(500);
      RunDialog(1);
    }
  }
}

Quotecan I get the message that comes up to be voiced by the character I am using

Yes:

string buffer;
GetMessageText(500, buffer);
DisplaySpeech(GetPlayerCharacter(), buffer);

Edit: Added dialog_request function header

sentient

#4
Whoa, lot's of brackets!
Ok, so if I show you my entire dialog script as it stands at the moment. I need to put all those brackets around each one? I am goinf to add the if (location!=1) to each one changing the 1 for the relevant number. This is an attempt to stop you selecting your current location.

/ main global script file
int location=2;

function dialog_request (int locationselect){
if (location != 1)
if (locationselect == 1)
if (character[GetPlayerCharacter()].inv[1] > 0)
SetPlayerCharacter(HANSON);
else
DisplayMessage (500);
RunDialog(1);

if (locationselect == 2)
if (character[GetPlayerCharacter()].inv[1] > 0)
SetPlayerCharacter(HANSON);

if (locationselect == 3)
if (character[GetPlayerCharacter()].inv[1] > 0)
SetPlayerCharacter(HANSON);

if (locationselect == 4)
if (character[GetPlayerCharacter()].inv[1] > 0)
SetPlayerCharacter(HANSON);}

Oh and the message question.. I want it to be the character I am talking to, not my character.

strazer

Right, technically, you don't need brackets for every
if (character[GetPlayerCharacter()].inv[1] > 0) SetPlayerCharacter(HANSON);
but I recommend adding the brackets anyhow in case you decide to put more lines in there later.

I don't know what you're trying to do exactly. There's a lot of duplicate code in there. Could you elaborate?

QuoteI want it to be the character I am talking to, not my character.

You'd have to store the number of the character you're talking to in a variable before running the dialog. Then you could do
  string buffer;
  GetMessageText(500, buffer);
  DisplaySpeech(VARIABLECONTAININGCHARACTERYOURETALKINGTO, buffer);

sentient

In the game I am trying to do, I have a cab driver who can take you to each major location. When you select a location you then go to the map screen where the cab will move to the location you specify. I am trying to make it so you aren't given the location you are at as an option. Hence the if (location !=1) line. So when you are at location 1 you only get the options for 2,3 and 4 for instance. Location 2 would give you 1,3 and 4 etc.

The code at the moment just takes you to the map screen if you have item 1 (payment for the fare). I plan to alter each section of code to alter where the cab will drive to when selected.
At the moment I just have a map screen that takes you to the same location each time, but I would have thought that should be easy to change once I get this selection process sussed.

strazer

#7
I was thinking about doing something like this myself, but haven't got around to it yet.
How about this (haven't tested it):

- CAB: Car character in map room
- CABDRIVER: Character to talk to when you want to use the cab
- Dialog topic 1: Talk to cab driver
- GlobalInt 77 stores which location to drive to when entering map screen

Dialog script for Topic 1

@S
option-on 1
option-on 2
option-on 3
option-on 4
//...
run-script 10
return

@1 // ego: Take me to location 1
set-globalint 77 1
run-script 11
return

@2 // ego: Take me to location 2
set-globalint 77 2
run-script 11
return

//...

Global script

int location=2; // game starts at this location

function dialog_request (int parameter) {
  if (parameter == 10) { // run-script 10: dialog startup
    SetDialogOption(1, location, 0); // turn off dialog option for current location
      // location number=dialog option number for this to work
  }
  else
  if (parameter == 11) { // run-script 11: player has chosen a destination
    if (character[GetPlayerCharacter()].inv[1]) { // player has cab fare
      location = GetGlobalInt(77); // set current location to destination
      NewRoom(MAPROOMHERE); // go to map room
    }
    else { // player doesn't have the cab fare
      string buffer;
      GetMessageText(500, buffer);
      DisplaySpeech(CABDRIVER, buffer); // cabdriver: Sorry, not enough money!
    }
  }
   
}

Map room

Player enters screen (after fadein)

  StartCutscene(4);
  if (GetGlobalInt(77) == 1) { // Player has selected location 1
    MoveCharacterBlocking(CAB, x, y, 0); // drive to location 1 on the map
    NewRoom(z); // go to this room when arriving at location 1
  }
  else
  if (GetGlobalInt(77) == 2) { // location 2
    MoveCharacterBlocking(CAB, x, y, 0); // drive to location 2
    NewRoom(z);
  }
  //...
  EndCutscene();

Edit: Changed everything back. This happens if I start to think too much. :P
Should work as it is now.

sentient

Ok, after a little tweaking I have got that to work! Thanks a lot. The StartCutScene stumped me for a min, but I realised it was StartCutscene that was needed (No Capital on scene), likewise for EndCutscene. I had to put in or take out (can't quite remember which) an } in the main script and change names to match the ones in my game.

That is a pretty useful bit of code, and I am starting to understand the scripting a little bit by putting this stuff in ( I think!)

sentient

Oh another problem. I'm trying to use SetPlayerCharacter(EGO) command to switch the scene to the new location. But it is doing it straighaway and not waiting for the cab to get to it's destination!

StartCutscene(4);
  if (GetGlobalInt(77) == 1) { // Player has selected location 1
    MoveCharacter(HANSON, 375, 112); // drive to location 1 on the map
    SetPlayerCharacter(EGO);
  }
  else
  if (GetGlobalInt(77) == 2) { // location 2
    MoveCharacter(HANSON, 38, 58); // drive to location 2
    SetPlayerCharacter(EGO);
  }
  else
  if (GetGlobalInt(77) == 3) { // location 3
    MoveCharacter(HANSON, 17, 116); // drive to location 3
    SetPlayerCharacter(EGO);
  }
else
  if (GetGlobalInt(77) == 4) { // location 4
    MoveCharacter(HANSON,126, 150); // drive to location 4
    SetPlayerCharacter(EGO);
  }
  EndCutscene();

I realise the location will be wrong at the min but I just put this in to test it and found this problem. Isn't movecharacter a blocking script?

strazer

#10
Right, just use MoveCharacterBlocking.

Edit: Come to think of it, the whole SetPlayerCharacter thing is pretty unnecessary. See my modifications above.

Edit 2: And the location variable is unnecessary now, too. :P GI 77 holds that value.

Edit 3: Aargh, no it's not. run-script 10 needs it. :P

Edit 4: No, it doesn't. Or does it? :D

sentient

I have it working now thanks. Although it will take me a while to be able to work out scripts like that myself!

sentient

AGGGHH!! Why doesn't THIS work now?

function dialog_request (int parameter) {
  if (parameter == 10) { // run-script 10: dialog startup
    SetDialogOption(0, location, 0); // turn off dialog option for current location
      // location number=dialog option number for this to work
  }
  else
  if (parameter == 11) { // run-script 11: player has chosen a destination
    if (character[GetPlayerCharacter()].inv[3]) || (character[GetPlayerCharacter().inv[4]) { // player has cab fare
      location = GetGlobalInt(77); // drive to this location when in map screen
      SetPlayerCharacter(HANSON);} // go to map screen
    else { // player doesn't have the cab fare
      string buffer;
      GetMessageText(500, buffer);
      DisplaySpeech(CABBIE, buffer); // cabdriver: Sorry, not enough money!
    }
  }
   
}

All I did was put in the or command which for some strange reason is ||

strazer

#13
Braces.

if (character[GetPlayerCharacter()].inv[3]) || (character[GetPlayerCharacter().inv[4]) { // player has cab fare

has to look either like

  if ( (this) || (that) ) { // proper syntax

or like

  if ( this || that ) { // works too

Edit: Btw, the character[].inv variables are whole integers, not just 0 and 1.
So you could have an inventory item MONEY with the value representing how much of it you have.

sentient

Got it working now. Sorry I couldn't reply last night. For some reason I couldn't get onto this site. Probably my ISP I guess.

SMF spam blocked by CleanTalk