<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>A function from a file... without a big switch... Just look here!</title>
		<link>http://www.allegro.cc/forums/view/295837</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 24 Aug 2003 19:45:50 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yay for the music note <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />... Anyways...<br />So I have a list of spells in a file. Like say:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//name, MP Req</span>
Kill Monsters, <span class="n">999</span>
Kill People, <span class="n">999</span>
Yo Mamma, <span class="n">999</span>
You`re A Lil` Girl, <span class="n">0</span>
</pre></div></div><p>
Right, but I want these to call their own functions to calculate damage/look cool/whatever. So... how do I do that? Is there a way without a big <span class="cuss"><span>ass</span></span> switch (or in this case a huge string of (else)ifs for the name)? Can I like say:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//name, MP Req, function()</span>
Kill Monsters, <span class="n">999</span>, KillMonsters
Kill People, <span class="n">999</span>, KillPeople
Yo Mamma, <span class="n">999</span>, YoMamma
You`re A Lil` Girl, <span class="n">0</span>, LilGirl
</pre></div></div><p>
Except that&#39;s all hard to do and possibley impossible <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />. Any ideas/comments/random stuff welcome!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 23 Aug 2003 06:32:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmm..Try making a list of Spell Names, and then a function type to call..that may be helpful...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kanzure)</author>
		<pubDate>Sat, 23 Aug 2003 06:40:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It seems to me you might be able to do some sort of struct with function pointers... I would normally work that idea into workable code and post the code, but I don&#39;t really feel like working it out right now because i&#39;m lazy <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />  But anyway, thats all I really do, just think of some sort of seemingly good option and work it out.  Usually they work, but in this case I&#39;m not sure.  You&#39;ll just have to experiment with that one...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ROSTheFuture)</author>
		<pubDate>Sat, 23 Aug 2003 06:41:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Kanzure said:</div><div class="quote"><p>Hmm..Try making a list of Spell Names, and then a function type to call..that may be helpful...</p></div></div><p> I already asked you, WTF?<br />ROS: Hmm, but wouldn&#39;t that sill come down to a big old else/if dealy?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 23 Aug 2003 06:48:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Something like this, I think he suggested:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">struct</span> <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>string<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">void</span> <span class="k2">(</span><span class="k3">*</span>func_ptr<span class="k2">)</span><span class="k2">(</span><span class="k1">void</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k2">}</span> func_list<span class="k2">[</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span></td></tr><tr><td class="number">5</td><td> <span class="k2">{</span> <span class="s">"Function 1"</span>, func_ptr1 <span class="k2">}</span>,</td></tr><tr><td class="number">6</td><td> <span class="k2">{</span> <span class="s">"Function 2"</span>, func_ptr2 <span class="k2">}</span>,</td></tr><tr><td class="number">7</td><td> <span class="c">//ect...</span></td></tr><tr><td class="number">8</td><td> <span class="k2">{</span>NULL,NULL<span class="k2">}</span></td></tr><tr><td class="number">9</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td><span class="c">// in function:</span></td></tr><tr><td class="number">12</td><td><span class="k2">{</span></td></tr><tr><td class="number">13</td><td>  <span class="c">// Search through the list</span></td></tr><tr><td class="number">14</td><td>  <span class="k1">for</span><span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>func_list<span class="k3">&lt;</span>i&gt;.string<span class="k2">;</span><span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span></td></tr><tr><td class="number">15</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>    <span class="c">// If the names match, run the function</span></td></tr><tr><td class="number">17</td><td>    <span class="k1">if</span><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_754.html" target="_blank">strcmp</a><span class="k2">(</span>string_to_check, func_list<span class="k3">&lt;</span>i&gt;.string<span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">18</td><td>      func_list<span class="k3">&lt;</span>i&gt;.func_ptr<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Yes, it&#39;s basically a big if/else statement, but it&#39;s a very compact big if/else statement..
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 23 Aug 2003 08:59:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I know some C puritans can get a bit irritated, but there I go:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="p">#include &lt;map&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;string&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">typedef</span> std::map<span class="k3">&lt;</span>std::string, <span class="k1">void</span> <span class="k2">(</span><span class="k3">*</span><span class="k2">)</span><span class="k2">(</span><span class="k2">)</span><span class="k3">&gt;</span> funct_map<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>funct_map fm<span class="k2">;</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">void</span> KillMonsters<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"KillMonsters\n"</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">9</td><td><span class="k1">void</span> KillPeople<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"KillPeople\n"</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">10</td><td><span class="k1">void</span> YoMamma<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"YoMamma\n"</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">11</td><td><span class="k1">void</span> LilGirl<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"LilGirl\n"</span><span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="k1">void</span> fill_fm<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td><span class="k2">{</span></td></tr><tr><td class="number">15</td><td>  fm<span class="k2">[</span><span class="s">"KillMonsters"</span><span class="k2">]</span> <span class="k3">=</span> KillMonsters<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  fm<span class="k2">[</span><span class="s">"KillPeople"</span><span class="k2">]</span> <span class="k3">=</span> KillPeople<span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  fm<span class="k2">[</span><span class="s">"YoMamma"</span><span class="k2">]</span> <span class="k3">=</span> YoMamma<span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  fm<span class="k2">[</span><span class="s">"LilGirl"</span><span class="k2">]</span> <span class="k3">=</span> LilGirl<span class="k2">;</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr><tr><td class="number">20</td><td>&#160;</td></tr><tr><td class="number">21</td><td><span class="k1">void</span> execute<span class="k2">(</span>std::string function_name<span class="k2">)</span></td></tr><tr><td class="number">22</td><td><span class="k2">{</span></td></tr><tr><td class="number">23</td><td>  <span class="c">//if the function doesn't exist it'll crash here</span></td></tr><tr><td class="number">24</td><td>  <span class="c">//using exceptions would be a good idea ^_^</span></td></tr><tr><td class="number">25</td><td>  fm<span class="k2">[</span>function_name<span class="k2">]</span><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td><span class="k2">}</span></td></tr><tr><td class="number">27</td><td>&#160;</td></tr><tr><td class="number">28</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">29</td><td><span class="k2">{</span></td></tr><tr><td class="number">30</td><td>  fill_fm<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>  std::string name<span class="k2">;</span></td></tr><tr><td class="number">32</td><td>  std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Enter function name: "</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>  std::cin <span class="k3">&gt;</span><span class="k3">&gt;</span> name<span class="k2">;</span></td></tr><tr><td class="number">34</td><td>  execute<span class="k2">(</span>name<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

