Beginner is confused

XYXZYZ

New member
I'm trying to learn basic scripting, the first thing I want to try is creating a variable for an item (such as apple), display "apples = x" on the HUD, and add +1 to apple total when I pick up an apple.
I'm looking through Bloodbane's Cherry on Top game to see how he does it with coins, but there's so much stuff there its kind of confusing.

First of all, I don't know C, the only programming language I'm familiar with is GML from Game Maker Studio (Though all my experience is from the older GM 8) and I haven't done it in a while so I'm a bit rusty.

To do this in GMS I would declare the variable (varapple = 0) in some controller object (like init_obj) that loads before the HUD loads, and in the HUD object's draw event have something like drawtext(x,y, "apple = " + init_obj.varapple) And I would put the increment in the item's collision event with the player (init_obj.varapple += 1)


In Openbor I'm not sure where to put the scripts. The HUD display text would go in updated.c right?

void main()
{
drawstring (10, 20, 2, "apple = " + varapple)
}


I assume the variable declaration would be something like

void main()
{
void oncreate()
{
setglobalvar(varapple, 0)
}
}

But I don't know where to put it. Is there some kind of pre-game initialize event?

Is there a file for the HUD? The old manual says lifebar.txt is just for colors, the new manual lists a bunch of HUD code but I don't see what file it applies to.
 
But I don't know where to put it. Is there some kind of pre-game initialize event?
There are a lot of those. Check the manual, there are many events which happens under certain circustances, like falling, moving on a certain axis, when joining, etc.

You can use, for example, onspawnscript to set the first value, as it runs only one time when you spawn.

For controlling the number of apples, you can either use an updatescript tied to the character or a global one (updated.c under scripts folder)
It has an oncreate section if you want to use, but it should be outside of the main() loop.

void main() {}

    • This is the main function for every script. main() is the main caller
void ondestroy() {}

    • Useful in update.c/updated.c script or in ondrawscript/updatescript/script events.
    • This function is called when the entity is killed or if in update.c/updated.c when close the engine.
    • Use this func to free your arrays/sprites for example
void oncreate() {}

    • Useful in update.c/updated.c script or in ondrawscript/updatescript/script events.
    • This function is called when the entity is created or if in update.c/updated.c when start the engine.
    • Use this func to init you vars for example

Also, there is update.c and updated.c

update.c

  • Engine starts update cycle.
updated.c

  • Engine completes update cycle.
 
Okay, in update.c I have
void oncreate()
{
setglobalvar("varapple", 0);
}

and it crashes when the level loads, log says exception while executing script 'update'. So I'm still doing it wrong...
 
Okay, in update.c I have


and it crashes when the level loads, log says exception while executing script 'update'. So I'm still doing it wrong...

Please actually read the log, you will find error details up stream.
 
Here is the log, I can't see anything that helps correct the script. Sorry if I'm being obtuse.

Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 6373376 Bytes

debug:nativeWidth, nativeHeight, bpp, Hz 1920, 1200, 24, 59

0 joystick(s) found!
OpenBoR v3.0 Build , Compile Date: Apr 8 2017

Game Selected: ./Paks/test.pak

FileCaching System Init...... Disabled
Initializing video............
'data/video.txt' not found.
Initialized video............. 320x240 (Mode: 0, Depth: 8 Bit)

Loading menu.txt............. Done!
Loading fonts................ 1 2 3 4 Done!
Timer init................... Done!
Initialize Sound..............
Loading sprites.............. Done!
Loading level order.......... Done!
Loading model constants...... Done!
Loading script settings...... Done!
Loading scripts.............. Done!
Loading models...............

Cacheing 'Flash' from data/chars/misc/flash.txt
Cacheing 'Kula' from data/chars/kula/kula.txt
Cacheing 'Max' from data/chars/maxima/maxima.txt
Cacheing 'Mandy' from data/chars/mary/mary.txt
Cacheing 'Apple' from data/chars/misc/apple.txt
Cacheing 'Meat' from data/chars/misc/meat.txt
Cacheing '1up' from data/chars/misc/1up.txt
Cacheing 'Coin' from data/chars/misc/coin.txt
Cacheing 'Gold' from data/chars/misc/gold.txt
Cacheing 'Time' from data/chars/misc/time.txt
Cacheing 'Box' from data/chars/misc/box.txt
Cacheing 'Crate' from data/chars/misc/crate.txt
Cacheing 'Trash' from data/chars/misc/trash.txt
Cacheing 'Table' from data/chars/misc/table.txt
Cacheing 'Chair' from data/chars/misc/chair.txt
Cacheing 'Fence' from data/chars/misc/fence.txt
Cacheing 'Drum' from data/chars/misc/drum.txt
Cacheing 'Trolly' from data/chars/misc/trolly.txt
Cacheing 'dummy' from data/chars/dummy/dummy.txt
Cacheing 'Knife' from data/chars/misc/knife.txt
Cacheing 'Shot' from data/chars/misc/shot.txt
Cacheing 'Star' from data/chars/misc/star.txt
Cacheing 'Steam' from data/chars/misc/steam.txt
Cacheing 'Steamer' from data/chars/misc/steamer.txt
Cacheing 'Furnace' from data/chars/misc/furnace.txt

