Please login or register.

Login with username, password and session length
Advanced search  

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Pages: [1] 2  All

Author Topic: Global variable becoming corrupted?  (Read 7815 times)

0 Members and 1 Guest are viewing this topic.

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Global variable becoming corrupted?
« on: February 20, 2008, 11:00:46 AM »

I have a scene_init that sets a global variable to contain the letter "A". I then pass that variable to another script to make that "A" part of the name of a sprite entity, then display the entity. But the "A" never gets there. When it arrives, it's a "D". I've commented out everything else but the departing Game.Msg and the arriving one both show that the variable has changed en route.

scene_init is:

// setup scene according to state variables
global CL = null;
global CN = null;
CL = "A";
Game.Msg("At scene_init, CL is " + CL);
CN = 4;
this.AttachScript("scenes\01_Forecourt\X11E_Zm_lock\scr\Combo_Disp.script");
//CL = "B";
//CN = 9;
//this.AttachScript("scenes\01_Forecourt\X11E_Zm_lock\scr\Combo_Disp.script");
//CL = "C";
//CN = 8;
//this.AttachScript("scenes\01_Forecourt\X11E_Zm_lock\scr\Combo_Disp.script");
//CL = "D";
//CN = 6;
//this.AttachScript("scenes\01_Forecourt\X11E_Zm_lock\scr\Combo_Disp.script");

display script is:

#include "scripts\base.inc"

global CL;
Game.Msg("At Combo_Disp CL is " + CL);
global CN;
var NumRef;
var CR;
var ItemNormal;

NumRef[0] = "0";
NumRef[1] = "1";
NumRef[2] = "2";
NumRef[3] = "3";
NumRef[4] = "4";
NumRef[5] = "5";
NumRef[6] = "6";
NumRef[7] = "7";
NumRef[8] = "8";
NumRef[9] = "9";

// Display the next number in this column
CR = CL + NumRef[CN];
Game.Msg("CR contains " + CR);
ItemNormal = Scene.GetNode(CR);
ItemNormal.Active = true;

// What was the last number in this column?
//CN = CN - 1;
//if(CN < 0)
//  {
//  CN = 9;
//  }
// Switch it off
//CR = CL + NumRef[CN];
//ItemNormal = Scene.GetNode(CR);
//ItemNormal.Active = false;

// Initialize
//CL = null;
//CN = null;
//CR = null;

I'm at a loss - to me it looks like a simple corruption. What have I missed?

Thanks

Kaz
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #1 on: February 20, 2008, 11:16:01 AM »

post contents of your base.inc as well.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #2 on: February 20, 2008, 11:28:45 AM »

#include "scripts\const.inc"

global Scene;
global Keyboard;
global actor;
global gNBInventoryActive;
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Global variable becoming corrupted?
« Reply #3 on: February 20, 2008, 11:35:09 AM »

You need to understand how AttachScript works. It just schedules the script to be executed, but the script invoking AttachScript does NOT wait for the other script.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #4 on: February 20, 2008, 11:43:01 AM »

Forgive my ignorance - I'm not a programmer by trade, so I don't have your experience and so I don't understand your answer. Perhaps attachscript was not the way to go? Should I be using some other method to pass one variable to another script and insist on its immediate execution or can you demonstrate how I might use attachscript to achieve that effect?

Thanks

Kaz
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #5 on: February 20, 2008, 12:14:40 PM »

I think the best would have been if you described a bit more what are you trying to achieve. You can use many methods (methods, events etc.) but it's not clear what's the purpose of your code to give you good solution.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #6 on: February 20, 2008, 01:17:32 PM »

It's a combination padlock with four numbers - each number can be rotated 0-9 by clicking a button, named A, B, C, D. Clicking button 'A' runs a script to set global CL = "A". It then adds one to a global CN, CN = CN+1, setting it to 0 if it's over 9, then this.attaches a script. The script takes the letter, parses the number through an array to get its Ascii equivalent, adds the letter to the ASCII number equvalent to produce the name of a sprite node, then sets that node to active. All the code is included above. It's just that when I send the letter 'A', the letter 'D' arrives.

Help file says that the attachscript is executed - that's why I don't understand Mnemonic's answer. It seems to be at odds with what the help file says.

Thanks

Kaz 
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #7 on: February 20, 2008, 01:32:26 PM »

I used the very same thing in Ghost in the Sheet. Let's see how I did it there:

I had 8 region entities labeled U1 - U8 for upper tumbler buttons and D1-D8 for lower tumbler buttons. In your case you'll have only one rotating button for each, right?

I attached common script to them:

Code: [Select]

on "LeftClick"
{
var tm = new String(this.Name); 
var w = tm.Substr(0,1);  // this gets U (upper button) or D (lower button)
var ww = ToInt(tm.Substr(1,1)); // this gets number of the button
global panelObject;
        panelObject.Set(w,ww);
}

Now panelObject is an object already initialized through Game.CreateObject() and placed into global variable panelObject.
I am also using dynamic overlay loading so I don't have to place them all in scene editor.

Also I have 8 overlay folders each with overlays named 0-9 png  because the overlays look differently on each part of the machine. So I have to construct correct path
and then Call SetSprite to assign correct image to the entity.

Relevant function is:

Code: [Select]
method Set(how,what)
{
var e;

if (how == "U") w[what-1] = w[what-1] + 1;
if (how == "D") w[what-1] = w[what-1] - 1;

if ( how == "U" && w[what-1] == 10) w[what-1] = 0;
if ( how == "D" && w[what-1] == -1)  w[what-1] = 9;

e = Scene.GetNode("DO"+what); 
var file  = "panelducholap\"+what+"zprava\"+w[what-1]+".png";
e.SetSprite(file);
}