That could be a C++ solution (you could also try with the hashed map (that isn&#39;t part of the standard)).</p><p>If you&#39;ren&#39;t using C++ you could try to implement the concept of the map and use it in a similar way.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sat, 23 Aug 2003 12:12:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use reflection to call the correct method. That should work well if you know what class will hold your methods. And if you use Java, of course.</p><p>But generally speaking, it&#39;s not that good if each attack uses it&#39;s own way to calc damage anyway.</p><p>Just specify the to-hit-chance, the damage, the type of attack / type of damage, any secondary effects and possible state changes (stunned 50%, confused 10%, etc.) for example.</p><p>That way you can just read in the data and use it. It makes it also more easy tweak these values later on.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sat, 23 Aug 2003 12:27:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>KittyCat: That&#39;s a very good idea, just using a for loop.<br /> <br />Ceniza: I&#39;ve never used any type of map before, I never knew what it was for. I guess now I know <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />. I&#39;m not a C puritan, but I don&#39;t like std::strings, so if anything I&#39;ll be using char arrays there <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Spelly: I have no idea what reflection is... Sorry, I&#39;m not that smart <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" />. I didn&#39;t understand the first part of your post <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" />, but as for the part about the fact that each attack uses it&#39;s own calc damage thing. Well, see I want them to take into account the characters magic, strength, whatever as well as the targets. Maybe I want different amounts for different spells. Also, I want different special effects for each one. Maybe for the spell  &quot;KillPeople&quot; I want some giant squid to come and slap the people around with it&#39;s tentacles, but for the spell &quot;LilGirl&quot; I want a little girl to come out and b*tch slap the enemy. See?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 23 Aug 2003 21:46:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>KittyCat was thinking exactly like what I was thinking, I was just too lazy at the time to think it out thourghougly like he did.</p><p>I personally like the map idea, myself.  That would have never occurred to me, because I had no idea you could do that.... very interseting.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ROSTheFuture)</author>
		<pubDate>Sat, 23 Aug 2003 21:47:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Synapse Jumps said:</div><div class="quote"><p> I&#39;m not a C puritan, but I don&#39;t like std::strings, so if anything I&#39;ll be using char arrays there </p></div></div><p>

Well, you&#39;re free for don&#39;t liking std::string but I still recommend you using it in the map and then use C-strings everywhere else (for your personal pleasure <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />), otherwise you&#39;ll get a lot of funny surprises when using map.</p><p>Remember when you use std::string you have implicit conversion at your disposal, so, exactly in the same example code you could change main to:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  fill_fm<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <span class="k1">char</span> name<span class="k2">[</span><span class="n">128</span><span class="k2">]</span><span class="k2">;</span>
  std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Enter function name: "</span><span class="k2">;</span>
  std::cin <span class="k3">&gt;</span><span class="k3">&gt;</span> name<span class="k2">;</span>
  execute<span class="k2">(</span>name<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

and that way you feel better and map will work as you need it to work.</p><p>If you&#39;re wondering why &lt;b&gt;char  isn&#39;t a good choice for map, just remember the <b>operator ==</b> for a C-string will compare the memory address and not the content.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sat, 23 Aug 2003 22:12:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Aha! Great. It works beautifully! Thank you. I&#39;m still sad that I have to use std::strings, but what&#39;cha gonna do (except over-ride the global == operator, which will lead to my next question). And since we&#39;re on the subject, here&#39;s why I hate std::strings: sizeof(std::string) == 28, sizeof(char *) == 4 <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />. Anyways, so I tried to over-ride the global ==operator and I did it like this (for char *s only)
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">bool</span> <span class="k1">operator</span><span class="k3">=</span><span class="k3">=</span><span class="k2">(</span><span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>left, <span class="k1">const</span> <span class="k1">char</span> <span class="k3">*</span>right<span class="k2">)</span><span class="k2">{</span>
  <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_761.html" target="_blank">stricmp</a><span class="k2">(</span>left, right<span class="k2">)</span><span class="k2">)</span>
    <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
  <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
And it says this:
</p><div class="source-code snippet"><div class="inner"><pre>main.cpp<span class="k2">(</span><span class="n">6</span><span class="k2">)</span><span class="k2">:</span> error C2803: <span class="s">'operator =='</span> must have at least one formal parameter of <span class="k1">class</span> type
</pre></div></div><p>Whassat mean? If this is impossible, I&#39;ll settle for using std::strings, but this just seems easier <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>EDIT: Also, what do you think, should I load the list from the file each time in battle, this way I don&#39;t have to keep track of the spells the first way, but the second way is quicker.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sat, 23 Aug 2003 22:46:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Reflection is pretty easy.<br />Mainly it allows you to take a look at all methods, constructors, fields etc. Quite handy.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> call<span class="k2">(</span>Object objectToCall, String theFunction<span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">try</span> <span class="k2">{</span>
        Method m <span class="k3">=</span> objectToCall.getClass<span class="k2">(</span><span class="k2">)</span>.getMethod<span class="k2">(</span>theFunction, null<span class="k2">)</span><span class="k2">;</span>
        Object result <span class="k3">=</span> m.invoke<span class="k2">(</span>null<span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span> <span class="k1">catch</span> <span class="k2">(</span>Exception ex<span class="k2">)</span> <span class="k2">{</span>
        ex.printStackTrace<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

regarding your problem... I know that this is not what you want to do, but still...<br />if you have magic spells which call other creatures, you could have something like:</p><div class="source-code snippet"><div class="inner"><pre>SummonLittleGirl, Single_Target, <span class="n">1</span> round, Lvl<span class="k3">*</span><span class="n">2</span>, LittleGirl
</pre></div></div><p>
Which would mean that the &quot;summon little girl&quot; spell summons a little girl of twice the level of the caster.</p><p>of course you&#39;ll need to specify the little girl as well:</p><div class="source-code snippet"><div class="inner"><pre><span class="k2">[</span>LittleGirl<span class="k2">]</span>
type<span class="k3">=</span>phsyical
DAM<span class="k3">=</span><span class="n">10</span> <span class="k3">+</span> <span class="k2">(</span>lvl<span class="k3">/</span><span class="n">2</span><span class="k2">)</span>
Atk<span class="k3">=</span><span class="n">5</span><span class="k3">+</span>lvl
gfx<span class="k3">=</span>
sound-<span class="k3">=</span>
</pre></div></div><p>

That way you&#39;d still be pretty flexible. And if you want to the girl to fight for two rounds, all you need is to change is one number.<br />Also, summoning a dragon attacking several targets is as easy:
</p><div class="source-code snippet"><div class="inner"><pre>SummonDragon, Group, <span class="n">1</span> round, lvl <span class="k3">*</span> <span class="n">1</span>, RedDragon


<span class="k2">[</span>RedDragon<span class="k2">]</span>
type<span class="k3">=</span>fire
Dam<span class="k3">=</span>lvl d12
Atk<span class="k3">=</span><span class="n">20</span><span class="k3">+</span>lvl<span class="k3">/</span><span class="n">2</span>
gfx<span class="k3">=</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_735.html" target="_blank">sound</a><span class="k3">=</span>
</pre></div></div><p>

Of course, if you just need a few spells, hardcoding them is quicker. </p><p>But... in that case you won&#39;t need any external files in the first place. You could just hardcode the spell description as well...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sun, 24 Aug 2003 00:01:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You overload operators for user defined types and &lt;b&gt;const char  looks like a built-in type for me <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>So you dislike <b>std::string</b> &#39;cause it&#39;s 24 bytes bigger? How unfair <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Those 24 extra bytes are worth it IMO and they won&#39;t hurt that much.</p><p>Think about common implementations that need temp <b>char []</b>s for storing stuff, most of those implementations like to use 512 bytes + 4 of the pointer = 516 bytes, and, in most cases, they use less than 40 of those 512 bytes... so... is <b>std::string</b> still that bad?</p><p>What if they need 512 or more bytes? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Also remember that asking for the length of a C-string (a very common &quot;task&quot;) has linear complexity while asking for the length of a <b>std::string</b> always takes constant time.</p><p>It&#39;sn&#39;t that bad after all, is it? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sun, 24 Aug 2003 00:14:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Woh, why didn&#39;t I reply to this til now? Oh well... <br />Spelly: That reflection stuff makes NO sense to me. I mean, I could see passing an object and stuff, but .getClass()? I&#39;ve never EVER seen that... anyways... I proly will use some summoning stuff like that. And I don&#39;t really want to hard-code the spells into the game, because I want this game to get very large and flexible <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />. Eventually, I want to make a spot where you can make your own spell and item. That would be pimp <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p><p>Ceniza: ... ... Okay, I can see using std::string where I REALLY need to, and I can only find two places where I really need it. 1) for this big ol&#39; buffer I have (except, how can I pass it like a c-string? If I pass .c_str to a function that writes to a c-string will it write on it?) 2) With this whole map dealy. </p><p>EDIT: I still wanna know why my overloaded operator== isn&#39;t right? Anyone know, or is it just not possible? I&#39;ve settled for strings, but still...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 02:52:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Synapse Jumps said:</div><div class="quote"><p> If I pass .c_str to a function that writes to a c-string will it write on it? </p></div></div><p>

Well, c_str() doesn&#39;t return the internal data of std::string, it&#39;s just a copy, and even if it returned the internal data stored in std::string, the return type is &lt;b&gt;const char , so you can&#39;t modify it.</p><div class="quote_container"><div class="title">Synapse Jumps said:</div><div class="quote"><p> I still wanna know why my overloaded operator== isn&#39;t right? Anyone know, or is it just not possible? </p></div></div><p>

As I said, &lt;b&gt;const char  is a built-in type and you can only overload operators for the types you create.</p><p>It wouldn&#39;t be smart to allow the user change the meaning of those operators for built-in types, like making:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> a <span class="k3">=</span> <span class="n">2</span>, b <span class="k3">=</span> <span class="n">7</span>, c <span class="k3">=</span> <span class="n">9</span><span class="k2">;</span>
c <span class="k3">=</span> c <span class="k3">+</span> a <span class="k3">*</span> b<span class="k2">;</span> <span class="c">// &lt;--</span>
</pre></div></div><p>

get a totally different meaning.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceniza)</author>
		<pubDate>Sun, 24 Aug 2003 03:55:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Spellcaster:  isn&#39;t that Java?  It would be nice to be able to call a function by its name like that in C or C++...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ROSTheFuture)</author>
		<pubDate>Sun, 24 Aug 2003 07:55:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Ceniza said:</div><div class="quote"><p>Well, c_str() doesn&#39;t return the internal data of std::string, it&#39;s just a copy, and even if it returned the internal data stored in std::string, the return type is const char *, so you can&#39;t modify it.</p></div></div><p>
Well there&#39;s another down point to std::strings... <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><br />And the rest of your post makes sense. </p><p>ROS: That makes sense... I was gonna say I didn&#39;t recignize any of that!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 08:51:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Well there&#39;s another down point to std::strings...::)</p></div></div><p>Especially if you aren&#39;t familiar with c_str()&#39;s sister functions, data() and copy() <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /> ...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sun, 24 Aug 2003 09:21:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">23 said:</div><div class="quote"><p>Especially if you aren&#39;t familiar with c_str()&#39;s sister functions, data() and copy()</p></div></div><p>ACTUALLY I&#39;m not. I assume data rerutns the data in char * format? And copy...? No one said anything about this...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 09:24:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Ceniza is right; the data returned by c_str() and data() is constant (would be rather dangerous otherwise) so copy() can copy a string&#39;s contents into your own char array so you can do whatever you want with it.</p><p>RMFA <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sun, 24 Aug 2003 09:29:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well that&#39;s no good, I can&#39;t send the thing to a function! The reason I wanted it was instead of having a huge <span class="cuss"><span>ass</span></span> buffer. So I can&#39;t even do this? See strings ARE stupid <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 09:30:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Yes, much smarter to allow direct access to private data so you can play merry hell with it <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /> Of course you can send it to a function; what&#39;s the problem?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sun, 24 Aug 2003 09:35:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, you can&#39;t send it to a function, properly. You can&#39;t do this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.delorie.com/djgpp/doc/libc/libc_320.html" target="_blank">fgets</a><span class="k2">(</span>some_std_string, <span class="n">99999</span>, some_file<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Now can yah? You can&#39;t do it with anything! None of std::strings members!</p><p>EDIT: As to not get a post cap. F**K IOSTREAMS! DON&#39;T EVEN GET ME STARTED ON THOSE DUMB A*S THINGS!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 09:39:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Quit mixing C and C++. Works fine with iostreams <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Sun, 24 Aug 2003 09:42:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Spellcaster: isn&#39;t that Java? It would be nice to be able to call a function by its name like that in C or C++...
</p></div></div><p>
Of course it is Java. I even said so in my first post mentioning reflection <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>nd I don&#39;t really want to hard-code the spells into the game, because I want this game to get very large and flexible . Eventually, I want to make a spot where you can make your own spell and item. That would be pimp </p></div></div><p>
Um... if you want to do that, you should just define your spells in an external file, not hardcode a function call.</p><p>Take a look at the spell descriptions of freely available RPG systems, say d20 or GURPS light or Fudge or whatever.</p><p>What you&#39;ll see is that these description classify a spell by its range, the number of targets, how the spell is targeted, what &quot;schools of magic&quot; the spell belongs to, how the traget can defend itself and finally also the damage that is made (if any) and if the state of the target can be affected.</p><p>For a computer game, you will also need to add some description on how the spell effect looks and sounds.<br />That means you might have a list of graphic styles for missles, beams, auras cunjuration of monsters, etc.</p><p>Oh, reading through some of the pen&amp;paper RPGs will also give you an idea how you can create a system in which you can explain both magic and normall attacks using just a few stats.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (spellcaster)</author>
		<pubDate>Sun, 24 Aug 2003 13:36:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Well, c_str() doesn&#39;t return the internal data of std::string, it&#39;s just a copy, and even if it returned the internal data stored in std::string, the return type is const char *, so you can&#39;t modify it.</p></div></div><p>

Half true. &quot;c_str()&quot; can return the actual string, provided the implementation is smart enough to add the &quot;\0&quot; character to its internal representation of strings, which it can (and should) do. After all, it has its own internal count of the size, so adding a &quot;\0&quot; to the end of it is fine. &quot;c_str()&quot; ought to be a light-weight operation; std::strings do enough copying on their own.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>You can&#39;t do it with anything! None of std::strings members!</p></div></div><p>

Nor should you. If you want to use &quot;fgets&quot;, you will have to create a char* yourself.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Quit mixing C and C++. Works fine with iostreams </p></div></div><p>

Or, you can write an &quot;fgetstring&quot; like so:</p><div class="source-code snippet"><div class="inner"><pre>std::string fgetstring<span class="k2">(</span>FILE <span class="k3">*</span>pFile<span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">char</span> strGetString<span class="k2">[</span><span class="n">16384</span><span class="k2">]</span><span class="k2">;</span>
  <a href="http://www.delorie.com/djgpp/doc/libc/libc_320.html" target="_blank">fgets</a><span class="k2">(</span>strGetString, <span class="n">16383</span>, pFile<span class="k2">)</span><span class="k2">;</span>
  <span class="k1">return</span> string<span class="k2">(</span>strGetString<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Simple. Then, you don&#39;t have to resort to rewriting all your code to support iostreams.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Korval)</author>
		<pubDate>Sun, 24 Aug 2003 14:36:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Spelly said:</div><div class="quote"><p>Um... if you want to do that, you should just define your spells in an external file, not hardcode a function call.</p></div></div><p>I am doing that, just not the damage calc functions and graphic functions. I need the map because I am getting the string from the file with all the stuff about the spell and it&#39;s function, and I don&#39;t want to send the part of the string containing the funciton through a big number of stricmps to find what function to send it to, so the map...works...
</p><div class="quote_container"><div class="title">Korval said:</div><div class="quote"><p>Or, you can write an &quot;fgetstring&quot; like so:</p></div></div><p>
But... the whole reason I was trying to use an std::string was because I didn&#39;t want to make a big ol&#39; char buffer <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Synapse Jumps)</author>
		<pubDate>Sun, 24 Aug 2003 19:45:50 +0000</pubDate>
	</item>
</rss>