Loading 'Flash' from data/chars/misc/flash.txt
Loading 'Kula' from data/chars/kula/kula.txt
Loading 'Max' from data/chars/maxima/maxima.txt
Loading 'Mandy' from data/chars/mary/mary.txt

Loading models............... Done!
Object engine init........... Done!
Input init................... No Joystick(s) Found!
Done!
Create blending tables....... Done!
Save settings so far........ Done!


Level Loading: 'data/levels/example1.txt'
Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 39022592 Bytes

Loading 'Apple' from data/chars/misc/apple.txt
Loading 'Dummy' from data/chars/dummy/dummy.txt

Level Loaded: 'data/levels/example1.txt'
Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 39256064 Bytes
Total sprites mapped: 374


********** An Error Occurred **********
* Shutting Down *

There's an exception while executing script 'update' Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 39268352 Bytes

Level Unloading: 'data/levels/example1.txt'
Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 39530496 Bytes

RAM Status:
Total Ram: 2147483647 Bytes
Free Ram: 2147483647 Bytes
Used Ram: 39165952 Bytes

Release level data...........
Done!

Release graphics data........ Done!
Release game data............

Unload 'Flash' ............Done.
Unload 'Kula' ............Done.
Unload 'Max' ............Done.
Unload 'Mandy' ............Done.
Unload 'Apple' ............Done.
Unload 'Dummy' ............Done.

Release game data............ Done!
Release timer................ Done!
Release input hardware....... Done!
Release sound system......... Done!
Release FileCaching System... Done!

**************** Done *****************

There's an exception while executing script 'update'
 
Here is the log, I can't see anything that helps correct the script. Sorry if I'm being obtuse.

Post the text file and the script file for us to check over. Please use code tags, not quotes for code and text samples.


DC
 
@XYXZYZ you need to create a scripts.txt inside your DATA folder and add this:

alwaysupdate {bi}

  • This command sets when update.c script is run
    • 0 = Only run update.c when game starts
    • 1 = Run update.c anytime including menu, scene, select screen etc
  • Use this command together with update.c of course
In your case, its alwaysupdate 1
 
I created the data/script.txt file, with alwaysupdate 1 in it.

in data/scritps/update.c:
C:
void oncreate()
    {
      setglobalvar("varapple", 0);  
    }
There's nothing else in the file.

The log file is attached. Surely there's some simple thing I'm missing...

By the way, this is the vanilla Beats of Rage game from the edit pack O Illusionista posted in "How to start on Openbor".
 

Attachments

Hmmm... try this for your update.c:
C:
void main()
{
  drawstring(30, 50, 2, "apple_=_1");
}

I know that it's too simple but at least, you should get this script working first.
 
Yes, it works and I had the text working earlier after following Msmalik's "noob's guide to scripting". Now I just need the variable it's drawing.
 
The log file is attached. Surely there's some simple thing I'm missing...
update and updated files needs a main loop, even its empty:

C-like:
void oncreate()
    {
      setglobalvar("varapple", 0);  
    }
    
void main()
    {
    int appleCount = getglobalvar("varapple");
    drawstring(30, 50, 2, "apple_=_"+appleCount);
    }
    
void ondestroy()
    {
      setglobalvar("varapple", NULL());  
    }
 
Last edited:
Okay, that works, thanks. So I have to define an integer and refer to that, instead of referring to the variable directly? I can't just use "apple = " + varapple", correct?

So now I have figure out how to increment the variable when I pick up an apple, I assume it would be something like "setglobalvar("varapple", varapple + 1)"? But again, I'm not sure where to put it. I see some people putting script into item.c and using a didhitscript in the item.



But before I get into that, what does "void" do? I see void main, void self, etc. before some scripts and not others, when do you use it? I can't find an explanation in the manuals.
 
But before I get into that, what does "void" do? I see void main, void self, etc. before some scripts and not others, when do you use it? I can't find an explanation in the manuals.
OpenBOR script is C-based (a weak typed C, as far as I remember), so you can take a look at C definitions.
In other words, void doesn't return any value.

I can't just use "apple = " + varapple", correct?
in fact, you can - You can use it as this:
C-like:
drawstring(30, 50, 2, "apple_=_"+getglobalvar("varapple"));

But I think it's better to define a local variable with a value inherited from a global variable than to keep referring to a global variable all the time.

@DCurrent can give you more in depth explanation on this.
 
Back
Top Bottom