Hope this helps. Let me know if anything is unclear.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: Global variable becoming corrupted?
« Reply #8 on: February 20, 2008, 06:11:15 PM »

Help file says that the attachscript is executed - that's why I don't understand Mnemonic's answer. It seems to be at odds with what the help file says.
It gets executed, but not the way you need (i.e. not immediately). The method meta suggested is much cleaner. The other script will contain a public method, you only attach the script once, and then call the method inside.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #9 on: February 21, 2008, 09:36:24 AM »

Thanks Mnemonic, that's much clearer.

 ::hijack ...I was thinking more about this. Two truths; 1) Game developers (i.e. The Ones Most Likely To...) are most likely artists and writers first, programmers second. 2) WM doesn't implement full C++ so there's no point in learning the entire language, but it gives you more than enough to make a magical gaming experience. Put these two together and you have what attracts game developers to WM.

The problem is that even with WM, the developer MUST have, or acquire at least a modicum of understanding about object-oriented thinking, then expressing that in an esoteric language. This is a large expectation and I wonder how many developers have failed because they could not make that leap from artist to programmer.

Sure, you, Meta and others do much to help the Newbie, but I can't help thinking that a more example-and-explanation based approach to the core documentation would help enormously. For instance - there is nothing in the docs to insist that a Method() would be needed in an attached script, or indeed why. This is not a criticism, but a suggestion - I wonder if WM would be even more popular if its documentation became more programmer-to-artist than its present tendency to be programmer-to-(you-already-know-all-about-OOP-Right?)-programmer. Time to complete the 'Advanced' tutorials, perhaps?

Can't help thinking that it's in your interests also. If inventive artists and writers are necessary for commercially successful games, then more artistic Muties would likely bring more commercial success, which in turn would bring more donations. I wonder how many do not donate because their games don't make a fiscal surplus? I wonder too how much less busy you would be answering case-by-case issues on the technical forum?

Cheers,

Kaz 
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #10 on: February 21, 2008, 10:43:11 AM »

Kaz: there's always my online book, which describes this quite throughly, isn't it? ;)

http://forum.dead-code.org/index.php?topic=2600.0



Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #11 on: February 21, 2008, 11:26:28 AM »

Well I thought it was clearer but clearly I don't know how to 'call' a method. So I'm stuck again. I don't know what "call" means because the code below gives me "CheckGarage not referenced". I've looked in the book and can't find any definition of what "call a method" means in terms. I know it must look dead basic to you and I am genuinely sorry to appear so daft but, Help!

I've got a script that goes:

#include "scripts\base.inc"

global ND;
var ItemNormal;
////////////////////////////////////////////////////////////////////////////////
on "LeftClick"
{
//increment dial
ND = ND + 1;
if(ND > 9)
  {
  ND = 0;
  }
//display digit
var spritename = "scenes\01_Forecourt\X11E_Zm_Lock\"+ND+".png";
ItemNormal = Scene.GetNode("D");
ItemNormal.SetSprite(spritename);
//have we got the combination?
Scene.AttachScript("scenes\01_Forecourt\X11E_Zm_Lock\scr\check.script");
this.CheckGarage();
}

While in 'check.script' I've got:

#include "scripts\base.inc"

global ND;
global gGarageOpen;
method CheckGarage()
  {
  if(ND == 1)
    {
    gGarageOpen = true;
    Game.PlaySound("sounds\unlock.ogg");
    Game.ChangeScene("scenes\01_Forecourt\X11E_DoorOpen\X11E_DoorOpen.scene",0,0);
    }
  }
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #12 on: February 21, 2008, 11:40:23 AM »

it's in the Survivor's guide to scripting. Anyway, the book is not a reference guide, so quick looking there won't give you anything. It requires to read it from beginning to the end as the knowledge there gained is heavily used in following chapters.

Re. your question, it's again the very same problem. You attach the script and then call the method immediately (before the script even makes it to be attached). Attach this script in scene_init.script and you'll see that it works correctly. If you really need it here, then check the method IsScriptRunning and make a Sleep(1) loop like this:

while (!Scene.IsScriptRunning(your script)) Sleep(1);

then you should be sure it's attached.

But my approach I've suggested was a bit different. Instead of AttachScript I've created object which doesn't have any scheduler issues:

var yourobject = new Object("path to your check.script");
yourobject.CheckGarage();


Edit: script corrections.
« Last Edit: February 21, 2008, 11:48:15 AM by metamorphium »
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Global variable becoming corrupted?
« Reply #13 on: February 21, 2008, 11:46:39 AM »

btw. any reason why you don't use normal function instead?
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Kaz

  • Arberth Studios
  • Regular poster
  • ***
  • Karma: 1
  • Offline Offline
  • Posts: 228
  • The story is the game
    • View Profile
    • Info on 'Rhiannon' & 'Coven'
Re: Global variable becoming corrupted?
« Reply #14 on: March 03, 2008, 06:16:49 PM »

Hi Metamorphium

Took me a while to get back to this part of development but thanks to the help from you and Mnemonic, I got it working, and I've learned a bit more about attaching scripts and methods now.

Why not use function? Coz I haven't figured out the difference between that and method yet, but method works, so I can crack on. Thanks again.

Ta

Kaz
 
Logged
\"Fans of popular horror adventures like Scratches and Barrow Hill should start bracing themselves for another haunting experience, as independent developer Arberth Studios has announced production on its debut title Rhiannon - Curse of the Four Branches.\" - AdventureGamers.com
Pages: [1] 2  All
 

Page created in 0.039 seconds with 23 queries.