Grabbing Variables

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
Westeford
Regular
Posts: 151
Joined: Mon Jun 19, 2017 4:43 pm
Completed: 12 Hours to Die
Projects: Project Premonition
itch: westeford
Location: United States
Contact:

Grabbing Variables

#1 Post by Westeford »

So I'm trying to practice making an RPG battle system in Ren'Py. So I'm storing enemy names and stats like this

Code: Select all

define gakk_name = "Gakk"
define gakk_hp = 50
define gakk_atk = 1
And I want to import these stats into this

Code: Select all

define bad1_name = ""
define bad1_hp = 0
define bad1_atk = 0
then I thought, what if I had something like this

Code: Select all

define bad1 = ""

$ bad1_name = [bad1]_name
$ bad1_hp = [bad1]_hp
$ bad1_atk = [bad1]_atk
so if I made {bad1 = gakk}, bad1_name would search for {gakk_name} and so on.
I want to know how I could do this. I have no idea what to call this, but I hope I'm making some sense.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Grabbing Variables

#2 Post by enaielei »

You should be using `default` instead of `define` if you're planning to modify these variables later on.
For what you want, I suggest using a `dict` instead, or better yet just make your own class.

Code: Select all

default entities = dict(
  gakk=dict(name="Gakk", hp=50, atk=1),
  jakk=dict(name="Jakk", hp=20, atk=5),
)

default current = "gakk"

label fight:
  $ entity = entities[current]
  $ name, hp, atk = entity["name"]. entity["hp"], entity["atk"]
  "Your enemy is [name]. HP: [hp], Attack: [atk]"
  
label start:
  $ current = "gakk"
  call fight
  "---"
  $ current = "jakk"
  call fight
  "---"
  
  $ current = "gakk"
  call fight
  "---"

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Grabbing Variables

#3 Post by zmook »

FWIW, it is possible to do what you suggest (though you shouldn't. It's absolutely better to do what enaielei recommends).

Code: Select all

$ bad1_name = getattr(store, bad1+"_name")
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: Zapor