Small problem with interscript referencing

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
squark
Veteran
Posts: 277
Joined: Fri Aug 17, 2007 9:59 am
Completed: Tour de Pharmacy (Proofreader), The Abandoned Land: Book One (Proofreader)
Projects: Raven Hollow (Core Coder, Proofreader), The Universal Hope (Core Coder, Proofreader)
Organization: FlareBlue Entertainment
Location: Always somewhere behind you...
Contact:

Small problem with interscript referencing

#1 Post by squark » Fri Oct 22, 2010 8:15 am

I've made a fair bit of progress, but I'm having a problem. I had the idea to use a definition in the script.rpy file for enemy data. So I tried it with everybody's favourite, a Rat (besides the point, but anyway).
I assigned it an image and defined every single one of it's stats.
The game compiles and runs perfectly UNTIL it gets to the point where it should reference the data itself, but it crashes with a traceback of "ratHP not defined" or something along those lines. I checked the spelling and definition structure; everything looks a-okay.
Do I have to define the stats inside the seperate combat script I have? Or can I use a subroutine to poke the script.rpy and then continue loading the combat (which, admittedly is not quite so problematic as I first thought... Just making it look pretty IS).
Anyway, thoughts, people?
Again, I've checked spellings and existing structure.
Without communication, nothing is possible.
"All we see or seem
Is but a dream within a dream.

I stand amid the roar
Of a surf-tormented shore"
-- Edgar Allen Poe, "A Dream Within A Dream"
Current Projects:
Universal Hope Stalled
Raven Hollow (on hold for now)
Peace and Love,
Squark

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: Small problem with interscript referencing

#2 Post by fortaat » Fri Oct 22, 2010 8:56 am

Do you have two different files (battle.rpy, script.rpy)?
Could you post the code where you define ratHP, and the code where you call it?

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Small problem with interscript referencing

#3 Post by Jake » Fri Oct 22, 2010 8:58 am

squark wrote: Do I have to define the stats inside the seperate combat script I have? Or can I use a subroutine to poke the script.rpy
You may want to read up on 'scope' in Python. Without knowing how and when the variable is defined and how and when you're trying to access it, it's a little hard to suggest reasons why it might not be working, but I can tell you in general:

- You should be able to define variables in your Ren'Py script on lines starting with a '$' and have them accessible in any other part of your Ren'Py script from that point onward. If you can't, then make absolutely sure that the variable is being set before the point that it errors - check via the developer menu, for example, which you can access by hitting Shift-D and looking at the 'Variable Viewer'.

- Try and avoid declaring variables you're going to change the value of in an init block; instead, set them to their default value immediately after the 'start' label.

- In Python, you can generally access variables defined in Ren'Py script with no problems. However, there are situations where Python decides a variable must be local to a particular function - basically when you assign to a variable in a function - which can be fixed by inserting the line "global <variable name>" at the top of your function (however, you would be getting a different error if this were the cause of your problem).

- If you're really having trouble getting at a Ren'Py-script-defined variable in Python, try accessing "store.<varname>" instead of "<varname" and see if that works, just in case. It shouldn't make any difference, but I'm sure I've had problems with it before in some peculiar set of circumstances.
Server error: user 'Jake' not found

User avatar
Sapphi
Eileen-Class Veteran
Posts: 1685
Joined: Fri Jun 05, 2009 3:31 am
Completed: Boku no Taisetsu na Yumeko
Projects: Twelve, PAW ★ PRINTS
Organization: Kitsch-soft
Location: Illinois, USA
Contact:

Re: Small problem with interscript referencing

#4 Post by Sapphi » Sat Oct 23, 2010 3:10 am

I had a somewhat similar issue come up while I was writing my script recently. I decided I wanted to put in a new character and have his name a variable. I added everything to the script correctly, then loaded from a save point so that I wouldn't have to skip over tons of text to test him out. When I got to him it crashed and told me I hadn't defined his name.

I ended up deleting the persistent while trying to solve the problem, but in the end I think it was something weird with the save. I recalled that it had happened to me before on a different instance of this project, so I ended up skipping through everything manually again and it worked like a charm.

I'm definitely not knowledgeable in Python or anything, but as I said, my problem seemed to be that the save file I was loading wasn't incorporating all the new data. If you're loading from a save for quicker testing, maybe we had the same problem?
"It is [the writer's] privilege to help man endure by lifting his heart,
by reminding him of the courage and honor and hope and pride
and compassion and pity and sacrifice which have been the glory of his past."
— William Faulkner
▬▬▬▬▬▬▬▬▬▬..+X+..▬▬▬▬▬▬▬▬▬▬
Image

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Small problem with interscript referencing

#5 Post by Jake » Sat Oct 23, 2010 6:54 am

Sapphi wrote:the save file I was loading wasn't incorporating all the new data.
When Ren'Py loads a save, it basically constructs your loaded-game-state like this:

- Start from a blank state with no variables or definitions or anything in it
- Run everything in all the init blocks, to set up the static doesn't-change-between-games data like image and character definitions
- Load in all the variables listed in the save game and their values
- Jump to the point in the script described by the save game

So loading from saves during development can have several pitfalls, and the most notable two are:

- Sometimes the point that you saved at in your script doesn't exist any more; in this case, Ren'Py will just crash
- Loaded saves will only pick up changes that are made in init blocks - it won't run any of the actual game script between the start of the game and the point the save was made at.

So if you've set up variables near the beginning of your script (say, asking the player what their name is) since you saved that particular game-save, then those variables won't (and can't) exist in the save, so when your later code references them, Ren'Py will crash. The best solution is to play through the game again.
Server error: user 'Jake' not found

User avatar
Sapphi
Eileen-Class Veteran
Posts: 1685
Joined: Fri Jun 05, 2009 3:31 am
Completed: Boku no Taisetsu na Yumeko
Projects: Twelve, PAW ★ PRINTS
Organization: Kitsch-soft
Location: Illinois, USA
Contact:

Re: Small problem with interscript referencing

#6 Post by Sapphi » Sat Oct 23, 2010 12:29 pm

Wow, thanks for the detailed explanation. ^_^
"It is [the writer's] privilege to help man endure by lifting his heart,
by reminding him of the courage and honor and hope and pride
and compassion and pity and sacrifice which have been the glory of his past."
— William Faulkner
▬▬▬▬▬▬▬▬▬▬..+X+..▬▬▬▬▬▬▬▬▬▬
Image

User avatar
squark
Veteran
Posts: 277
Joined: Fri Aug 17, 2007 9:59 am
Completed: Tour de Pharmacy (Proofreader), The Abandoned Land: Book One (Proofreader)
Projects: Raven Hollow (Core Coder, Proofreader), The Universal Hope (Core Coder, Proofreader)
Organization: FlareBlue Entertainment
Location: Always somewhere behind you...
Contact:

Re: Small problem with interscript referencing

#7 Post by squark » Mon Nov 08, 2010 12:35 pm

Okay.... I'm sure I've made an elementary mistake here, but it's evading me thus far. And in deference to my previous post, I'm just going to post some errant code anyway.

Code: Select all

On line 82 of C:\Games\Dev\Raven Hollow/game/combat.rpy: expected statement.
Experience += ratXP
           ^
On line 105 of C:\Games\Dev\Raven Hollow/game/script.rpy: expected statement.
global Power(self, item, type) :
       ^
I've got ratXP and Experience set with $ in the script. This is confusing me.

As regards, global Power, it goes on with this following after one tab space.
pipe.item.type = Power
self.attack += 2

I've cleared up most of the other problems I've had but these are the most persistant.

Help me, please! *refrains from tearing hair out and eating it*
Without communication, nothing is possible.
"All we see or seem
Is but a dream within a dream.

I stand amid the roar
Of a surf-tormented shore"
-- Edgar Allen Poe, "A Dream Within A Dream"
Current Projects:
Universal Hope Stalled
Raven Hollow (on hold for now)
Peace and Love,
Squark

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Small problem with interscript referencing

#8 Post by Jake » Mon Nov 08, 2010 1:45 pm

squark wrote: I've got ratXP and Experience set with $ in the script. This is confusing me.
You need to use a '$' at the beginning of every single line in which you modify those variables, not just the ones they're first declared on. Basically, all variable-modification is done in Python, not strictly in Ren'Py script, and the '$' tells Ren'Py that the rest of the line is a Python instruction, and it shouldn't try and interpret it as Ren'Py commands.

squark wrote: As regards, global Power, it goes on with this following after one tab space.
pipe.item.type = Power
self.attack += 2
What exactly are you trying to do here? It doesn't look anything like a valid Ren'Py command, and I don't see exactly what you're trying to do, so it's hard to say what needs to be changed...
Server error: user 'Jake' not found

User avatar
squark
Veteran
Posts: 277
Joined: Fri Aug 17, 2007 9:59 am
Completed: Tour de Pharmacy (Proofreader), The Abandoned Land: Book One (Proofreader)
Projects: Raven Hollow (Core Coder, Proofreader), The Universal Hope (Core Coder, Proofreader)
Organization: FlareBlue Entertainment
Location: Always somewhere behind you...
Contact:

Re: Small problem with interscript referencing

#9 Post by squark » Mon Nov 08, 2010 2:01 pm

Seems like that should fix experience allocation. Thanks. So my += wasn't causing the error, regardless of the traceback :P

Basically, what I'm trying to do with the second item is I'm trying to assign it an attribute so stat/type modification will be easier... At least, that was my thinking.

For example, the player mainly uses Power-type weapons (pipes, hammers, fists/feet, and so on) and the base Power stat will increase faster per level than the others. The converse is also true as regards speed/arcane/dextrous types as well.

I was flummoxed as to how to implement it, so I spent quite a while and it seems I've cocked it.
Without communication, nothing is possible.
"All we see or seem
Is but a dream within a dream.

I stand amid the roar
Of a surf-tormented shore"
-- Edgar Allen Poe, "A Dream Within A Dream"
Current Projects:
Universal Hope Stalled
Raven Hollow (on hold for now)
Peace and Love,
Squark

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Small problem with interscript referencing

#10 Post by Jake » Mon Nov 08, 2010 2:26 pm

squark wrote: Basically, what I'm trying to do with the second item is I'm trying to assign it an attribute so stat/type modification will be easier... At least, that was my thinking.
OK, but what exactly are you expecting those lines of code to do, on a command-by-command basis? Can we see a few lines either side of the relevant ones to see it in context?

In short: 'global' doesn't mean anything in Ren'Py, but it's got meaning in Python. In Python, however, it's a scope-modifier that can apply to a single variable name, but you seem to be trying to set up a function...?
Server error: user 'Jake' not found

User avatar
squark
Veteran
Posts: 277
Joined: Fri Aug 17, 2007 9:59 am
Completed: Tour de Pharmacy (Proofreader), The Abandoned Land: Book One (Proofreader)
Projects: Raven Hollow (Core Coder, Proofreader), The Universal Hope (Core Coder, Proofreader)
Organization: FlareBlue Entertainment
Location: Always somewhere behind you...
Contact:

Re: Small problem with interscript referencing

#11 Post by squark » Mon Nov 08, 2010 3:07 pm

Okay, I've sorted the Experience += thing, thanks Jake.

I've put every def and the global Power coding into one python block, now it's returning this:

Code: Select all

On line 105 of C:\Games\Dev\Raven Hollow/game/script.rpy: invalid syntax
global Power(self, item, type) :
             ^
As I haven't quite got dynamic stat modification down yet, I'll probably comment it out until I get a bit better at this.

But you asked for a few lines before that so I'll post the ones I think are most likely the ones that matter.

Code: Select all

 python :
        
            class Item:
                def __init__ (self, name, type, cost) :
                    self.name = name
                    self.type = type
                    self.cost = cost
    
            class Inventory:
                def __init__(self, money=0) :
                    self.money = money
                    self.items = [ ]

                def has_item(self, item) :
                    if item in self.items :
                       return True
                    
                    else :
                       return False
These are all after the label start header, and before the thing that's kicking my backside. As I said, I have no idea how to continue it for dynamic stat modification.
I was hoping for something like The Elder Scrolls, where the more you use one type of weapon more than the others - weapons were bound to one paticular stat - you'd get more points to spend on that stat, but I was hoping to do it without needing points to spend - just done on the fly on level up.
I know my code is perhaps horrible, but this is really important to me.
Without communication, nothing is possible.
"All we see or seem
Is but a dream within a dream.

I stand amid the roar
Of a surf-tormented shore"
-- Edgar Allen Poe, "A Dream Within A Dream"
Current Projects:
Universal Hope Stalled
Raven Hollow (on hold for now)
Peace and Love,
Squark

User avatar
Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Small problem with interscript referencing

#12 Post by Jake » Mon Nov 08, 2010 3:36 pm

squark wrote: These are all after the label start header
Firstly - I would advise putting your class definitions in an 'init python:' block - like that they'll be available anywhere in your script, not just the points after the place you wrote them.

Secondly - I still don't really follow what you're trying to get your 'global Power' definition to actually do... is it part of the Inventory class? Are the 'type' and 'item' in "pipe.item.type = Power" supposed to be the same 'item' and 'type' in the parameters you've given in parentheses?



As to dynamic stat modification - do you have a 'fighter' or 'character' class which is the thing that's going to be gaining XP? I would advise just putting the raw stats as fields on that class (like you have with 'items' and 'money' in the Inventory class), have more fields for the amount of XP/usage that character has for each kind of weapon, and then you can have a function on that class (def ...) to return the calculated value of 'Power' or whatever to use as the working-value of the stat at all times.

You may find this thread useful:
http://lemmasoft.renai.us/forums/viewto ... f=8&t=8318
- you basically seem to be after the same kind of thing as Melkoshi was.
Server error: user 'Jake' not found

Incendium
Regular
Posts: 25
Joined: Wed Dec 09, 2009 12:57 am
Contact:

Re: Small problem with interscript referencing

#13 Post by Incendium » Mon Nov 08, 2010 3:40 pm

"global Power(self, item, type) :" is not valid Python. Change it to "def Power(self, item, type):" and you should be good. You don't need the global keyword unless you are trying to modify an existing variable in the global scope from another scope (a function runs in a different scope). Here's an example:

Code: Select all

test_var = 'hi'

def func():
    test_var = 'hello' # only changed in the local scope, discarded after function finishes executing

func()
print test_var # outputs 'hi'

Code: Select all

test_var = 'hi'

def func():
    global test_var
    test_var = 'hello' # changed in both the local and global scope

func()
print test_var # outputs 'hello'

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]