Drawing text from the database and adding variables [SOLVED]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Drawing text from the database and adding variables [SOLVED]

Post by alexrules01 »

Ok the title might sound... weird lol, but what I am trying to set up is like a fighting game, where a fighter can learn new moves etc.
What I want to do is, when a fighter uses a move (for arguement sake, lets say a Punch) I want to draw from the database a set-up of the move then the execution.
So, for example, i want it to read
"$Fighter1 pulls his arm back"
"And hits $Fighter2 with his fist using a $Punch"
I have put dollar signs to show what are variables. I know how to get the $Fighter1 and $Punch variables in, but I am trying to figure out how to incorporate $Fighter2.
I want the code to be dynamic, so it can cater for many moves, like say
"$Fighter1 jumps up and down like crazy and $Fighter2 is so scared"
"$Fighter2 runs away in fear and Fighter1 wins the fight, using a I pwn n00bs"

So, I want to be able to dynamically incorporate variables within text from the database. Can this be done in PHP or do you need another language?
Last edited by alexrules01 on Mon Jul 18, 2011 3:22 pm, edited 1 time in total.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Drawing text from the database and adding variables

Post by hallsofvallhalla »

create a table called actions and a table called reactions then randomly pull those.
so an example of an action would be
" swings wildly "
" jumps up and kicks "

reactions
"falls to the ground"
"blocks bravely"

so you roll attack and defense
if hit or not goes in $didhit, then have the table for reactions have a field for miss or hit,

echo $fighter1 . $action . " and " . $didhit;
echo $fighter2 . $reaction

so it might look like this
george swings wildly and hits
Bob falls to the ground.
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Drawing text from the database and adding variables

Post by alexrules01 »

Thanks halls if there is a way to do what I want I will do, but I musn't have been clear enough.

Within the move description text, i want to be able to include a players name, which is stored in a variable.
Im not sure if there is a command or somthing. I'll use your example as what I want:

george swings wildly and hits bob
Bob falls to the ground and George celebrates.

How would I incorporate the variable within the text? I was hoping you could have the variable in mySQL but it doesnt work like that lol
Hope that is clearer?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Drawing text from the database and adding variables

Post by Jackolantern »

Very simply. You have probably done the same thing elsewhere in your code without knowing it. You could just do something like this:

Code: Select all

echo $playername." swings wildly at ".$enemyname;
Where you are getting the playername and enemyname variables is a matter of how your game is structured.
The indelible lord of tl;dr
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Drawing text from the database and adding variables

Post by alexrules01 »

Sorry its kinda hard for me to explain what I need.

jack, what you said, is what I want, but the text is called from the database, and so I can;t have the code as a constant like that.

So in the databse I have the Name of the move, and the description of the move.
If I have a ton of moves they are all going to hav a different description. I want to be able to insert a players name, a variable, at anytime within the text from the database.

In the database I would really love to have it like [$Player1 . "hits" . $Player2 . "punch"] but I know i can't have that.
I'll make some visuals to try and explain it better
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Drawing text from the database and adding variables

Post by alexrules01 »

Heres a very basic database setup, I will call move to show what move the player is using, and the setup of the move description and the execution of the move description. The line is the place I would have the variable.
Image

This is from another game, which I used to play, so I am using it as a kind of base of what I want to achieve. The name highlighted in Green and the Move highlighted in blue will be at the start and the end, so that should be easy enough. But the names highlighted in red is the variable I am not sure how to insert
Image

Image

As you can see I have two moves with different descriptions. Even the second one as a name twice within the description.

So I am asking, is there a way I can insert a variable within the text that is retrieved from the database?
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Drawing text from the database and adding variables [SOLVED]

Post by alexrules01 »

I think I have solved it! Well not me, but i got the answer I needed.

I needed to use the str_replace() function, which is a pretty nifty thing :P

Heres a link if anyone needs it.

http://php.net/manual/en/function.str-replace.php
Post Reply

Return to “Advanced Help and Support”