Page 1 of 1

Grabbing Variables

Posted: Sun Dec 12, 2021 1:44 am
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.

Re: Grabbing Variables

Posted: Sun Dec 12, 2021 2:10 am
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
  "---"

Re: Grabbing Variables

Posted: Sat Dec 18, 2021 2:20 am
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")